Introduction to IaC using SAM and CloudFormation

By | March 25, 2023

AWS Serverless Application Model (SAM) is a framework for building serverless applications on AWS. SAM provides a simplified way of defining the Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by your serverless application. SAM also provides a command-line interface (CLI) for deploying and managing your serverless application on AWS.

In this guide, we will use SAM for serverless applications and AWS CloudFormation for all other deployments using templates for reusable resources.

Prerequisites

Before we get started, you’ll need the following:

  • An AWS account
  • AWS CLI installed on your local machine
  • Basic understanding of AWS Lambda and AWS CloudFormation

Using SAM for Serverless Applications

To get started with SAM, follow these steps:

  1. Create a new SAM application using the sam init command. This will create a new directory with the basic structure of a SAM application.
  2. Define the AWS resources needed for your serverless application in the template.yaml file. This can include AWS Lambda functions, Amazon API Gateway APIs, and Amazon DynamoDB tables.
  3. Write the code for your AWS Lambda functions in the app.py file.
  4. Test your application locally using the sam local command. This will spin up a local environment that mimics AWS Lambda and API Gateway.
  5. Once you’re happy with your application, use the sam deploy command to deploy your application to AWS. This will create the necessary AWS resources defined in your template.yaml file.

Using CloudFormation for All Other Deployments

For deployments that don’t use SAM, we recommend using AWS CloudFormation. CloudFormation provides a way to define infrastructure as code using templates, which can be versioned and reused.

To use CloudFormation for your deployments, follow these steps:

  1. Define the AWS resources needed for your deployment in a CloudFormation template. This can include Amazon S3 buckets, Amazon EC2 instances, and Amazon RDS databases.
  2. Use the aws cloudformation create-stack command to create a new stack based on your CloudFormation template. This will create the necessary AWS resources defined in your template.
  3. Once your stack is created, you can update it using the aws cloudformation update-stack command. This will update the AWS resources in your stack based on any changes made to your CloudFormation template.
  4. You can also delete your stack using the aws cloudformation delete-stack command. This will delete all AWS resources created by your CloudFormation template.

Here’s how to set up your CodeBuild pipeline to use SAM and CloudFormation:

  1. In your CodeBuild project, add the necessary IAM permissions to deploy resources to AWS. This includes permissions for CloudFormation and SAM.
  2. Add a build step to your CodeBuild project that uses SAM to build and package your serverless application. You can use the sam build and sam package commands to do this.
  3. Add a build step to your CodeBuild project that uses CloudFormation to deploy your resources. You can use the aws cloudformation deploy command to do this.
  4. Update your buildspec.yml file to include the SAM and CloudFormation build steps. Here’s an example buildspec.yml file:

phases:
  build:
    commands:
      - sam build
      - sam package --output-template-file packaged.yaml --s3-bucket my-bucket
      - aws cloudformation deploy --template-file packaged.yaml --stack-name my-stack --capabilities CAPABILITY_IAM
  1. Save and commit your buildspec.yml file to your source code repository.
  2. Start a build in CodeBuild to deploy your resources. Once the build is complete, you can view the deployed resources in the AWS Management Console.

By integrating SAM and CloudFormation into your CodeBuild pipeline, you can automate the deployment of your serverless applications and infrastructure using templates for reusable resources. This makes it easy to version and deploy your resources consistently across your AWS account.

Conclusion

Using SAM for serverless applications and CloudFormation for all other deployments using templates for reusable resources is a great way to streamline your deployment process on AWS. With SAM, you can easily define your serverless application resources and deploy them using a simple CLI. With CloudFormation, you can define your infrastructure as code using templates, which can be versioned and reused.