DEV Community

Cover image for AWESOME SAM
Yogesh Sharma
Yogesh Sharma

Posted on

AWESOME SAM

The AWS Serverless Application Model (SAM) is an open-source framework for constructing serverless applications. It presents shorthand syntax to explicit functions, APIs, databases, and occasion supply mappings.

SAM builds upon CloudFormation carrier and CloudFormation Templates. So, it is an extension of AWS CloudFormation.
SAM ONLY supports serverless services. If one want to create other resources, use CloudFormation in SAM template!

For installation- Follow install SAM instructions and SAM Git Repo

SAM Flow

SAM Application
Let's follow the popular SAM example to create a Python Hello world Lambda function.
SAM Application

The SAM template
SAM Template
As an extension of the Cloudformation templates, SAM templates will follow the same format. This means that we are able to use in the SAM templates the same references/structure that we use on Cloudformation. At the beginning of the above template file, the key difference with normal CloudFormation templates:

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Enter fullscreen mode Exit fullscreen mode

The Transform property tells CloudFormation how to handle the template, and allows you to use the SAM resource types.

Build and Deploy
Just run sam build
This compiles your source code and builds any dependencies that you have in the application. It then moves all the files into the .aws-sam/build folder so that they are ready to be packaged and deployed. It also updates the template.yml file accordingly.

Test Application Locally
The SAM CLI provides the sam local command to run your application locally. This internally uses Docker to simulate the execution environment of Lambda.

sam local invoke "Hello" -e event.json

Here you are invoking the function Hello using as the event source the mocked event.json file.

Other Resources SAM Types
AWS::Serverless::HttpApi — creates an HTTP API instead of a REST API (check here the diferences between both).
AWS::Serverless::LayerVersion — creates a Lambda LayerVersion with library or runtime code that is required by the lambda function.
AWS::Serverless::SimpleTable — creates a DynamoDB table that just covers the simple use case of a table with a single attribute primary key.
AWS::Serverless::StateMachine — creates an AWS Step Functions state machine to form complex workflows.

Top comments (0)