AWS SAM

AWS Serverless Application Model.

Introduction

In my earlier blog post Using Serverless Framework - First Impressions, I told experiences regarding the Serverless Framework - an excellent multi-cloud serverless IaC tool. Besides the Serverless Framework I have extensive experience also using Terraform and Pulumi - you might want to read those articles as well.

I started working in a new project and I have been helping domain specialists with certain cloud implementations using the AWS cloud. The team uses AWS Serverless Application Model (SAM) as an IaC tool. In this blog post, I describe my first impressions using SAM - a serverless IaC tool provided by AWS. SAM is not a multi-cloud tool like the Serverless Framework, but if you are working in the AWS cloud then SAM is an excellent choice.

What is AWS SAM?

AWS SAM is a serverless IaC tool provided by AWS. SAM is a domain specific language (DSL, using YAML for defining serverless infrastructures). SAM uses AWS CloudFormation under the hood for deployments - CloudFormation is a general purpose IaC tool provided by AWS. AWS SAM uses a command line tool called SAM CLI for deploying serverless applications to the AWS cloud, and also for developing those applications locally.

Example

I had to maintain an application which had a Javascript/React frontend and a Python/Flask backend application which provided API for the frontend application. The backend application was deployed to the AWS cloud using SAM.

Working with Python and SAM was pretty nice. You can develop a Python application locally, and once you are ready, you can test the application locally using SAM CLI. I had two terminals open, one for building the SAM backend, and one for running the SAM backend locally.

Terminal 1:

sam build

Terminal 2:

aws-vault exec YOUR_AWS_VAULT_PROFILE --no-session -- sam local start-api --env-vars .env_local.json

(I’m using aws-vault to manage my AWS credentials.)

SAM local development uses a Docker image public.ecr.aws/sam/build-python3.9 behind the curtains. You can pass an environment variable file (env_local.json) in which you can define environment variables for your local development.

Then, when you give command sam build in your terminal #1, SAM automatically reflects your backend application changes in the development server.

Using SAM provides nice developer experience when implementing the frontend and backend applications at the same time.

SAM Template

SAM Template is a YAML file which defines your serverless infrastructure. If you have been working with AWS CloudFormation then you are familiar with the SAM template.

SAM Configuration

Deploying your serverless infrastructure for various environments, you can use a samconfig.toml configuration file. In this file you can provide various settings for your environments.

SAM Deployment

The deployment into an environment defined in your samconfig.toml file is as simple as:

aws-vault exec YOUR_AWS_VAULT_PROFILE --no-session -- sam deploy --config-env prod

The deployment creates a CloudFormation Stack into your AWS account. You can use AWS Console to check which resources got created by your SAM deployment.

Comparing SAM with Serverless Framework and Terraform and Pulumi

SAM is a serverless IaC tool provided by AWS. It is not a multi-cloud tool like the Serverless Framework. If you are working in the AWS cloud, then SAM is an excellent choice. If you are working in a multi-cloud environment, then you might want to use the Serverless Framework.

If you are working in the AWS cloud and you want to quickly create rather independent serverless deployment units, AWS SAM is an excellent choice. If you want to create a larger IaC solution, then you might want to use Terraform or Pulumi.

Conclusions

AWS SAM is an excellent IaC tool if you are working in the AWS cloud and you want to have a tool for quickly creating independent serverless deployments to AWS.

The writer is working at a major international IT corporation building cloud infrastructures and implementing applications on top of those infrastructures.

Kari Marttila

Kari Marttila’s Home Page in LinkedIn: https://www.linkedin.com/in/karimarttila/