Leveraging AWS SAM Pipelines And GitHub Actions To Automate The Deployment of Serverless Applications (Part 1)

Ogonna Nnamani
4 min readJan 7, 2024

--

What is Serverless Computing?

Serverless computing is a cloud computing execution model in which the cloud provider dynamically allocates and manages backend infrastructure (servers, databases, and networking) on your behalf. This allows you to build and run applications without having to provision or manage servers yourself.

Think of it as renting an apartment versus owning a house. With serverless, you don’t have to worry about buying, maintaining, or repairing the underlying infrastructure; the cloud provider takes care of all that for you. You simply write and deploy your code, and the cloud provider takes care of the rest.

Easy right?

What is AWS SAM?

AWS SAM, which stands for Serverless Application Model, is a toolkit that simplifies the development and deployment of serverless applications on AWS. It uses CloudFormation under the hood to provision these resources.

AWS SAM consists of two parts SAM CLI and SAM templates

  1. AWS SAM CLI: This is a command-line tool that helps developers build, test, and deploy serverless applications on AWS

Some of the common capabilities of SAM CLI include

  • Initializing serverless applications in minutes with AWS-provided infrastructure templates with the sam initcommand.
  • Compiling, building, and packaging Lambda functions with provided runtimes and with custom Makefile workflows for zip and image types of Lambda functions using the sam buildcommand.

2. AWS SAM Templates: SAM templates are a specific way to define serverless applications on AWS using a simplified syntax and additional features that streamline development. They are built on top of AWS CloudFormation.

An example of a SAM CLI template that defines a basic AWS Lambda function with an API Gateway trigger.

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Description: An example AWS SAM template

Resources:
MyServerlessFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs14.x
CodeUri: ./
FunctionName: myServerlessFunction
Events:
MyApi:
Type: Api
Properties:
Path: /my-api
Method: get

Now that we’ve covered the basics of AWS SAM, it’s time to shift our focus to the main event: the automated deployment scenario!

…Now, in a scenario where we want to develop a CI-CD pipeline seamlessly to automate the deployment of a serverless application developed with AWS SAM using GitHub Actions.

How do we go about it?

— Someone, Somewhere

SAM pipelines

In 2021, AWS launched another feature of AWS SAM called SAM pipelines. SAM Pipelines is a feature of the AWS SAM CLI that automates the creation of CI/CD pipelines for serverless applications. 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.

PIPELINE!

SAM pipelines uses two commands

sam pipeline bootstrap and sam pipeline init.

The sam pipeline init command takes us through a guided process and provisions the resources required for the pipeline, such as IAM roles, s3 buckets, and the specification of the appropriate AWS account for each environment.

sam pipeline init

While the sam pipeline bootstrap command creates the pipeline script, for example, in a Jenkins deployment, this command will create a Jenkinsfile using information gathered by the bootstrap command while prompting for a setup.

sam pipeline bootstrap

The commands can be run together using sam pipeline init -bootstrap to take you through a guided process that also creates the script at the end. This is because this would be our first time.

sam pipeline init --bootstrap

We have now also covered the basics of AWS SAM Pipelines. Now there is a second section where we do a demo on automation using SAM pipelines.

Navigate to Part 2 while I get another cup of coffee!

If you enjoyed this article, kindly follow me on LinkedIn

Happy Cloud Computing!!

--

--