Getting started with AWS SAM SAM Series 1

hi welcome to another majestic dot cloud video my name is Laszlo and I'm going to show you today how to get started with AWS Sam AWS Sam or serverless application model is an open-source framework developed by AWS on top of cloud formation this framework allows us to develop serverless applications run them locally test them emulate an API gateway then it will allow the packaging and deployment of the application to the AWS cloud this framework comes with a command-line tool which you need to have installed I will provide links to the installation documentation in the video description so you can install follow the instructions there and install the the AWS CLI and the AWS Sam CLI on your machine and once you have done that you can really get started with what I'm doing here so let's get started and let me show you how to work with AWS Sam once you've installed the AWS Sam CLI you need to open your terminal window and initialize the project this is done in the following way we issued a command Sam in it the name of the project which in our case will be Sam demo and the run time and in our case the run time will be no js' 8.10 but you can use any other runtime you want once we issued this command it was the project was created we can see it it's in the Sam demo directory let's see what's inside it and you see there are a couple of files and a couple of directories we will look a little bit more in detail on what these directories and files contain I have opened the project in my code editor and here we can see we have the events folder which contains a sample event the events can be specified to the function when you run it and this is like a payload the payload of the function then you have actually the hello world folder in which you can see the app Jes this is the our lambda function this is just a simple function that outputs hello world if you are want to add business logic you would add it here into this this file we also have one important file here the template ml this is a Yamal template and this is where you specify all the components of your servers application and also the the different parameters needed so you can see in this initial template that some generated for us we have defined a function this is the hello world function this is it the type AWS serverless function the properties where is the folder what's the handler of the function was the runtime and it has also an associated event source which in our case is an API endpoint so it generated for us an API and point and this will be deployed as an API Gateway project and attached to our lambda function so whenever we invoke the API endpoint this lambda function which is in the app J app.js file gets executed the path of the API is hello and the method is get we will see this in action in a couple of seconds now let's go back to the command line and let's invoke our function but before invoking it let's build it the build command builds our project fetches the necessary dependencies and then we can invoke it you can see our application is being built it takes a couple of seconds but it's done and now we will issue some local invoke and we specify the event source in our lead case this will be the event Jason in that directory I have shown you and our Sam CLI has fetched the docker container image because all these runs in a docker container you need docker installed to to run the Sam CLI and the function run we got some details like the duration the build duration memory size of the function and the memory used and in the end we have the status code 200 and the body of the a result which is hello world if we would go back here and modify here the message let's just test this out hello majestic dot cloud and if I go back and invoke my function again what can we see here is that I still got hello world that's because I didn't build my code so let's issue Sam build or project is being built again and let's invoke again the lambda function locally okay and now you can see the result is hello majestic God cloud now let's test an API gateway emulation locally you would do this the following way some local start API and what happens is the Sam CLI starts an API endpoint for us on this address so in on the local host and we can we now can invoke this API endpoint with requests and I will do that from another window here and I will do this with Co URL color and this is a get request and I need to type my API and point name and the port and also the path which I have specified in the template Yama and what happens now the function was invoked and I got back a low majestic cloud once we deploy this application on the AWS cloud invoke it a little bit more elegantly with postman but we will see that bit later okay so let's go back here and stop the API clear the screen and let's start the deployment of our application first we need to create an s3 bucket so let's create a bucket because the package command needs needs a bucket where the output will be packaged so let's create a bucket we'll use AWS s3 make bucket and the bucket name will be let's call it ma just tick Sam I hope this is not taken ok it created it now we can package the application we issue some package and the output template name will be package it llamo you can put here any template name you want then we should we specify the bucket name we just created so majestic Sam and now our application is being packaged this will take a bit of time but it was pretty quick pretty quick this time because it's not a very big project now we can deploy it before deploying it let me show you around the AWS console I'm here in the cloud formation console and you can see that I have known stacks in this AWS region let's go over to API gateway so you can see that in API gateway I also have known API is defined so in the command line we will now issue the deploy command this is done with Sam deploy we need to specify the template file and this needs to be the same as you specified earlier in the Sam packaged command we will specify the region if you don't specify this it will take the default region you have configured in the CLI in my case it will be US East one we also need to specify the capabilities capability I am this is needed because our function needs to create permissions for the function so this is done with capabilities capability I am and then we need to specify the stack name which will be in our case Sam demo majestic and now if I run this command it should deploy the application to AWS so it invokes cloud formation and cloud formation we start processing the template Yama file and create all the resources for us that we need for this function to run properly let's wait for this cloud formation run to complete and then we will see what it was created actually we can go over to cloud formation and see what happens so if I do a refresh now you can see that I have stack some time on majestic which has the status create in progress all the resources needed for my application are being created right now if we click on the stack name we can see the events that cloud formation does for us so it creates the roles needed for our function to run it crease the function and serverless REST API and you see I have a create complete if I go over to API gate gateway and do a refresh here I should have also an API created for me that's it's here but it doesn't want it didn't want to reload the get started page okay so this is the API I have with the hello but and I have the the get which has an integration request to alum the function ok let's go back to cloud formation and grab you see here we have this hello world API endpoint and we need to grab this so we can test our function I will invoke this API endpoint from Ostman and it should give us the output of the lambda function here this is postman I open a new pan tab I mean and paste the URL I copied from the outputs and this is the get request and if I send the request I get back the message from the lambda function hello majestic dot Cloud so in a few easy steps we created a simple service application we deployed it from the command line and we can start using it of course in the real life you will have a little bit more complex applications but the the path is the same you create a skeleton with some init you modify the function implement your business logic and then proceed on to deploy it you can add the deployment flow into your CI NCD pipeline and then you have full service workflow in the following episodes of this video which will come every week from now until the middle of October we will also go a little bit more in depth and we will also see an example of an automatic deployment complete pipeline with approvals and all that kind of stuff also we will expand a little bit dysfunction and create a small little application which will use Amazon recognition to recognize objects and stuff in images so if you are curious about that please come back in a few weeks and see serverless in action here on majestic dot cloud I hope you like this video if you did please give it a like subscribe to the channel and see you in the next video