AWS Solutions Architect Associate Certification

Services Compute

Serverless Application Model

In this lesson, we explore the Serverless Application Model (SAM) and its pivotal role in simplifying the deployment and management of serverless applications on AWS. By leveraging SAM, developers can streamline infrastructure management and focus solely on building efficient, scalable applications.

What is the Serverless Application Model?

SAM is a toolkit designed to enhance the developer experience when building and running serverless applications on AWS. Similar to an architect creating a detailed blueprint for a house, SAM provides a comprehensive plan for your serverless infrastructure. This approach means you don't need to focus on every individual resource—instead, you work from a well-defined template that outlines how everything fits together.

The image illustrates a comparison between building a house and deploying a serverless application on AWS cloud, highlighting the roles of a builder and a developer.

In this analogy, the finished house represents your serverless application. Constructing a house requires the coordinated effort of various components, and similarly, building a serverless application involves integrating multiple AWS services such as Lambda functions, databases, and API Gateways. SAM acts as the blueprint for your application, detailing how to deploy both the infrastructure and your application code seamlessly.

The Role of SAM in Application Development

As a developer or architect, you define a SAM template—typically written in YAML (or JSON)—which specifies all the serverless resources required for your application. This includes:

  • AWS Lambda functions
  • API Gateways
  • Databases
  • And other essential services

The SAM CLI is a powerful command-line tool that assists in building, testing, and deploying serverless applications. It automates many of the traditionally complex processes in application deployment.

Note

The SAM CLI workflow begins with a configuration file similar to an AWS CloudFormation template. Once your template is defined, it is packaged along with your application code and uploaded to an S3 bucket. AWS CloudFormation then utilizes this template to swiftly deploy the defined resources.

Key SAM CLI Commands

The SAM CLI offers a suite of commands to enhance the serverless development cycle. Below is an example workflow:

sam init          # Initialize a new serverless application
sam build         # Build the deployment package for your application
sam local invoke  # Invoke a local Lambda function for testing (invokes once and quits)
sam package       # Package the application for deployment
sam deploy        # Deploy the application to AWS

Additional useful commands include:

  • sam logs: Retrieve logs for your Lambda function.
  • sam validate: Check your SAM template for errors.
  • sam publish: Share your application via the AWS Serverless Application Repository.

SAM and AWS CloudFormation

SAM is an extension of AWS CloudFormation and follows a similar template-based deployment process. When you create a SAM template, you describe the entirety of your serverless application, including functions, APIs, and other resources. Once packaged and uploaded to an S3 bucket, AWS CloudFormation deploys the resources as defined in your template. This integration not only sets up your infrastructure but also generates a change set for managing updates efficiently.

The image illustrates a Serverless Application Model workflow, showing the flow from a SAM Template (YAML) to an application, then to an S3 Bucket, and finally to CloudFormation.

Tip

Leveraging SAM and its CLI can significantly streamline the creation and management of complex serverless architectures, resulting in an organized and efficient deployment process.

Happy coding!

Watch Video

Watch video content

Previous
Step Functions Demo