AWS CodePipeline for Serverless

Uday Reddy
3 min readFeb 25, 2021

Today we are going to discuss how to set up AWS serverless applications from end to end. I have gone through many articles and felt like sharing the knowledge I have gained during this process. It is essential for one to automate these kinds of tasks, as we may need to create many pipelines for different applications, which can save time from redoing the same stuff. We are going to take a sample hello-world application and create a code pipeline using Terraform.

Sample Application

Prerequisites

i) AWS CLI (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)

ii) AWS SAM CLI (https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)

iii) Python3 (https://www.python.org/downloads/)

iv) Terraform (https://www.terraform.io/downloads.html)

Github Code

Please pull the code from the following GitHub repository and run the necessary commands as mentioned in the Readme.MD file.

git repo: https://github.com/udayreddy29/sam-app

The project structure looks like below:

Fig 1 Sam project structure

Let us understand how to build this application. Initially, get into the folder where template.yml exists and run the below build command.

sam build

Whenever we make changes in the yaml files, we need to build the application. The sam build command builds the entire applications and it’s dependencies, copies the entire application source code to folders under .aws-sam/build folder.

The folder structure inside the .aws-sam folder:

Fig 2 build folder structure

Now, it’s time to set up application on AWS. We have to run the below command in order to deploy application on AWS.

sam deploy -g

Follow the on-screen prompts and copy the details you have enter on the command line. Please copy the details mentioned in the below picture.

Fig 3 cloud formation stack details

This command deploys code in the AWS. It copies the build artifacts under .aws-sam folder to Amazon S3 which was created during above command and deploys the application using Amazon Cloud Formation. Cloud Formation is responsible for creating Api Gateway and Lambda functions the application requires.

Once everything is done, please run below command to test if application is working fine.

curl https://<restapiid>.execute-api.us-east-1.amazonaws.com/Prod/hello/

After successfully deploying the application, you can see output like the following:

{"message": "hello world"}

The above discussed steps are only responsible for deploying serverless application, but it is highly recommended to configure a CI/CD pipelines instead of doing it from the terminal.

We have successfully deployed application on AWS. Let’s use AWS CodePipeline to configure CI/CD pipeline for the sam application. Instead of manually creating an entire CI/CD pipelines, let’s use Terraform which is used to automate the task. Moving forward, if we want to create multiple CI/CD pipelines for different application, we can use this and create which would save so much of manual effort.

Please clone the below terraform code and run according to the steps mentioned in the Readme file.

Terraform: https://github.com/udayreddy29/aws-codepipeline-sam.git

The above terraform code is responsible for creating code pipeline on AWS. Once the codepipeline is created, go to AWS dashboard and run it. After making the changes to the application(Sam-app), run the codepipeline. You can review the changes using the below URL.

https://<restapiid>.execute-api.us-east-1.amazonaws.com/Prod/hello/

UpComing Articles:

i) Canary and Blue Green Deployments for SAM Applications.

ii) Articles on Terraform(detailed explanation of tasks)

--

--