Introduction to building AWS Lambda using .NET Core 3.1 (Introduction for .NET developers)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dotnet code central in today's video i'm going to talk about aws lambda so what is aws lambda aws lambda is a serverless compute service provided by aws when we use serverless feature we do not have to worry about managing the infrastructure managing how to scale out or scale down and that is the biggest value add for the serverless functions or any other serverless feature provided by aws in today's video i am going to focus only on lambda but in subsequent videos i am going to talk about other serverless functions like dynamodb sqs sns etc but with any serverless function or any serverless feature it comes with a price it is costlier than using compute like ec2 if the ec2 is optimized properly why is it optimized properly because based on use case lambda might be a better option but building complex application would probably be better suited with docker container running on ec2 and in subsequent videos i am also going to talk about how to use ec2 with ecs but today's focus will be only on aws lambda now where to use aws lambda just like any other feature serverless also has a lot of hypes and that's why it's very important to understand where to use it just like any other technology or programming language everything has its own place and architecturally not everything fits everywhere so we have to be very cautious choosing aws lambda or serverless compute first thing or the most important to keep in mind is that lambda is meant for smaller functions or i call it nanoservice not even a microservice it's a very small unit of compute that you want to execute for example sending an email or sending a text message or running a computation to deliver something but very small function a single responsibility function that's how lambda should be designed second thing is lambda integrates extremely well with other aws serverless services like sqs sns etc and then finally whenever you use lambda the function time should be minimal or the execution time for the function should be minimal since you pay for the compute time used so it doesn't make sense running highly time consuming operations in a lambda and to be honest that's not what it is meant for and next where not to use it as i mentioned earlier we should not use it for continuously running processes and long-running processes so things like let's say you have a application which has a listener to uh maybe a queue but it gets millions of messages a day and it does a lot of processing in those kind of situation lambda is not the right answer the other situation is you have a process which does a mapreduce job gets lot of data creates some aggregations and saves it into a database or a table that is also not a good use case for lambda because it will take a lot of time it's a long running process and why is it so as i mentioned earlier you pay for compute so you might end up paying a lot of money for running this kind of job in lambda compared to taking a ec2 server and running multiple container on that we should not use lambda where we have a very large footprint and then we should not use it for places where we have requirement of high frequency or low latency the reason for that is the functions are loaded only when it is requested and then finally complex business logic with multiple transactions should not be in lambda it is not meant for that now that we discussed let's go and see how to create a new lambda function so right now if you see this is my aws console i do not have any lambda function running so what we are going to do is we are going to create a new lambda and i am going to use aws cli for that so first thing you have to do is after you create an account with aws and you can create account with aws for free it needs a credit card to validate but it does not charge you anything and you can get a lot of feature free for a year but there are features which are always free things like lambda there are like million function calls or something you can make on lambda for a year and things like that there are a lot of features which are always free and that is a very good ground for doing some r d work so as i mentioned first thing you have to do is you have to install aws cli and i have a wscli already installed and after aws cli is installed you have to use aws configure and provide a profile name and then once you do that it is going to ask you for aws key secret so you should provide that once you provide that your pc will be configured to use the aws cli and then the next thing we would need is aws lambda tool and this is for dotnet so you can use dotnet tool install globally now i have already installed it so it is just going to tell me that it's already installed but you should install it and then if it is installed you can update it using the same command but instead of install it will be updated so this will update the tools for me it should be already updated the next thing you can do is you can install the amazon lambda template and for that you need to do is dotnet new install 5.2.0 that's the one i already installed so i'm not going to install again and after the lambda templates are installed you can use the lambda template to create a new lambda function so i'm going to do that so i'm going to use the dotnet cli command prompt because this is the easiest way to do it so i'm just going to use this instead of going and opening visual studio so you can do new and the template name is lambda dot empty function and then name of the function is lambda demo that's the name of the function i'm going to give now in this video i'm going to try out this empty function because the idea here is to just introduce you to lambda function run it in aws and see how it can be deployed with the cli and in the next video i'm going to use other serverless function like dynamodb and sns to integrate with lambda and then in subsequent video i'll use few other features with lambda to conclude the series on lambda now let's open visual studio and open the code here now if i open the folder for lambda demo already there are two folders created internally one is the source and one is for test and i'm going to first open the project from the source and then i'm going to add the test project also into this solution so let me go and add an existing project i'm going to go into test and i'm going to select the lambda test so now both test and the lambda functions are added so let's go through some of the things which are done by the cli so in the dependencies it has added amazon.lambda.com and amazon.lambda.serialization.systemtext.json these two nuget packages are essential and now let's go into the function and let's try to understand what is there so in the function as you can see it uses the lambda serializer which is part of the aws dot lambda dot serialization dot system text json nuget package and it uses the default lambda serialization and as you can see the template already gives a comment that assembly attribute to ensure lambda functions json input is converted into dotnet class so this is important second thing is it creates a class with name function now this class name can be changed and then it creates a function which will be the entry point for lambda now this function name also can be changed but changing the class name and function name needs to be incorporated somewhere else and i'm going to show it in a few minutes so instead of this function by default it comes with an input parameter and this input string can be anything here instead of string you can define an object and the json serialization will take care of that and then you have an eye lambda context and i lambda context is the context of the function and it gives things like logging and other stuff so here you can get the function name version that's fine you can get the identity if identity is integrated the cognitive identity feature of aws and this is something i can discuss in future videos and then it gives a logger which automatically logs into aws cloudwatch and if you are not aware of cloudwatch cloudwatch is the place where all the log goes and then you can create dashboards and alarms on aws cloudwatch as the name suggests is the watch of the cloud pretty intelligent naming i'll say and then you can get what is the limit on the memory and every lambda has a timeout and the remaining time will give what is the remaining time from the timeout and you can see here remaining execution time till the function will be terminated and i'm going to show where the timeout is also available so now this function is very simple function it is just taking the input and changing it to two upper now the most important file here is this aws lambda tool default.json this file is where you see this function handler this is very critical this function handler provides what is the assembly name what is the namespace the class name and the function name and as i mentioned we can change this class name and function name but if we change it it has to reflect here also so let's say we change it to demo function and then we change this to demo function handler if we do that what we have to do is we have to go here and change it from function to demo function and function handler to demo function handler that's something we have to do this one says how much memory will be allocated to this lambda this is the timeout as i mentioned here from the context we can see time remaining it based on this timeout and this is in second the function runtime here is dot net core 3.1 framework is dot net core app 3.1 and this is for release version and here we can provide the region and profile and i'm going to provide both so that i don't have to type during the command and my profile is personal and the region is used east 2 because i am by default in ohio which is u.s is 2 zone and as you know all cloud providers have different regions and they have certain naming convention for example in aws u.s easter is the region where the data center is in ohio similarly us east one is where the data center is in virginia and then it has u.s west one usb s2 then u is one and so on and so forth so for my purpose i'm using uscs2 and that's all and this is just an information i'm not going to change it i'm just going to keep it as is it's not important so here is the basic lambda function now we can use the input here to do a lot of thing and implement some logic but for this video i'm going to create as i mentioned earlier i'm going to create a very simple function and i'm going to keep the default implementation i'm not going to change anything here because idea of this video is to introduce you to lambda and see everything the only thing i'm going to do here is i'm going to just use the context dot logger dot log so that i log something in the cloud watch and i'm going to say got new message and let's print the message also that's about it so this will show you the cloud watch integration from the context.logger so once this is done let me just show you also the test function which comes with this so as you can see the project lambda demo is added as a dependency which is expected because we are testing this and then here it added the lambda code it added the left test utilities and i'm going to explain you why this is critical and it is needed and then it uses the x unit for the testing framework and here it has a test and it is trying to use the demo function test and then this is the text lambda context as i said text lambda context is one which will be passed as a context so you can see here his context here text lambda context will be passed and then we are calling this function we are sending hello world and since we are just doing an upper case we should see the response is equal to hello world so let's run and see what is the output and once we run this test if we go to the text explorer we can see it is working as expected and the test lambda context will automatically provide a null logger so the logger function we used here context.logger is not going to throw any exception so this shows a very basic lambda function created using the cli next thing what we want to do is we want to publish this to aws and test it so for publishing we are going to again use the dotnet cli because we already installed the amazon lambda tools with dot net so we'll say dot net and then next thing we'll say lambda and then we'll use deploy function but before that first i have to go into the function folder so i have to go to lambda demo then source then lambda demo because this is where my function is and then next thing i have to do is dotnet lambda deploy lambda and the function name is lambda demo and then i have already provided profile and region so i don't have to provide this to you otherwise you can provide profile here and similarly you can provide region region here but i don't need this to because i already provided them in the json configuration file so i'm going to execute this and this is i made some mistake it should be deploy function or deploy lambda okay so let's run this and first thing it's going to do is it's going to use dotnet publish and then after it publishes it's going to package the entire publish folder into a zip file and then publish it into lambda now for lambda you can see we need to provide a new iam role now i do not have any role created here and i'm going to create a new role and here going to select just lambda and here i'm going to select aws dynamodb full access because i might use dynamodb in future video and s3 full xs for s3 these two i might be using i'm not going to use any tag but if you're building enterprise application you should always add tag and the tag should say what is the application name and other features so that it's easier to find out in terms of billing how much your application cost and this one i'm just going to say lambda demo this is the name of the role and i'm going to create this rule takes couple of minutes for the role to reflect now i'm going to go here and i'm going to say lambda demo so it did not work yeah it did not work so let me try again it's going to go through the same process it's going to create a zip file ultimately a zip file is what is uploaded so now my lambda demo is shown here i'm going to select one and now it shows the lambda is created so if we go here if we refresh we should see the lambda is created as you can see the lambda is created it's uploaded as a zip file and it's dot net code 3.1 it was added 10 seconds ago the size is 25 kb it's extremely small that's the beauty of dotnet core and now here as you can see i do not have any trigger and this is the handler and now i can go into test and i can test something for test i do know i'm not expecting uh json i'm just expecting a plain string so i'm going to say convert to uppercase and that's my test right and then i'm going to call invoke and if i invoke you can see it's successful and if i expand it convert to uppercase it converted and this is the response and also if you look into the log in the log it says got new message convert to uppercase so that is the message i sent so you can see here how the log gets logged when i log something in logger and this is integrated with the lambda itself here but you can click here to go to cloudwatch to see the log and here is the monitor which is going to show cloud watch metrics now there is no error rate or any such things the only thing you see that zero error rate hundred percent success rate this is the one call we made and here you can see there was one invocation made so that's the cloud watch metrics that we can see here so that's all i wanted to cover today this will just give you a very basic idea of how you can create a lambda function from scratch and deploy into aws in my subsequent videos i am going to cover using lambda to connect to some of the other aws feature like dynamodb or s3 or sqs and sns and how you can write a pure serverless function based application in aws if you like this video please give me a thumbs up if you are new to my channel and if you think you are going to get value out of my channel please subscribe to my channel and thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 10,150
Rating: undefined out of 5
Keywords: aws, lambda, .net core 3.1, aws lambda, aws lambda .net core 3.1, aws lambda .net core, aws serverless .net core 3.1, serverless, aws serverless compute, aws serverless compute .net core 3.1, aws serverless compute .net core
Id: GZ8_anxgpK8
Channel Id: undefined
Length: 19min 11sec (1151 seconds)
Published: Sun Apr 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.