Definition

What is AWS Serverless Application Model?

AWS Serverless Application Model (AWS SAM) is an open source framework that helps developers create and deploy serverless applications for the AWS cloud. By providing a template specification and a command-line interface (CLI) tool, AWS SAM simplifies and improves the process of defining and building serverless applications.

Benefits of AWS SAM

AWS SAM helps to streamline the software development lifecycle (SDLC) for serverless applications. With its templates and easy provisioning of resources, it enables development teams to save time and quickly move from ideation to production. The built-in CLI tool makes it easier to develop applications and manage them through the deployment and monitoring phases of the SDLC. It also helps teams to easily and efficiently debug and deploy higher-quality applications to production. Teams can also perform real-time testing and debugging in the cloud with the SAM Accelerate feature.

The AWS SAM template enables developers to define and manage infrastructure code. This YAML file includes the AWS SAM template specification that provides a shorthand syntax. This syntax reduces the number of lines of code needed to define the various elements and properties (resources, event source mappings, configurations, permissions, APIs, etc.) of the serverless application. Also, developers can use AWS SAM connectors in the templates to define permissions between AWS resources.

AWS SAM, specifically its CLI, can be used to locally debug AWS Lambda functions written in Node.js, Java, Python and Go programming languages. Also, the framework can be used to build serverless applications that use any runtime supported by AWS Lambda.

AWS Lambda use cases diagram
Admins can use AWS SAM framework to build serverless applications that use runtimes supported by AWS Lambda.

How AWS SAM works

AWS SAM is open sourced under the Apache 2.0 license. It includes two primary components: AWS SAM templates (including a template specification) and the AWS SAM CLI. The templates are an extension of AWS CloudFormation templates, designed specifically for serverless applications. A developer can use a SAM template to define commonly used resources for a serverless app, such as APIs  functions and database tables. Furthermore, templates can be employed as a single stack, rather than having to manually deploy and manage them separately. The templates can also be deployed to CloudFormation if teams require infrastructure as code (IaC) support on AWS.

The CLI is a development tool that teams can use to build, package and deploy their applications. It is very useful to initialize new projects, perform local testing and debugging, deploy the application, configure CI/CD pipelines, monitor serverless applications in the cloud and to sync local changes to the cloud as development proceeds. The AWS SAM CLI can be installed on Linux, Mac or Windows using pip, the standard package manager for Python.

During deployment, AWS SAM expands its "shorthand" syntax (which is used to express functions, APIs, databases and event source mappings with fewer lines of code) into AWS CloudFormation syntax. CloudFormation then provisions the required resources and ensures reliable application deployment.

Two easy-to-use commands can be used to prepare the application for deployment to the AWS Cloud: sam build and sam deploy. The SAM CLI also provides other commands, including the following:

  • sam sync -- watch to automatically sync the application and its local changes to the cloud for development and testing.
  • sam package to bundle the application code and dependencies into a deployment package.
  • sam pipeline init--bootstrap to create or modify pipelines to deploy for a CI/CD system.
  • sam remote invoke to invoke and test AWS Lambda functions in the AWS cloud.
  • sam logs to fetch, tail and filter logs for Lambda functions.
  • sam list to view deployed resources.

To get started with AWS SAM and its templates and CLI, developers must first carry out two steps:

  • Install the CLI.
  • Use the sam init command to generate a preconfigured AWS SAM template with sample application code.
Common serverless use cases diagram
The open source AWS SAM framework helps users develop and deploy serverless applications for the AWS cloud.

AWS SAM templates and AWS CloudFormation templates

An AWS SAM template represents the architecture of a serverless application. To speed up development and deployment, AWS advises developers to declare the AWS resources that will comprise the application within the template. These templates closely follow the format of AWS CloudFormation templates with most sections in the AWS SAM templates corresponding to the AWS CloudFormation template file sections of the same name. For these reasons, any resource that is declared in the latter can also be declared in the former.

There are, however, several differences between AWS SAM templates and AWS CloudFormation templates. AWS SAM template files require the transform declaration (Transform section), while CloudFormation templates do not. Also, AWS SAM templates include a unique and optional Globals section that defines the properties common to an application's functions and APIs. In addition, the Resources section in AWS SAM templates can contain both AWS CloudFormation resources and AWS SAM resources.

Some SAM CLI commands are equivalent to CloudFormation commands, such as sam package in SAM and aws cloudformation package in CloudFormation. However, the SAM CLI has some unique features, including template validation and local testing.

Pros and cons of AWS CloudFormation diagram
AWS Serverless Application Model templates are an extension of AWS CloudFormation templates.

AWS SAM project

An AWS SAM project contains all the files and folders that are created when a developer runs the sam init command. One of the files is the AWS SAM template which is required to define the AWS resources and infrastructure code using a shorthand syntax for the serverless application being built. The AWS SAM CLI is used with the AWS SAM project to run the commands needed to build and run the application.

AWS SAM transforms lines of code into the AWS CloudFormation syntax required to generate the required resources in AWS. The transformed AWS CloudFormation template generally contains more lines of code than the AWS SAM template, but these are required to provision the application.

AWS SAM features

AWS SAM integrates with a collection of native Amazon cloud services, including Cloud9 IDE, AWS CodeBuild, AWS CodeDeploy and AWS CodePipeline. These tools work in conjunction with SAM and CloudFormation to build and run Lambda functions in AWS.

In addition, SAM lets developers share configurations between resources ahead of deployment, as a single, versioned entity.

A developer can use the AWS SAM Local feature to build and test Lambda functions in an offline environment. This can expedite testing, as code changes can take several minutes on AWS.

With SAM Local, a command runs a Docker container and simulates an API Gateway and Lambda environment in a developer's local system. The developer can then identify any potential code issues and fix them locally before the serverless application is updated on the AWS cloud.

Developers want to focus more on building apps, and with serverless computing cloud vendors can take care of back-end tasks. Explore the top benefits and disadvantages of serverless computing.

This was last updated in August 2024

Continue Reading About What is AWS Serverless Application Model?

Dig Deeper on AWS cloud development

App Architecture
Cloud Computing
Software Quality
ITOperations
Close