0% found this document useful (0 votes)
44 views5 pages

AWS SAM Template

AWS SAM is an open-source framework for building serverless applications that extends AWS CloudFormation. SAM templates define serverless resources like Lambda functions and API Gateway APIs in JSON. SAM introduces resource types and policy templates to simplify definitions and includes CLI commands to build, test and deploy applications.

Uploaded by

ARYAN PATEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

AWS SAM Template

AWS SAM is an open-source framework for building serverless applications that extends AWS CloudFormation. SAM templates define serverless resources like Lambda functions and API Gateway APIs in JSON. SAM introduces resource types and policy templates to simplify definitions and includes CLI commands to build, test and deploy applications.

Uploaded by

ARYAN PATEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

AWS SAM Template

Introduction to AWS SAM:

AWS Serverless Application Model (AWS SAM) is an open-source framework for building
serverless applications. It extends AWS CloudFormation to simplify the definition of serverless
applications, including AWS Lambda functions, Amazon API Gateway APIs, and Amazon
DynamoDB tables.

Structure of SAM Templates:

SAM templates are written in JSON and follow a structure similar to AWS CloudFormation
templates. They consist of the following main sections:

AWSTemplateFormatVersion:

Specifies the version of the SAM template.

AWSTemplateFormatVersion: '2010-09-09'
Transform:

Indicates the SAM transformation.

Transform: AWS::Serverless-2016-10-31
Description:

A brief description of the application.

Description: My Serverless Application


Globals:

It contains settings that apply to all functions in the template.

Globals:
Function:
Timeout: 30
Parameters:

Defines input parameters that can be customized during stack creation.


Parameters:
MyParameter:
Type: String
Resources:

Specifies the AWS resources that make up the application.

Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs14.x
Outputs:

Defines output values that can be retrieved after the stack creation.

Outputs:
MyOutput:
Value: !Ref MyFunction
AWS SAM Resource Types
AWS SAM introduces several resource types that simplify the definition of serverless resources:

AWS::Serverless::Function:

Represents an AWS Lambda function.

Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs14.x
AWS::Serverless::Api:

Defines an Amazon API Gateway API.

Resources:
MyApi:
Type: AWS::Serverless::Api
AWS::Serverless::SimpleTable:

Represents an Amazon DynamoDB table.

Resources:
MyTable:
Type: AWS::Serverless::SimpleTable
AWS::Serverless::StateMachine:

Defines an AWS Step Functions state machine.

Resources:
MyStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: s3://my-bucket/state-machine-definition.json
AWS SAM Policy Templates
AWS SAM includes predefined policy templates that simplify the definition of IAM policies:

AWSLambdaBasicExecutionRole:

Provides basic permissions for Lambda execution.

Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Policies: AWSLambdaBasicExecutionRole
AWSLambdaVPCAccessExecutionRole:

Grants Lambda access to a VPC.

Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Policies: AWSLambdaVPCAccessExecutionRole
AWSLambdaSQSQueueExecutionRole:
Allows Lambda to send messages to an SQS queue.

Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Policies: AWSLambdaSQSQueueExecutionRole
SAM CLI Commands
The SAM CLI provides commands for building, testing, and deploying serverless applications.

sam init:

Initializes a new SAM project.

sam init --runtime nodejs14.x


sam build:

Builds the SAM project.

sam build
sam local invoke:

Invokes a Lambda function locally.

sam local invoke MyFunction --event event.json


sam deploy:

Deploys the SAM project.

sam deploy --guided

Conclusion:

AWS SAM simplifies the process of building and deploying serverless applications by extending
AWS CloudFormation with specific constructs tailored for serverless resources. It provides a
more concise and readable way to define serverless infrastructure, making it easier for
developers to manage and deploy their applications on AWS

You might also like