AWS SAM template is an abstraction layer, runs on the top of CloudFormation, for creating serverless resources. So, when you run a SAM template to create a Lambda function or an API gateway, it generates a CloudFormation stack behind the scene. The main purpose is ease-of-management of serverless resources in AWS.
SAM Vs. CloudFormation: when to use AWS SAM instead of CloudFormation?
Though there are multiple ways of creating serverless resources like serverless framework, CloudFormation, Terraform, and AWS CDK, the SAM template can be run locally to test your code before deploying it on AWS, which can help to minimize test time and cost as well. Besides, you can use it for rapid prototyping of your serverless applications.
Another benefit of using SAM template is that you can use a list of pre-defined policy templates for Lambda. For instance, if you Lambda requires reading the SQS queue, you can simply attach the SAM policy named “SQSPollerPolicy” in the Template. Thus, you no longer have to write a custom policy for Lambda to call other AWS services.
Prerequisites:
Make sure you have installed AWS CLI and setup the access key. Also, use the link below to setup
AWS SAM CLI installation guide.
You can follow the steps below.
1) create the template file
Create a directory called mysam on your local machine to store the SAM template files.Go to the directory and then create a file for the lambda function.
mylambda.js
Type the handler code in the above file or simply paste the following test code below.
exports.handler = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify('This is a test Lambda function deployed using SAM Template!')
};
callback(null, response);
};
Next, create a SAM template file named template.yml to declare the lambda function:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template test deployment
Globals:
Function:
MemorySize: 128
Runtime: nodejs10.x
Timeout: 10
Resources:
TestFunction:
Type: AWS::Serverless::Function
Properties:
Handler: mylambda.handler
Environment:
Variables:
S3_BUCKET: your-s3-bucket-name
Note: replace the bucket name.
2) package the template
Make sure to replace the “your-s3-bucket” name by your bucket name.
sam package --template-file template.yml --output-template-file sam-template.yml --s3-bucket your-s3-bucket-name
Note: the sam package command will generate a file called sam-template.yml in the root directory of your SAM template, and also a new file with random string will be uploaded to the S3 bucket. The next step will be to deploy the template to AWS
3) deploy the package
To deploy the package rune the below command. remember to replace the bucket name by your own bucket name.
sam deploy --template-file sam-template.yml --stack-name my-sam-stack --capabilities CAPABILITY_IAM
#use the bucket name you used in the previous step
To delete the lambda function deployed by SAM, you need to delete the CloudFormation stack created by the template.
aws cloudformation delete-stack --stack-name my-sam-stack
Ref: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-command-reference.html