Setup AWS CI/CD for Serverless Deployment

Jay Desai
Level Up Coding
Published in
6 min readMar 1, 2020

--

In this article, we are going to automate the deployment of our serverless applications with AWS CI/CD tools. We would be using three services of AWS: CodeCommit, CodeBuild, CodePipeline.

AWS CodeCommit

AWS code commit is a source control service that allows you to host your git-based repositories. You can also use Github/Github Enterprise with the AWS code commit. In this article, we would be using code commit for hosting our application code

AWS CodeBuild

AWS CodeBuild is a continuous integration service to help to package and optionally deploy our serverless application

AWS CodePipeline

AWS CodePipeline is the Continuous delivery service which helps to automate the deployment and release cycles

Some Pre-requisites:

Initiate and create the serverless project

serverless create --template aws-nodejs --path serverles-cicd

Setting up the project & Local GIT repository

  • Initialize Git inside the serverless-cicd project folder
git init
  • Creating the master and feature branch
git checkout -b featuregit checkout -b master

Feature Branch — This will be the development branch in which the dev code would be pushed and test before the codebase is merged to the master branch

Master Branch — This branch will be used further in Codebuild to build the final package which we will see next

Setting up the CodeCommit — Remote Repositories

Set up the new repository inside Codecommit in AWS. Give it the name as serveless-cicd and create the repository.

Repo Created

Code Commit Permissions

In this example, we would be using the HTTPS Git credentials to connect our remote repository from our local machine. For creating the Git credentials, it is recommended not to use the root user and instead create the non-root user.

Create the IAM user with the Administrator Access permissions and having the programmatic as well as AWS console permissions and log into the AWS console with the same with your non-root user.

After login with the non-root user for your account, create another user which would have permissions for AWSCodeCommitFullAccess

Creating the HTTP Git credentials

Go to the security credentials tab and generate the HTTP GIT credentials for Codecommit. You can download it to your machine and save it and use this later. Store it in a safe location.

As we have already created the serverless project. Let’s write/modify the base code given to us.

In your handler.js file, insert the below code. The code will return the message node in our body object.

handler.js

Insert the below code inside serverless.yml file.

serverless.yml

provider — As we are using AWS cloud, the name has to be AWS

stage — Specify the dev stage

region — Specify the region where your cloud formation stack would be created

memory size — Specify the memory size. If not specified, the default size is 1GB.

timeout — Specify the timeout for the function to run.

Functions —This is the Lambda function can be found in serverless.yml under the functions property.

serverless.yml

Change the origin to the remote repository from your local repository

git remote add origin https://git-codecommit.ap-south-1.amazonaws.com/v1/repos/serverless-cicd (this should be the URL of your repository)

Now commit your code from the local repository and enter the HTTP Git credentials.

git commit -m “first commit ”

Push the code to the remote repository

git push — set-upstream origin featuregit checkout mastergit push — set-upstream origin master

Setting up the CodeBuild

AWS codebuild

Setting up the env variables in CodeBuild

Creating the BuildSpec File

A build spec is a collection of build commands, in a YAML format, that CodeBuild used to build the project.

Insert the below code into buildspec.yml file.

buildspec.yml

Codebuild has four phases — install, pre_build, build, and post_build.

We need to install the serverless command globally on CodeBuild. Build commands will deploy the serverless project in our AWS cloud.

Setting up the CodePipeline

We would be using CodePipeline to automate the above process which would ensure continuous delivery of our project.

As soon as we commit our code to code commit, it will automatically be deployed to the AWS cloud.

AWS code pipeline

CodePipeline would be creating the new service role for us and go to next.

Source Provider, Repository name & Branch Name
build stage

We need to skip the deploy stage as we are using the serverless framework in the code build and we have defined the build-in build spec file.

Since the source and build were successful, we can now check the API Gateway and copy and paste the address in the browser test. Alternatively, we can check the tail logs as well

Build Logs

CodePipeline can also allow you to create some manual approval before you push the code to the production env. We will see this in future articles.

We have initially created the ENV as dev and deployed the build commands to our AWS cloud. We can similarly use the code build for the production env.

For secure storage of the env variables, please visit for more info.

https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html

Testing the production deployment

For testing purposes, please change the codebase and commit the codebase to the CodeCommit. CodePipeline would automatically build the source code, build the cloud formation stack via the serverless framework, and create the Lambda function and the API Gateway for us.

API GATEWAY

api gateway

LAMBDA FUNCTION

lambda function
Testing the get request from the browser

Conclusion

In this article, we learned how to Automate the deployments of Serverless applications with AWS CI/CD tools.

If you liked it please leave some claps to show your support. Also, leave your responses below and reach out to me if you face any issues.

Follow me on Twitter | Check out my LinkedIn | See my GitHub

--

--