• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

securitywing

3 Easy Steps to Create and Deploy AWS SAM Template

by wing

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.

Serverless Policy Template

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

Related posts:

  1. How to Permanently Auto-Mount EFF Volume to EC2 instance using User-Data
  2. How to Enable AWS Cross Account ECR Image Replication
  3. Top 10 Reasons Why AWS ECS is Better than Kubernetes
  4. What is the Difference Between AWS SDK and Boto3

Filed Under: AWS Tagged With: lambda, sam, serverless

Primary Sidebar

Please help us sharing

Categories

  • AWS
  • Basics
  • Containers
  • Cryptocurrency
  • Cyber
  • Internet Security and Safety
  • IS Audit
  • IT Security Exams
  • Law & Human Rights
  • Network Security Tips
  • Off Track
  • Social Media Governance
  • Tech Comparisons
  • Tech Stack Suitability
  • Telecom
  • Tutorial

CISSP Sample Test

Take a CISSP Sample Test

CISA Sample Test

CISA IT governance Sample test

Please Follow Us

Contact us for Ads

Go to Contact Form

Search

Footer

Copyrights

Protected by Copyscape Duplicate Content Detection Software

Securitywing.com reserves the copyrights of all of its published articles.No contents of this site is permitted to be published to anywhere else in the Internet.If any contents are found in any other websites, securitywing reserves the rights to file a DMCA complaint. But you have the right to use the link of any relevant article of this site to point from your website if you consider that it might improve the quality of your article.

Tags

audit AWS backup basics browser check cisco cloud computer configuration cyber data database email gmail hsrp ids iis informaiton internet kubernetes linux load balancing malware microsoft network protection redundancy risk router security security tips server social media SSL switch test tools vpn vrrp web webserver website windows wordpress

Copyright © 2010-2025 ·All Rights Reserved · SecurityWing.com