AWS LAMBDA For The .NET Developer: How To Easily Get Started | AWS LAMBDA SERIES

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
aws lambda is a serverless compute service that lets you run code without managing or provisioning servers creating workload aware cluster scaling logic maintaining event integrations and managing runtimes in short aws lambda is amazon's answer to serverless compute services that allows you to run code with zero administration of the infrastructure required to run it aws lambda allows us to upload the code to it and then takes care the compute execution power required to run the code embracing services like this which requires little or no infrastructure maintenance from an application developer is usually termed as serverless hello everyone my name is rahul and welcome back to this youtube channel couple of months back i joined ofx and i've been working with the global currency account team the team is mostly on amazon cloud and building applications on it now i am quite new to this and much of my experiences has been with the microsoft azure cloud where i am also a microsoft mvp but this calls for some additional learning and i am really excited about it my core programming language is still c-sharp and mostly asp.net in this video let's see how to get started with aws lambda as a dotnet developer i will show you right from starting a new project the tools and packages available how to debug and run aws lambdas on your local development machine and then we'll also deploy this to the aws environment and see it function there as well finally we'll have a quick sneak peek on how aws lambda works internally and what happens when a lambda is triggered so without much delay let's jump right into it let's head off to visual studio to create our first aws lambda project i have visual studio open here i'm using the 2019 version and on the latest update visual studio does not support creating aws lambda or in fact any projects for the aws cloud by default to do this we need to add an extension so let's skip creating a project in this page and choose the continue without code option once that's selected we can go into extensions and click manage extensions here we can search online in the box here using the aws toolkit keyword this lists back the aws toolkit for visual studio 2017 and 2019. select that and install this for your visual studio since i have it already installed it shows this green tick if not you will get this download or the install button so make sure you get this and then you might have to restart the visual studio ide again once that's done let's close this let's go to file new project now you can see here there is an option for aws and also it gives you different options inside here so let's choose serverless because that's the kind of applications that we are going to build it gives you different options right from building a serverless application or a lambda project let's choose the aws lambda project dot net core c sharp this is the most basic template so let's select that and click next it asks you to give the name let's make sure it's in the appropriate location and then give this a project name let's keep this as my first lambda and click create now this prompts you to select a blueprint blueprints are lambda projects that contains some getting started code so it has some things already wired up the different options you can see here is to start with an empty function create something to integrate with dynamo or sqs sns dotnet 5 etc let's choose empty function and see how everything works together and build it up from scratch so let's click finish and this will create the new project for you the aws toolkit that we just saw for visual studio is also available for other popular ides like this one here it shows the jet brains rider 1 and there's also one from visual studio code and many other ides out there so you can pick and choose the toolkit based on the ide that you are working on currently so let's go back to the project the project is created successfully and we can see it has the function.cs file which is of importance to us let's double click to open that and we can see a function handler method which simply takes in a string input the lambda context and then returns back the string in an uppercase so this is a symbol function which takes in any string and returns an uppercase variant of that if you scroll up we can see this is also attributed with the lambda serializer attribute this enables to serialize and deserialize the string content coming into the lambda function in here by default this is set to a lambda json serializer if we open up the dependencies we can see two packages that is the amazon lambda core and also one for the amazon lambda serialization so this lambda serializer and the i lambda context that we saw is coming from the core library and the default serializer is coming from this serialization library so these are two nuget packages that are already added in with the project template if you right click the project and go to properties we can see the target framework is default to dotnet core 3.1 at the time of recording this video the dotnet core 3.1 is the officially supported version of.net you can read more about the announcement for 3.1 support in this blog here and the various things that it supports i'll make sure to put a link to this in the description below now even though we have dotnet 5 and even dotnet 6 almost there aws lambda still does not support it out of the box this is because dot net 5 is currently under the current support level and the aws teams policy has been to support lts versions of languages and runtimes so that in.networld is currently.net core 3.1 the next lts support version of dotnet is dotnet 6 and we should be seeing that soon on aws lambda as well if you want to create the projects using the command line you can also do that for this you will need to first install a set of templates using the dotnet tool so if you go to this link which will be linked in the description below you can get the required commands so if we copy this.net new minus i amazon lambda templates and run this in your command line we will have all the templates using the.net cli tool as well since i have this already run i have it installed now if i switch over to the command line and use the dotnet new command i can see all the templates that's supported by this cli if we scroll up i can see that we have the lambda functions and the runtimes in here as well to create the project very similar to the one we just created you can use the lambda dot empty function however note this will also create the test projects by default but it doesn't differ from what we have right now so let's come back to the ide and use the application we created from there to run this application on the local development environment we can see there is already an option set up here using the mock lambda test tool so how is this populating up here this is a profile that is added as part of the project under the properties and the launch settings file this is very similar to the profiles that gets set up in asp.net core applications if we look at this we can see there is a profile with the name mock lambda test tool which is appearing up on that run button and it specifies it's an executable command specifies the port and also the actual executable that needs to be run it points to the dotnet lambda test tool which is getting installed as part of the toolkit let's run this to see what exactly happens let's click this or press f5 to run it now this by default launches a console application which is the lambda test tool that we just linked to in the profile and this starts it up on the local host port 5050. so now we have an application that's listening to this port which we can hit to so let's navigate to our browser where this is getting automatically launched so here we can see that there is the aws.net core mock lambda test tool this allows us to run the lambda function code inside this tool and the ide is automatically attached to the debugger so let's switch back to the ide navigate to our function.cs and make sure to put a breakpoint up here let's navigate back to the browser here we can see the config file which is the aws lambda tools default which is part of our project it also specifies the function and the credential profile so how is this function name coming up here this function name is coming up from this aws lambdatoolsdefaults.json if you navigate back to the code and to the json file we can see this function handler the exactly the same as we just saw the function handler is in a specific format if you see it specifies the assembly type first and then uses a double colon and then specifies the full namespace of the class that is my first lambda.function in our case because it's inside the function.cs class and it also specifies the function name in our case this is function handler this is how the function handler gets specified if you want a more friendly name to appear in there we can use the function dash name attribute and give it a friendlier name so let's call this my first lambda let's save this and run this again to see if that gets effect it launches the lambda tool again and then it browses automatically to localhost 5000. here we can now see the function is named myfirstlambda let's enter in an input so in this case it expects a string so let's say hello lambda and then execute this using the button up here so this hits the debugger as expected it gets the input which is hello lambda and calls the two upper function on this it returns the result and we can see this inside the lambda test tool so if we scroll down there is a response section down below and we can see the two uppercase string for the string that we just passed in if we navigate back to the code we saw initially we had specified a json serializer this means we should be able to take in a json data input as well to see this in action let's create a class to represent the json structure so let's create a public class and specify my data and then add in a new property let's name it name which is a string property so now we can update the string to be mydata and then also call this as data to start using the mydata class let's update this code to reply hello and the name so let's use string interpolation and specify data dot name and then call the two upper on that as well so now in this function we have the function handler taking in a type of my data which has a name property and we return back hello and that name in uppercase let's run this again to see it in action we have the mock lambda tool running so let's pass in the input so let's specify the name attribute on the json and give my name in this example so let's click execute function again the function handler is hit if we expand over data we can see now this has the name rahul so let's return back the response and this should show appropriately inside the response so here we have hello rahul in uppercase as expected the lambda serializer attribute right now is applied at the assembly level this means any function that's part of this project is going to use the same serializer you can also specify the serializer at a function level so instead of specifying it at the assembly level let's remove this from here and specify it right above the function that we need since this is at a function level we no longer need the assembly attribute in here so let's remove that and this is going to function just as before so let's run this to see this in action i can pass in the same content so let's say name and pass in rahul and then click execute function now this also uses the default lambda serializer because this is attributing this current function let's hover over data and we can see the value populating as expected so let's continue execution and it works as expected so you can use this if you want different serializer and if you have multiple functions in the same project we will see how we can do this in a future video now that we have the lambda function running and working locally as expected let's see how we can deploy this to an aws environment the easiest way to do this from visual studio is to navigate to the project right click and choose the publish to aws lambda option however in a real life project i would be setting up a build deploy pipeline but that's a separate topic for a completely different video so let's use this for now and see this in action to just get started on how things work so let's choose the publish to aws lambda option and this prompts up a dialog here you can see i already have a default profile setup now you can set up one for yourself on your local ide using this plus button in here all it asks is an access id and a shared access key this is available once you set up the aws account now if you don't have an aws account you can check out the aws free tier option which gives you free services and things to try out for a year this is quite useful and it has almost all the services in here that is available for free but in restricted amounts this is good enough to learn and give things a try so i am using this for my current learning and the videos that i'm making so let's navigate back once you have the account profile set up now you have the other options so you see here there is a region that this is getting deployed too now you can choose different regions that's available so let's choose sydney in this case now the package type by default is zip and it specifies the lambda runtime as dot net core version 3.1 it has also picked up the name of the function from the configuration file and also the other settings in here you can see that the handler method is also getting specified inside here this is what is used by the lambda to invoke this particular function so we have the breakdown of this long string which is an assembly type and the method like we saw before let's click next to move on now this prompts you to select an iam role for the aws lambda to execute now this has a lot of different templates available by default so let's choose the aws lambda basic execution role so this gives us the permissions to write to cloudwatch logs as well so let's select that let's ignore the other settings and leave it to their defaults because i'm not using it in the current video let's click upload and that starts uploading this function let's uncheck this so that it doesn't automatically close the wizard if we expand this we can see the log of actions that's happening to create the aws lambda inside here as you can see it first creates the lambda role and then it executes the dotnet publish command which publishes the output of this project and then it zips up into a folder now you can see the different files that's getting zipped up including a runtime config file which is automatically generated by this template so if you navigate off to this folder let's copy this path to the publish folder and navigate there we can see the runtime config.json file in here let's open this up in an ide and this specifies the runtime that's used by the aws lambda so here since our project is using.net core app 3.1 this specifies the version as that we will see a moment later how this is getting used by aws lambda let's navigate back to visual studio the publish is successful so let's close this dialog and we can see that it has automatically launched up the function myfirstlambda inside here now if you want to navigate to this function again let's say if this was accidentally closed or not opened you can go to it by view and then going to aws explorer this explores all the different things that's there in your aws account so right at the bottom of this we can see aws lambda and the my first lambda double clicking that is going to open that same window again now this is looking at the deployed version of the function in the aws runtime if you were to specify the sample request let's say a json input with the name property and specify the name again let's say rahul close this and then invoke this function from here this is going to actually invoke the function on aws lambda so you can see here in the below the log output which gives in a request id and it ends it immediately it also specifies the duration that the function ran for on the right side you can see the return message which is the capitalized hello rahul message if you want to see this from the aws console we can navigate to a browser and navigate to the aws.amazon.com this prompts you to login to the console so let's sign in once signed in you can go to the lambda section for me since i had recently visited it it comes up here if not you can search it inside this box using the lambda key and then it prompts that service so select that make sure you have the correct region selected up on the right so that you can start seeing the function so use this to wherever you deployed it into so since mine was sydney you can see that my first lambda function is appearing up here it shows that it's the runtime is dot net core 3.1 and also says it was last modified five minutes ago so let's select that to navigate into it there are a lot of options and configurations that you can set from this lambda function i am still exploring this and will cover that as part of a separate video for now the only thing that we are interested in is to test and invoke this particular function before we do that let's navigate to monitoring and we can see that there is cloud watch metrics so if we click the view logs in cloud watch button this takes us to the cloud watch that's specific to that particular lambda function since we just did an execution from visual studio that record comes up here i had earlier used a similar function name to test for this video which is why these two records are appearing up here if we navigate into this record we can see the same log request that we saw in the visual studio console so this says the request id and it ends it immediately we can do additional logging inside the lambda and that will start coming up here i will be doing a follow-up video on logging practices so we can see more about it in that video for now let's navigate back to the aws lambda and then we can use the test button in here to trigger this so this specifies a json so let's remove these properties and specify the name property and a name let's give rahul again we can create a new template so we can specify a name as well in this case let's specify my data and then click create so this has created a template which i can now use it from the test button so invoking this is going to invoke this function with that particular data so if we expand the details we can see the response back hello rahul if we want to create a new test data we can choose configure test events and specify the new data so let's specify create a new test event specify a name for this let's say my data new and specify the name as let's say rahul new so let's click create which creates a new test data event now we can switch between the two events inside here so let's select the new one that we just created and click test again so this shows up the return message as hello rahul new so you can save as many requests that you want to test inside of here if we scroll down and navigate to the configuration tab you can see that the only thing mentioned here under the runtime settings is that the runtime and also the handler name so how is aws lambda invoking this particular handler using just a string now we saw earlier that this is a fully qualified name using the assembly type the full namespace and the class name and also the function name aws lambda supports multiple languages through the use of runtimes now the runtime provides a language specific environment that runs in an execution environment so in this case we uploaded it using the zip file so this is specified inside a config file which we earlier saw the runtime relays invocation events context information and the request and responses between the function code and the aws runtime so it sits in between the code that we write and also the aws lambda infrastructure so it communicates between these two and allows to execute our code now for net the out of the box supported runtimes are currently.net core 3.1 and also 2.1 now this like i said before it will be updated when the next long term support version of dotnet comes out which the new one is going to dotnet 6. now with this string defined here the dotnet runtime internally uses reflection to load this assembly finds this class name and invokes this function with the parameter that comes in the request it also passes in the lambda context for any additional information that's required if you are interested to learn more there is an aws lambda runtime support library which is available on github so you can look at this to understand more of the internals on how the lambda infrastructure works and how it invokes the lambda code that we have provided i hope this helps you to set up your first aws lambda project get it working locally and also deploy it to an aws infrastructure if you created the aws free tier account make sure not to be running resources for a long time because then it might start charging you so i often delete it immediately after i'm done with it in this video we manually triggered the aws lambda using the visual studio and also the aws console however in real life we would be using external automated triggers to invoke our lambda functions i will cover this in detail in an upcoming video if you like this video please make sure to hit the like button do drop in the comments if you have any feedback or questions i'll make sure to answer them if you want to be notified of future such videos please make sure to hit the subscribe button it really helps me to grow this youtube channel thank you and see you soon
Info
Channel: Rahul Nath
Views: 4,666
Rating: 5 out of 5
Keywords: aws lambda, dotnet core aws lambda example, aws lambda dotnet core, aws lambda dotnet, .net core aws lambda, .net core aws lambda tutorial, .net aws lambda, .net lambda aws, mock lambda test tool, mock lambda test tool c#, aws lambda for .net developer, .net and aws lambda, getting started with aws lambda .net, aws lambda visual studio 2019, visual studio aws lambda project, aws toolkit for visual studio, aws lambda .net tutorial
Id: IHIJFVUQyFY
Channel Id: undefined
Length: 23min 13sec (1393 seconds)
Published: Thu Mar 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.