DEV Community

Cover image for Building Serverless with SAM

Building Serverless with SAM

AWS Serverless Application Model (SAM) is a framework for developing and deploying Serverless applications on AWS.

Key takeaways from the blog

Understanding SAM

Understanding SAM CLI
AWS Serverless Application Model (SAM) is an open-source framework which consists of -

SAM helps developers by providing different bootstrapped starting points when creating a new SAM application.
SAM templates for quick start

SAM CLI supports various programming runtimes as a choice for developers to get started with. The recent update being the NodeJS 16.x.
SAM supported runtimes

SAM templates

This is an abstracted version of AWS CloudFormation templates which promotes Infrastructure as Code (IaC) practice for your serverless applications and uses AWS CloudFormation under-the-hood for all AWS resources provisioning.
SAM Transforms the template into AWS CloudFormation template so that it could be later deployed by creating a new CloudFormation stack or updating the existing CloudFormation stack.

SAM template also makes it easier to defined different AWS Serverless services with pre-defined resource types -

  • AWS::Serverless::Function
  • AWS::Serverless::LayerVersion
  • AWS::Serverless::Api
  • AWS::Serverless::HttpApi
  • AWS::Serverless::SimpleTable an equivalent of AWS::DynamoDB::Table
  • AWS::Serverless::Application
  • AWS::Serverless::StateMachine.

SAM template uses various pre-managed AWS IAM policies for authorization to various resources. One of the managed policies for DynamoDB CRUD operations.
Sample SAM template
The above sample SAM Template describes a AWS Lambda function and AWS API Gateway REST API with a deployment stage.

SAM CLI

The CLI is the tooling which facilitates developers to get started with Serverless application development and helps developers to build, deploy, test, monitor and also package the artifacts for production ready deployments using CI/CD pipelines.
The SAM CLI has different CLI commands such as -

  • sam init - The init command is used to create a new SAM application, the CLI command helps developers with an interactive questionnaire based walkthrough to setup a SAM app with steps involving choosing from a template, choice of language and type of Lambda function deployment.
    You can find the documentation, here.

  • sam build - The build CLI command is used to build your SAM workstation with the template and validating it against the right resources. This builds the artifacts in .aws-sam/build directory which can be used to deploy into an AWS Account.
    You can find the documentation, here.

  • sam deploy - The deploy CLI command is used to deploy the built artifacts to an AWS Account. This under-the-hood uses AWS CloudFormation to either deploy the resources with a CreateStack operation or UpdateStack operation. The sam deploy has different flags sam deploy --guided or sam deploy --changeset for either walking through the deploy with interactive questionnaire or deploying the changeset alone.
    You can find the documentation, here.

  • sam local generate-event - Whenever working with AWS Lambda functions locally, you would need to generate different events to test the AWS Lambda function when the Lambda fn is getting invoked from a specific event source such as - Amazon S3 operations, AWS API Gateway, AWS SNS, AWS SQS and many more.
    You can find the documentation, here.

  • sam local invoke - The local invoke command invokes the specific Lambda function with an event and exits. This helps in testing Lambda function logic against the simulated event.
    You can find the documentation, here.

  • sam package - The package CLI command creates a zip file of the code and dependences and uploads to the designated AWS S3 build artifacts bucket. This is internally performed by sam deploy.
    You can find the documentation, here

  • sam pipeline bootstrap - SAM helps in a CLI interactive way to setup CI/CD pipelines with SAM Pipelines. This setups different staging environments and the also CLI generates the needed CI/CD workflow template in the project directory based on the CI/CD provider.
    You can find the documentation, here

  • sam sync - The SAM Accelerate feature is used for immediate code deploys to development AWS Account and also real-time monitoring.
    You can find the documentation, here.

  • sam logs - The logs command is used to fetch the CloudWatch logs generated by AWS Lambda functions.
    You can find the documentation, here.

  • sam delete - If you want to delete your SAM application you can delete it with this command, which uses a CloudFormation DeleteStack operation under-the-hood.

Wrapping-up

SAM helps building Serverless apps easily and for developers who are familiar with AWS CloudFormation, it's even more easier with the SAM template, even otherwise SAM init facilities you to get started with one of the templates and build on top of it and also expand it with different SAM CLI commands.
You can refer to my session on Building Serverless Apps with IaC at AWS UG Colombo April meet-up where I demonstrate how we can use IaC with AWS SAM.

You can also refer to my AWS Summit India 2022 session about Building Serverless Apps with SAM Accelerate and SAM Pipelines slide deck.

Top comments (0)