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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a serverless application is a combination of lambda functions even sources and other resources that work together to perform tasks it is more than just a lambda function it can include other resources like apis databases and even source mappings the aws serverless application model also called as sam or sam is an open sourced framework that helps us to build serverless applications on the aws cloud in this video let's learn how to build an aws serverless application using the serverless application template as part of the aws toolkit we will see how to deploy one or more lambda functions api gateways and also dynamo databases using aws sam hello everyone my name is rahul and welcome back to this youtube channel if you like the videos on this channel please make sure to hit the subscribe button it really helps me to grow this youtube channel without much delay let's head right into the code and see more about serverless applications in aws i have visual studio opened up here so let's create a new project scroll down and click create a new project and select the aws serverless application template so i have already filtered by aws c-sharp and serverless and also we have installed the aws toolkit as i had shown you in the first video which introduces us with aws lambda check that out here in case you missed it so once we have the toolkit installed we can choose a template so in this case i'll choose the aws serverless application for dotnet core let's click next this prompt us to enter a name so let's create a name for this let's give in my serverless app let's choose the location so i'll leave it in my youtube code repository and let's click create this prompts to select a blueprint which is basically a starter code for this particular serverless application you have many options here and you can choose one of them that actually fits your project needs to start demoing this i will first create one using the empty serverless application blueprint template so let's click that and make sure to click finish this creates the project and opens that up in visual studio the project is created successfully and similar to the aws lambda we have the function.cs the aws lambdatools.defaults.json file and also we have the dependencies which are the amazon lambda api gateway events lambda core and serialization ones the only one extra in this template is the api gateway events because this integrates with api gateway let's see that in a moment we also have a serverless.template file which is basically the sam application template we will look at this in detail we can see this has the functions class and it also defines a get method inside that before we go any further let's understand a bit more about the the serverless.template file the aws serverless application model helps us build serverless applications for aws aws sam consists of two components one the template file which we just saw the serverless.template file and also the aws sam command line interface so this is the cli that helps us to interact with aws and deploy this template file aws sam templates are an extension to cloud formation templates and it adds a few properties and components making it easier to deploy or create these templates so what is cloud formation aws cloud formation is basically infrastructure as code it is a service that helps us model and set up services or resources for our application so that we can spend less time managing them and spend more time building the application itself it makes it easier to create deploy resources onto the aws framework when you're working with aws cloud formation you're working with templates and stacks now a cloud formation template is a json or a yaml formatted file you can save these as any extension like json.yaml.template.txt etc so these templates are the blueprints for creating resources in aws for your application so this represents your infrastructure as code or as a file now this is an example of a json file you can also define this in yaml as shown here when you use cloud formation you're basically managing related resources as a single unit called stack so it's like something like a grouping that groups all your resources that is part of your template file now you can create update delete a collection of resources by basically creating updating or deleting a stack we will see how these are represented in aws in this video if you look at the aws template structure we have different sections inside this template file some of them are required and some of them are not i'll put a link to this document so that you can go through it in detail but on a high level you can see there is a set of globals a description for the template file some metadata parameters mappings conditions resources and outputs now resources is where we define the resources that's required for our application so some of these as i mentioned are optional and some are required so have a look through this document to understand more about the structure and what goes where coming back to our serverless.template file let's expand this to understand a bit more so here we have the template format version it specifies a transform attribute and also a description under that we have the resources section which is basically defining the resources that's part of this application template it also grabs a few of the output values and gives in an api url we will see this once this application gets deployed if we expand the get we have a lot of properties defined inside that as well the type here specifies the type of the resource so in this case this is defined as aws serverless function now this is a short form representation because of the aws sam model which is built on top of the cloud formation structure so it gives additional features and functionalities like this now here in the handler as we had seen before in our lambda function video it specifies the assembly name the namespace and then the name of the method inside that class so here note that even though the file is function.cs the class name is functions which is why this is using functions if we go into the function.cs you can see that the class name is functions coming back to the template file this has the other properties like the runtime the memory size etc and also policies that's assigned to this aws lambda function coming to the events section this defines how this lambda is triggered in my previous video i had used the console and manually invoked it but in this case this is exposing this function as an api gateway with the base path of just the root so this is typically a get method on the api gateway and exposing this function if we go into the function we can see that this is taking in an api gateway request and the context and returning back some data all it's doing in this case is returning a hello serverless message if you want to explore the different properties and the values associated with it for the aws serverless function type you can go to the documentation under the serverless application model and choose the type serverless function so this defines all the properties that can be there and in the yaml format but the properties remain the same so you can read about it and what it does in this particular document link the aws cloud formation designer is a tool that helps us to build this template i will put the link in the description below now if you see here this defines the various resource types and you can click and add them to build out the template i will not go into the details of this tool in this video but i'll cover that as part of a separate video but if we go back and select this template we can copy this into the template section so right on the bottom tab we can choose template and paste in the template that we copied inside here once we refresh it will start showing that this is a get lambda function and the associated properties to it you can also convert this template from json into a yaml format now if you simply select between these it automatically converts that into the yaml format so you could use this tool to do that as well switching back to visual studio how is this template file associated with this whole project so if we go into the aws lambda tools defaults.json which is basically the toolkits configuration file you can see that here there is a template specified the name specified here and that of the file must match if there is a mismatch then when we publish it will throw an error so let's see how we can publish this now normally for an application i would have a build deploy pipeline setup to automatically publish this into different environments but since we are exploring and understanding about the serverless model let's just manually do that to do it from visual studio if you have the toolkit installed you can right click on the project and select the publish to aws lambda option this prompts up a dialog where we can specify a stack name and also an associated s3 bucket let's create a new bucket so let's say my serverless app dash uq and click ok we also need to specify a stack name so let's use the same my serverless app as the stack name as well this s3 bucket is used to upload the packaged application and also the template file and then aws will start deploying the template file from this s3 bucket now that's how cloud formation works internally so it doesn't use it from the local but it uploads it directly to an s3 bucket and uses it from there so let's click publish and this should publish it into the sydney region for my account clicking publish executes the publish command and creates the artifact that needs to be deployed once done it zips them into an archive file and uploads it to the s3 bucket after that we can see the cloud formation is kicked in and it creates these resources as we have requested now since i have the open cloud formation stack view after publish it has automatically opened that in the back so let's close this to view more details now you can see the stack is still creation in progress so if you refresh this you can see that it's now completed and also it lists the list of things and activities that this particular template file has done if you go into resources you can find the resources that's associated with this particular template let's navigate to the aws console to see that in there so switching to my browser let's go to aws.amazon.com let's sign into the console now it's default to sydney the region that we deployed to so let's go into stacks so searching from here and let's search for stacks so navigating to this we can see the my serverless app stack that is created now these two other stacks i tried it to test this video so let's navigate into the my serverless app and we can see the details of that stack if you navigate to resources you see all the resources that's associated with this stack now as you see the stack is a grouping of the resources that is part of this application so we have the get lambda function we have a get role and we also have the serverlessrest api so you can see the type of these resources on the right side clicking on this takes us to that particular resource now we had seen the aws lambda function earlier video and this is typically the same so we have the handler that's specified here and it's using the zip uploaded file it's also specifying the runtime as expected now if we navigate back and go to the api gateway or the serverless rest api we can see the serverless api endpoints for this application now since we specified just a base path url on the get method it's just showing that so if we navigate to the dashboard we can see the url of this particular api if we switch back to visual studio the output would also have given the same url which is coming back here in the aws serverless url so let's click this to navigate to this particular url and it returns us back hello aws serverless which is the message that the aws lambda function returns us now that we have successfully deployed one lambda function let's see how we can add one more to this whole application so switching back to visual studio let's collapse this let's navigate to the functions.cs let's copy and paste the same function i will just update the name let's say we call this get again so let's add again in all of these comments so also say hello again aws serverless so this is going to create a new function inside of this class to use this let's go to the serverless.template file copy this whole get section again and paste it below that since the names have to be different let's name this get again and this function handler needs to map to the get again function inside the same class we can also update the api gateway route so let's put this as slash again now let's save this make sure it builds successfully and we can do the same to right click and publish this application so let's go to the solution explorer right click and say publish to aws lambda so this has all the stack names and the associated bucket saved into this lambda toolsdefault.json so it's automatically populated let's click publish the publish is successful this update is also complete let's switch back to the aws console and refresh this stack we can now see there is a get again lambda function also available here if you go to the api gateway and refresh the resources we can see there is a slash again endpoint as well now if we go to the dashboard and navigate to this url it defaults to hello aws serverless and if we navigate to slash again endpoint it gives back the message hello again aws serverless now these lambda functions could be any of your api endpoints doing a get post update or delete on your application or any other functions that can be triggered by different mechanisms now let's see how we can build out an api like that so let's navigate back to this project close all this and in the solution explorer let's add a new project so let's click add and say new project now similar to before let's create the aws serverless application and use the.net core option in here and click next let's name this my api serverless app and click create now in the blueprint we already have a blueprint that helps us to set up that so let's use this to make it easy so let's use the blog api using dynamodb as the option and click finish so we have the my api serverless app created so let's collapse this away which has the similar files that we saw before so this has the functions.cs which has all the function code and also it has the serverless.template file which has a lot more going in here now we also have the blog.cs class which is a data transfer object that contains a few properties if we navigate to the functions.cs class we can see that it uses the i dynamodb context now if you're new to dynamodb and want to learn more check out the video linked here or in the description below now this instantiates the db context right inside the function and uses that inside these function methods let's collapse this to see the different functions it has so it has a get blocks essence method it has a single get block essing which gets a block by an id an ad block and also a remove block method now if we navigate to the serverless.template we can see the structure similar to before with the certain sections more in this case now earlier we didn't have the parameters section but this also takes in that so it asks whether we should create a new table or not and also the table name etc this is also defining a conditions section which is building out and condition on which the table should be created or not now under resources this defines a get blog a get blog add and remove like we saw all of these are aws serverless functions which means they are lambda functions now the last one which is a block table is an aws dynamodb table so this is creating the dynamo database table inside here it's using the condition to determine whether to create the table or not if you want to see more about the properties and what each of these does you know where to go so go to the documentation under the aws dynamodb column table if we expand each of these get blocks function we can see this has an associated policies so this gives the aws lambda full access now for some reason it doesn't have an access to dynamodb so if we were to deploy this template asus it is failing so let's add in the access policies for dynamodb since i had already looked it up i know this is amazon dynamodb full access to provide full access for dynamodb but you can also search the documentation and find out the other specific roles if you are interested in that so let's make sure to give this in all the policies for these functions so if we go to get blog make sure to expand this add in the policy in here similarly for add and the remove blog as well let's make sure to remove this extra space at the end so that it doesn't fail with that specified let's deploy this application to see how this works so let's navigate to the solution explorer right click the my api serverless app and click publish to iws lambda now this again asks us for the s3 bucket and the stack name let's keep this s3 bucket as the same as we used before so let's select my serverless app youtube and for the stack name let's use my api serverless app and click next since we have defined parameters this provides an additional dialog to choose in the parameter names so you can see the blog table name the read capacity and the write capacity and also the should create table to be true you can switch this to false if you wish to let's keep it to true because we want the table to be created and click publish now this follows a similar process it first executes the publish it zips up the publish folder uploads to s3 and then runs the cloud formation template so this is successfully done so let's close this to see the details in here we can also navigate to the aws console and go to stacks to see more details so let's search for stacks and navigate to stacks here we have the my api serverless app stack created now it's still in progress so let's navigate to that to see the resources this has a lot more resources going on in here because it has a few lambda functions a dynamodb table and api gateways as well so if you want to sort or search you can do that using this bar and use this to sort the values that comes back now if you want to go to the api gateway you can search for api gateway and navigate to that in this case we have a delete get put and a get by id method so there are different methods on this endpoint in this case which points to different aws lambda functions let's navigate back to the stack refresh this to make sure this is complete and start using this api endpoint to see the data actually getting created let's go to the dynamo table and open that up in the console now here in this dynamodb we have the my api serverless app blog table already created this was done by the template file that we just ran if we navigate to the items section this is currently empty so let's start adding an item in this case we need to make a put request and given the blog object to test this function we can either pass in the attributes from here or use a tool like postman so let's open up postman which i have already installed on machine you can get it for free and the link will be in the description below so let's click the plus button to create a new request now since we know this is a put request let's switch that to put let's navigate back to the console and to the dashboard to get the api url so let's click this and copy this url from the browser by default the get all is returning an empty array because we don't have any items inside the dynamodb let's copy this url switch back to postman and use that url in here now we don't need any authorization so let's choose no auth let's switch to body make sure this is raw and also select that this is a json type if you go to the functions.cs and expand the add block we see that the id and create a date time is automatically set by the code so if we navigate into the blog.cs the only properties we need to set is the name and content so let's switch back to postman and specify the name property let's say aws serverless and the content property so let's say this is a test blog now we can make a post request with this so let's click send it successfully returns back an id of the object that is created which is exactly what the function does so if we switch back to functions.cs we can see this is returning an api gateway response and the body has the blog.id now if we navigate to the dynamodb in the console and refresh this we can see the item that we just created the id of this matches the id that was sent back now we successfully created an item into the dynamodb table so if you go back to this url and refresh this this starts getting all the blogs in this dynamodb table if you want a specific blog item you can specify the id that we just copied into here and make a request now this is returning the specific blog item and not an array of blogs similarly if we want to delete the item we can navigate back let's create a new tab request specify that it's a delete request and copy this full url to the specific id and use that in the delete request note in the delete request we didn't have any parameters specified because here we have the id that was coming after slash which is why the route got matched when i specified this id after the prod endpoint however with the delete endpoint it's not like that how this is defined this is defined in the server list or template file under the events section if we navigate to the get blog method we can see the events has the slash id parameter specified in here so this is the route template that gets used however with delete that's not specified so in that case we'll need to explicitly pass in as a query parameter and the function.cs retrieves this id parameter from the query parameters using a specific name so it's hard coded this as the id property so let's pass that in into our url so switch back to postman and make this a slash prod give question mark id is equal to the id that needs to be deleted now this is a delete so let's click send it's written a 200 okay let's switch back to the dynamodb refresh this and that item no longer exist so we have successfully deleted it from the dynamodb table i hope this helps you to understand more about the aws serverless application template and how to build serverless applications in the aws environment in a later video we will see how to automate this sam application template from a devops pipeline and not have to manually deploy this every time to different environments if you like this video please make sure to hit the like button if you have any questions or feedback do let me know in the comment section below as part of this aws series i am building a lot more videos so if you want to be notified of it in the future please make sure to hit the subscribe button thank you and see you soon
Info
Channel: Rahul Nath
Views: 2,775
Rating: undefined out of 5
Keywords: aws serverless, aws serverless .net, .net core serverless, .net core serverless web api, .net core aws lambda, .net core aws dynamodb, dotnet core aws serverless, dotnet core aws lambda example, serverless aws lambda api gateway, serverless aws .net, SERVERLESS APPLICATION on AWS, .net core and serverless for .net, aws toolkit for visual studio, aws serverless application template, serverless application template .net, .net core aws
Id: iLo8svHzD_g
Channel Id: undefined
Length: 25min 42sec (1542 seconds)
Published: Thu Apr 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.