AWS SAM Pipelines: Effortless Serverless Application Deployments

In this quickstart guide, we'll delve into the technical aspects of AWS SAM Pipelines and explore how they can simplify setting up your CI/CD workflows.

Page content

Welcome to the world of AWS SAM Pipelines, where the deployment of serverless applications becomes a seamless process. In this quickstart guide, we’ll delve into the technical aspects of AWS SAM Pipelines and explore how they can simplify setting up your CI/CD workflows. Get ready to master the art of serverless application deployments with AWS SAM Pipelines.

Image

Introducing AWS SAM Pipelines

AWS SAM Pipelines provide a powerful framework for automating your serverless applications’ continuous integration, testing, and deployment (CI/CD). Leveraging your favourite toolsets such as Jenkins, AWS CodePipeline or Github actions and integrating seamlessly with AWS SAM, these pipelines enable you to define end-to-end workflows that handle everything from source code management to production deployments.

With AWS SAM Pipelines, you can focus on crafting your serverless applications while the pipeline takes care of the rest. A simple push to your repo with your latest code change or a merge of a pull request and your latest feature is built, tested and deployed in no time.

Benefits of AWS SAM Pipelines

AWS SAM Pipeline provide many benefits to the serverless developer of today. How much code would you have to write to create your Well-Architected CI/CD Pipeline in your favourite IaC declarative language? This is how AWS SAM simplifies this process:

  1. Streamlined CI/CD Processes - AWS SAM Pipelines automate the entire CI/CD pipeline for your serverless applications. You can define various stages such as source code retrieval, building, testing, and deployment, ensuring consistent and reliable releases with minimal manual intervention.

  2. Infrastructure as Code (IaC) Approach - SAM Pipelines embrace the principles of Infrastructure as Code by utilising AWS CloudFormation to define the pipeline infrastructure in a single file. This allows you to version control and manage your pipeline alongside your application code, facilitating reproducibility and scalability. Your pipeline also becomes managed by its own definition as well, so when you want to change your testing regime, it’ll also automatically update from your source updates. (Some would call this inception!)

  3. Tight Integration with AWS SAM - AWS SAM Pipelines seamlessly integrate with AWS SAM, empowering you to leverage the full potential of SAM templates for deploying and managing your serverless applications. This tight coupling ensures consistent configurations and simplifies the deployment process.

The Anatomy of an AWS SAM Pipeline

An AWS SAM Pipeline consists of several key components:

  • Source Stage - Retrieves the application source code from a version control repository such as AWS CodeCommit or GitHub.
  • Build Stage - Compiles and builds the serverless application, generating the necessary artefacts for deployment.
  • Test Stage - Executes automated tests to validate the functionality and integrity of the application.
  • Deploy Stage - Orchestrates the deployment of the serverless application using AWS SAM and AWS CloudFormation.
  • Manual Approval Stage - AWS SAM Pipelines can optionally provide a manual approval step for controlled deployments in production * environments.
  • Pipeline Execution Role - Defines the permissions and access required by the pipeline stages to interact with AWS services.

Getting Started with an AWS SAM Pipeline

To get started with AWS SAM pipelines, we call this bootstrapping. This prepares your AWS account with resources to support the relevant CI/CD Pipelines. To bootstrap an AWS SAM Pipeline, you can use the sam pipeline bootstrap command provided by the AWS SAM CLI.

After which we’ll then prepare our application to be deployed by the pipeline using sam pipeline init.

If you’re just getting started, you can do both of these using sam pipeline init –bootstrap. This will guide you through your stage setup and pipeline deployment.

Follow these steps to get started:

  1. Install the AWS SAM CLI. (If you hadn’t already!) https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html

  2. Ensure your aws cli is configured with valid credentials to connect to your AWS account. https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

  3. Let’s start building the Pipeline using the sam pipeline bootstrap command.

  • You will be asked for;
  • Your stage definitions (dev, staging, production)
  • Location of AWS Credentials details.
  • The region you would like to deploy to.
  • Your preferred method of authentication for your pipeline (Recommended OIDC)
  • Your preferred provider (Github Actions in this case)
  • Repo username or organization
  • Repo name and branch
  • The IAM roles that are required for building resources. If you do not have this defined it will create them on your behalf.
  • Artefact bucket and storage specifications

This will deploy resources in your account as in the screenshots below. In our screenshotted demo I’ve chosen some sensible defaults;

  • 2 stages, dev and prod
  • Deploy to ap-southeast-2
  • AWS CodePipeline as my CI/CD system

Image Image

You will be asked to enter at least 2 lots of stage information, don’t be shocked at the repetition here. This allows you to separate your development resources from your production resources. You could separate them by stack or even account if you select a different set of credentials from your AWS CLI profile.

In this example, I am just demonstrating two separate stacks.

N.B. No prizes for spotting my deliberate mistake in my screenshot

Image

This is very much a case of following the prompts, reading them carefully and understanding what they are asking you to do. As you’ll see there are other options for other pipelines and deployment methods, such as containers and/or Jenkins.

Image

Image

Image

Image

Image

( For those that are less visual and more just like to see the output, see here! )

https://github.com/dbla-tech/demo-hello-world/blob/a4ba0c824b20ab18fe58e1f1bdb9347766ab2b7a/docs/BootstrapOutput.md

As you can see, AWS SAM has created a SAM Template to help deploy the pipelines and supporting resources. You’ll also note in your CloudFormation console that SAM also took the liberty of setting up the required IAM Roles and Artefact buckets.

Image

  1. At this point, you can customize the Pipeline Template to define stages, actions, and configurations.

  2. Finally, you can now deploy the Pipeline using the sam deploy command, referencing the generated codepipeline.yml template.

sam deploy -t codepipeline.yaml --stack-name codepipeline.yaml --capabilities=CAPABILITY_IAM --region ap-southeast-2

Image Image

And that’s it. Our deployment pipeline is deployed and ready to go!

Here are a couple of gotchas that may mean your pipeline won’t work out of the box.

  • Ensure you upload your code to your repo after the pipeline is deployed.
  • Check the repo connection for the pipeline, it may not have authenticated with your chosen repo provider.

In this week’s post, we explored the technical details of AWS SAM Pipelines and their ability to streamline the deployment of serverless applications. By automating CI/CD workflows and integrating with AWS SAM and AWS CloudFormation, AWS SAM Pipelines provide an efficient and scalable approach to managing your serverless deployments. Now, armed with the knowledge and instructions for bootstrapping an AWS SAM Pipeline, you’re ready to enhance your serverless application deployments with ease. Happy coding and deploying!

Sources