serverless-application-model
The AWS Serverless Application Model (AWS SAM) transform is a AWS CloudFormation macro that transforms SAM templates into CloudFormation templates.
Top Related Projects
⚡ Serverless Framework – Effortlessly build apps that auto-scale, incur zero costs when idle, and require minimal maintenance using AWS Lambda and other managed cloud services.
Pulumi - Infrastructure as Code in any programming language 🚀
OpenFaaS - Serverless Functions Made Simple
Kubernetes Native Serverless Framework
Quick Overview
AWS Serverless Application Model (SAM) is an open-source framework for building serverless applications on AWS. It extends AWS CloudFormation to provide a simplified way of defining the Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by your serverless application.
Pros
- Simplifies serverless application development with a declarative syntax
- Provides local testing and debugging capabilities for Lambda functions
- Integrates seamlessly with other AWS services and tools
- Offers built-in best practices for serverless architectures
Cons
- Limited to AWS ecosystem, not suitable for multi-cloud deployments
- Learning curve for developers new to CloudFormation and serverless concepts
- Some advanced configurations may still require direct CloudFormation usage
- Local testing environment may not fully replicate cloud behavior
Code Examples
- Defining a Lambda function:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs14.x
CodeUri: hello-world/
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
- Creating a DynamoDB table:
Resources:
MyTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
- Setting up an API Gateway:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Cors:
AllowMethods: "'GET,POST,OPTIONS'"
AllowHeaders: "'content-type'"
AllowOrigin: "'*'"
Getting Started
-
Install the AWS SAM CLI:
pip install aws-sam-cli
-
Initialize a new SAM project:
sam init
-
Build your application:
sam build
-
Deploy your application:
sam deploy --guided
Competitor Comparisons
⚡ Serverless Framework – Effortlessly build apps that auto-scale, incur zero costs when idle, and require minimal maintenance using AWS Lambda and other managed cloud services.
Pros of Serverless
- Multi-cloud support (AWS, Azure, Google Cloud, etc.)
- Extensive plugin ecosystem for added functionality
- Supports multiple programming languages
Cons of Serverless
- Steeper learning curve for beginners
- More complex configuration for advanced scenarios
- Potentially slower deployments for large projects
Code Comparison
Serverless Application Model (SAM):
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Serverless:
service: my-service
provider:
name: aws
runtime: nodejs14.x
functions:
hello:
handler: handler.hello
Both frameworks use YAML for configuration, but Serverless Framework has a more concise syntax. SAM is specifically designed for AWS, while Serverless Framework supports multiple cloud providers.
Serverless Application Model is tightly integrated with AWS services and provides a simpler setup for AWS-specific deployments. It's ideal for developers already familiar with AWS CloudFormation.
Serverless Framework offers more flexibility and cross-cloud compatibility but may require additional configuration for complex scenarios. It's better suited for projects that need multi-cloud support or extensive customization through plugins.
Pulumi - Infrastructure as Code in any programming language 🚀
Pros of Pulumi
- Supports multiple cloud providers, not limited to AWS
- Uses general-purpose programming languages (Python, TypeScript, Go, etc.) for infrastructure definition
- Offers more flexibility and control over resource creation and management
Cons of Pulumi
- Steeper learning curve for those unfamiliar with programming languages
- Requires more setup and configuration compared to SAM's simplicity
- May be overkill for small, AWS-specific serverless projects
Code Comparison
Serverless Application Model (SAM):
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs14.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Pulumi:
import pulumi
import pulumi_aws as aws
hello_world_function = aws.lambda_.Function("helloWorldFunction",
handler="index.handler",
runtime="nodejs14.x",
code=pulumi.AssetArchive({
".": pulumi.FileArchive("./app")
})
)
api_gateway = aws.apigateway.RestApi("api")
resource = aws.apigateway.Resource("resource",
rest_api=api_gateway.id,
parent_id=api_gateway.root_resource_id,
path_part="hello"
)
method = aws.apigateway.Method("method",
rest_api=api_gateway.id,
resource_id=resource.id,
http_method="GET",
authorization="NONE"
)
OpenFaaS - Serverless Functions Made Simple
Pros of faas
- Platform-agnostic: Can run on any Kubernetes cluster or bare-metal servers
- Supports multiple programming languages and Docker images
- Community-driven with active development and contributions
Cons of faas
- Requires more setup and infrastructure management compared to SAM
- Less integrated with cloud provider services
- Steeper learning curve for developers new to containerization
Code Comparison
SAM template example:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs14.x
OpenFaaS function example:
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
my-function:
lang: node14
handler: ./handler
image: my-function:latest
Both frameworks use YAML for configuration, but SAM is more tightly integrated with AWS services, while OpenFaaS focuses on container-based deployments. SAM simplifies AWS Lambda function creation, whereas OpenFaaS provides more flexibility in terms of deployment targets and supported runtimes.
Kubernetes Native Serverless Framework
Pros of Kubeless
- Platform-agnostic: Works with any Kubernetes cluster, not limited to a specific cloud provider
- Supports multiple programming languages out-of-the-box
- Integrates well with existing Kubernetes ecosystems and tools
Cons of Kubeless
- Less mature and smaller community compared to SAM
- Limited built-in integrations with cloud services
- Requires more manual configuration and setup
Code Comparison
Kubeless function definition:
apiVersion: kubeless.io/v1beta1
kind: Function
metadata:
name: hello
spec:
handler: handler.hello
runtime: python3.7
SAM function definition:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: python3.7
Both frameworks use YAML for configuration, but Kubeless follows Kubernetes-style resource definitions, while SAM uses AWS CloudFormation syntax. Kubeless is more focused on function definitions, while SAM provides a broader set of resources for serverless applications in the AWS ecosystem.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
AWS SAM transform
The AWS Serverless Application Model (AWS SAM) transform is a AWS CloudFormation macro that transforms SAM templates into CloudFormation templates.
To use the SAM transform, add AWS::Serverless-2016-10-31
to the Transform
section of your CloudFormation template.
Benefits of using the SAM transform include:
- Built-in best practices and sane defaults.
- Local testing and debugging with the AWS SAM CLI.
- Extension of the CloudFormation template syntax.
Getting started
Save the following as template.yaml
:
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs18.x
Handler: index.handler
InlineCode: |
exports.handler = async (event) => {
console.log(event);
}
And deploy it with the SAM CLI:
sam sync --stack-name sam-app
The AWS::Serverless::Function
resource will create a AWS Lambda function that logs events it receives.
Under the hood, the template is transformed into the JSON equivalent of the following CloudFormation template:
Resources:
MyFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
exports.handler = async (event) => {
console.log(event);
}
Handler: index.handler
Role: !GetAtt MyFunctionRole.Arn
Runtime: nodejs18.x
Tags:
- Key: lambda:createdBy
Value: SAM
MyFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Tags:
- Key: lambda:createdBy
Value: SAM
For a more thorough introduction, see the this tutorial in the Developer Guide.
Contributing
Setting up development environment
You'll need to have Python 3.8+ installed.
Create a virtual environment:
python3 -m venv .venv
source .venv/bin/activate
Set up dependencies:
make init
Run tests:
make pr
See DEVELOPMENT_GUIDE.md
for further development instructions, and CONTRIBUTING.md
for the contributing guidelines.
Getting help
The best way to interact with the team is through GitHub. You can either create an issue or start a discussion.
You can also join the #samdev
channel on Slack.
Learn more
Workshops and tutorials
- The Complete AWS SAM Workshop
- AWS Serverless Developer Experience Workshop
- Deploying a "Hello, World!" application
- Testing in the cloud using the SAM CLI
Documentation
Top Related Projects
⚡ Serverless Framework – Effortlessly build apps that auto-scale, incur zero costs when idle, and require minimal maintenance using AWS Lambda and other managed cloud services.
Pulumi - Infrastructure as Code in any programming language 🚀
OpenFaaS - Serverless Functions Made Simple
Kubernetes Native Serverless Framework
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot