Deploy a Lambda Function using AWS SAM in 5 Minutes

Deploy a Lambda Function using AWS SAM in 5 Minutes

AWS Serverless Application Model (SAM)

ยท

5 min read

Today, I am going to show you how to quickly get started with AWS Lambda Functions! I will show you how you can use Amazon's Serverless Application Model (SAM) to quickly generate a project and then seamlessly deploy it!

Prerequisite

To use AWS services you must first have an AWS account. Also, you need to install SAM from the command line along with a few other requirements.

๐Ÿฆ Follow me on Twitter to see even more content! ๐Ÿฆ

1. SAM Init

Once you have SAM installed you can get started with our first command!

sam init

Lets run sam init and use the AWS Quick Start Template init-1.png

Next, you can choose between packaging the AWS Lambda as a Zip that will be uploaded to S3 or an image you can push to ECR. For right now, we are going to stick with the Zip file approach. init-2.png

Now you get to choose your own runtime! There is a wide variety and just about any programmer should be able to get started. I am going to choose nodejs12.x for this tutorial. init-3.png

You will subsequently need to create a project name and select a template to generate. There are some very useful options from creating an API endpoint to scheduling an event to be run periodically. We are going to use the From Scratch option init-4.png

Finally, the project is generated! That was almost too easy. A README.md file was generated inside your source files. This includes very useful information that is outside the scope of the article. init-5.png

SAM Test

One of the most frequently asked questions about AWS Lambda's is, "How do you test them?" Now that we have a project initialized you can test it locally! SAM comes with built-in functionality to test your Lamba Functions. There are a number of ways to be able to pass parameters as well. We are going to keep it simple for this demonstration.

sam local invoke

You can see the message that was generated in the starter project. There is also some useful logging to help you determine what costs will be associated with your AWS Lambda. deploy-4.png

3. SAM build & deploy

Finally, we can build and deploy this Lambda to AWS in a single line of code! When you build the project SAM will compile an AWS CloudFormation template, template.yml and a samconfig.toml file encapsulating your settings. There is also a file included telling SAM how to build your Lambda Function named buildspec.yml.

sam build && sam deploy --guided

The project will get built. You can see our npm install occurring. You will subsequently be asked a series of basic questions and you can use all the default values. deploy-1.png

SAM is very smart! Using CloudFormation, it will let you know which resources will be added, removed, or modified. deploy-2.png

You will be asked to confirm the CloudFormation changes. Once you do so your changes will be deployed. This may take several minutes. deploy-3.png

Conclusion

That's it! In as little as 5-10 mins you should have a testable and deployed AWS Lambda Function deployed using AWS SAM! Check your code into source control and treat it like any other part of your software system. Cheers!

๐Ÿฆ Follow me on Twitter to see even more content! ๐Ÿฆ

Cleanup Pro Tips

Find the CloudFormation Stack

aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE

Delete the CloudFormation Stack

aws cloudformation delete-stack --stack-name my-stack

Did you find this article valuable?

Support Phillip Ninan by becoming a sponsor. Any amount is appreciated!