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

securitywing

Menu
  • About
  • Must Read
      • IIS Performance Boost
      • RFID Security
      • Web App Security Testing
      • How to Secure Home Network
      • Prevent Cross-Site Scripting Attacks
      • Renew Self-Signed Certificates
      • Penetration Testing Tools
      • VPN Concentrator
      • Forensic Investigation Tools
      • Digital Certificates
      • Cloud Security Issues
      • Advanced Evasion Prevention
      • Firewall Types
      • Tips to Prevent Data Exfiltration
      • Classified Info Handling
      • MySQL Security
      • Definition of 7 Types of Malware
      • VOIP Security
      • Why Antivirus Software Fails
      • 15 Network Security Vulnerabilities
      • Web App Security
      • IT Security Standards
      • Types of Virtualization
      • Android Security
      • Digital Signature
      • Advanced Malware Protection
    • Close
  • Consultancy
  • Contact

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:

  • No Related Posts

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

Primary Sidebar

CISSP Sample Test

Take a CISSP Sample Test

CISA IT governance Sample test



Twitter Follow @securitywing

Categories

  • AWS
  • containers
  • Internet Security and Safety
  • IS Audit
  • IT Security Exams
  • Network Security Tips
  • Off Track
  • Telecom
  • Tutorial

Pages

  • About
  • Best IT Security Certification Exam
  • CISA IT governance Sample test
  • CISA Sample Test
  • CISSP Sample Test Online
  • Consultancy
  • Contact

Popular Posts

  • 3 Steps to Install Miniku...
  • How to install a new Goda...
  • 63 Web Application Securi...
  • How to Renew Self-Signed...
  • How to Setup AWS CloudFro...
  • Host Based IDS vs Network...
  • 8 Effective Ways to Impro...
  • Active vs Passive FTP Mod...
  • Top 10 RFID Security Conc...
  • Yahoo Mail Security Setti...

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

antivirus audit AWS backup browser check cisco cloud computer cyber data database encryption firewall home hsrp ids informaiton internet intrusion it kubernetes linux load balancing malware network protection putty risk router security security tips server ssh SSL switch tools virus vpn vulnerability web webserver website windows wordpress

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