Getting started with AWS SAM

Serverless Application Model

Raywon Kari - Published on June 10, 2021 - 6 min read


Serverless has NOT just been a buzzword but also a paradigm shift in general, when it comes to choosing the kind of cloud infrastructure one should embrace and implement. If you are still not embracing serverless, you are missing out on the fruits it gives. Check out my earlier blog post about serverless to know more, if you haven't already.

As the adoption increases, Cloud providers are creating new tools to even speed up the usage and make it user friendly. As part of that, AWS has created a new framework called SAM i.e., Serverless Application Model. Using this framework, it's as easy as running a few commands and writing a few lines of code and our initial version of a serverless application will be ready.

In order to get started, we need to do the following:

  • Install AWS & SAM CLI
  • Configure AWS CLI
  • Create and deploy an application


The following commands on macOS will install the CLI:

brew tap aws/tap
brew install aws-sam-cli
## Verify
sam --version
SAM CLI, version 1.24.1

For all distributions, the doc can be found here.

Next up is to configure the AWS CLI, follow the instructions here.

What is SAM

SAM is basically a framework, using which we can extend our CloudFormation templates with Serverless resources, such as Lambda functions, API Gateway, Step functions, Dynamodb tables etc. We create SAM templates, in other words CloudFormation templates with SAM framework specific resources, and deploy them using SAM CLI, or regular AWS CLI, or CloudFormation action using CodePipeline.

Using SAM, we get a toml config file which we can use to define certain constants and behaviors, such as app name, changeset logic, parameters etc.

The following resource definitions are currently supported:

  • Serverless::Api
  • Serverless::Application
  • Serverless::Function
  • Serverless::HttpApi
  • Serverless::LayerVersion
  • Serverless::SimpleTable
  • Serverless::StateMachine

Apart from resource definitions, we do have other stuff to do:

  • Transform: AWS::Serverless-2016-10-31 => This transformation is needed in order to make a CloudFormation template SAM compatible.
  • Globals: In this section, we can centralise few settings so they are distributed to all the relevant underlying serverless resources such as runtime for lambda functions.
  • Parameters: We can define parameters and store them alongisde the SAM configuration to overwrite during runtime etc.

The benefits we get with using SAM is that, it does a lot of work under the hood by creating certain dependencies automatically for us, which we would have done it ourselves if we were to use regular CloudFormation templates.

One example is that, we would need an IAM role to deploy a lambda function. In CloudFormation, we would have added an IAM role resource and plugged that in to an AWS::Lambda::Function resource. Using SAM, we don't need to specify that because SAM adds it for us.


Following are a few advantages we get when using SAM:

  • All our serverless resources are packed as one SAM application. This is visible in AWS Console in Lambda service under Applications.
  • It's CloudFormation. Nothing new, nothing old.
  • Local dev. Using SAM, we can test our applications easily from local.
  • Integration with AWS eco-system. So much stuff is ready to use with few lines of code. In some use cases, third party tools are supported.

App Setup

First, we need to create our application locally.

sam init
# Select
# 1 - AWS Quick Start Templates
# 1 - Zip
# 7 - nodejs12.x
# Project name: getting-started-with-aws-sam
# Template selection: Hello world example
# that's it

Check the folder structure:

├── events
│ └── event.json
├── hello-world
│ ├── app.js
│ ├── package.json
│ └── tests
│ ├── integration
│ │ └── test-api-gateway.js
│ └── unit
│ └── test-handler.js
├── samconfig.toml
└── template.yaml
  • template.yaml => cloudformation/SAM template
  • hello-world => source code for the lambda function
  • samconfig.toml => SAM CLI config file


Once the code is ready, now is the time to try deploying it.

sam deploy --guided -t template.yaml --profile raywon
# few questions will be asked
# then the resources will be deployed
# Example output
: '
CloudFormation outputs from deployed stack
Key HelloWorldFunctionIamRole
Description Implicit IAM Role created for Hello World function
Value arn:aws:iam::010503879086:role/getting-started-with-aws-sa-HelloWorldFunctionRole-1B3NDI7M829S2
Key HelloWorldApi
Description API Gateway endpoint URL for Prod stage for Hello World function
Key HelloWorldFunction
Description Hello World Lambda Function ARN
Value arn:aws:lambda:eu-north-1:010503879086:function:getting-started-with-aws-sam-HelloWorldFunction-JYuswyaYSmfn

and Done! Our sample API application is ready. Source code can be found here.

Watch it in practice below:

Few other handy commands:
  • sam build
  • sam validate
  • sam local invoke
  • sam logs
  • sam publish

It's an exercise for you to play around with them :)


We at are building a modern platform with Serverless workloads. This lead to developing tools over a period of time to ease development efforts when it comes to the platform, and also to improve the developer experience.

All the tools are open sourced. Head over to the GH org where we host them: MhLabs

Spoiling the surprises, some handy tools to shout out:

Feel free to play around! Serverless all the way!

Raywon's Blog © 2020 - 2021

Built with Gatsby & React