In Part-1 of this series, we covered some basics of AWS SAM which is an extension of AWS CloudFormation service and greatly simplifies managing serverless resources.
We then defined and deployed a lambda function along with API Gateway using an SAM template.
Following diagram shows the state of our serverless infrastructure from the previous post:
Please check the previous post, if you are new to topic as we are building on top of that learning and this way it will be much easier for you to follow the topics in this post.
Today, we’ll resume from previous post and we will add a DynamoDB Table to the solution. This way we’ll have a SAM templates which covers common web application solution layers i.e. Web API Gateway, Logic and Persistence.
Here is the screenshot of SAM template from previous post for your reference:
What We are Building
We’ll be adding a DynamoDB Table to our solution and following picture shows the target serverless infrastructure
We’ve already built API Gateway and Lambda in previous post. In this post, we will cover DynamoDB.
DynamoDB Table
We can use AWS::Serverless::SimpleTable type in SAM template to define DynamoDB table. Following picture shows DynamoDB table section.
As you can see that, we used the type SimpleTable for this purpose. Now there are some default with DynamoDB table, which will be assumed automatically e.g. it will define a partition-key, with name id with string type, by default, if not mentioned explicitly in the SAM template.
Furthermore note the Environment section for Lambda function, where we are passing the table name via Environment Variable.
Environment variables provides a nice and easy way to us to pass configuration data to our code at run-time which makes solution more flexible.
We are also giving AmazonDynamoDBFullAccess to our lambda function. Note, that this policy is giving too much permission to our lambda function, but its ok for our learning purposes. In real production applications, we should limit the permissions.
With these changes to the SAM template, we can now use package and deploy command to deploy changes to AWS:
and here the AWS web console showing created table:
SAM Outputs
I’ve also updated the Outputs section in SAM template with the followings:
and here is the Outputs Tab shown in the AWS web console:
So our SAM template is now capable of creating a basic serverless infrastructure. Next, lets see some other useful tools and resources which will help you with your serverless journey with SAM.
AWS SAM CLI
AWS SAM provides you with a command line tool, the AWS SAM CLI, that makes it easy for you to create and manage serverless applications.
It helps manage serverless application written with SAM and it can be used for local testing, validation or start a new project.
You download the installer from this link and install it.
you can check the installation by running command as shown below:
sam --version
SAM CLI comes with a lot of useful commands e.g. init, build, deploy etc. You can check more information on this official command reference.
Serverless Patterns Collection
AWS provides many great resources to get you started with SAM. This tutorial provides Step-by-step instructions to download, build, and deploy a simple serverless application.
You can also check Serverless Patterns Collection. This is a great resource for learning.
I’ll encourage you to check this collection and you will find a lot of common SAM templates, instructions and links to useful information.
Summary
In this post, we defined and deployed a DynamoDB table to our serverless infrastructure. We also saw how we can utilize Outputs section in SAM.
We also learned that SAM CLI provides many useful commands and Serverless Patterns Collection contains many common examples and templates as a useful resource.
You can download the updated source code and SAM template from this git repository.
In the next post, we will cover more SAM topics and will also build a REST API for common CRUD operations utilizing our serverless infrastructure and SAM.
Let me know if you have some questions or comments. Till next time, Happy Coding!
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.
1 thought on “AWS Serverless Applications using SAM – Part 2”
Comments are closed.