AWS CI/CD Pipeline to Deploy a Serverless Framework Project
Tags: aws, cicd, serverless, devops
Published: 2023-12-25 04:49:45

#####Introduction
In this article, we will set up an AWS CI/CD Pipeline to Deploy a Serverless Framework project using AWS CodeCommit, AWS CodeBuild, and AWS CodePipeline.

We will automate the release process so once developers commit code and push changes to a branch it will auto-trigger the AWS CI/CD pipeline which will then build & deploy the project changes to a specific stage.

While using AWS CI/CD services you don’t need to worry about creating custom build servers manually, that of course saves your engineering team time and money. There is no maintenance cost as you only pay when it’s running. Furthermore, there is no need to worry about scaling as it is easily configurable, secure, and highly available.

#####AWS Architecture:
![AWS Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vh1jl7fvdyem19gps7be.png)
Above is an AWS CI/CD Architecture Diagram showing how AWS CodePipeline, AWS CodeCommit, and AWS CodeBuild are leveraged to deploy Serverless Framework project code changes automatically when they are committed.

#####What is a CI/CD?
CI/CD is the combined practice of continuous integration (CI) and continuous delivery (CD). [[1]](https://en.wikipedia.org/wiki/CI/CD)

CI/CD bridges the gaps between development and operation activities by enforcing automation in the building, testing, and deployment of applications. The aim is to increase early defect detection, increase productivity, and faster release cycles.

AWS provides a range of Developer Tools Services. AWS CodePipeline, AWS CodeCommit, AWS CodeBuild, and AWS CodeDeploy services are used to build a full CI/CD flow.

#####AWS CodePipeline
AWS CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates. CodePipeline automates the build, test, and deploy phases of your release process every time there is a code change, based on the release model you define. [[2]](https://aws.amazon.com/codepipeline/)

#####AWS CodeCommit
AWS CodeCommit is a secure, highly scalable, managed source control service that hosts private Git repositories. CodeCommit eliminates the need for you to manage your own source control system or worry about scaling its infrastructure. It supports the standard functionality of Git, so it works seamlessly with your existing Git-based tools. [[3]](https://aws.amazon.com/codecommit/)

#####AWS CodeBuild
AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy. With CodeBuild, you don’t need to provision, manage, and scale your own build servers. CodeBuild scales continuously and processes multiple builds concurrently, so your builds are not left waiting in a queue. [[4]](https://aws.amazon.com/codebuild/)

#####AWS CodeDeploy
AWS CodeDeploy is a fully managed deployment service that automates software deployments to a variety of computing services such as Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises servers. AWS CodeDeploy makes it easier for you to rapidly release new features, helps you avoid downtime during application deployment, and handles the complexity of updating your applications. [[5]](https://aws.amazon.com/codedeploy/)

Implement Basic Serverless Framework Project with Git initialized in local
![git init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qi3szq2f87rkrbukdoav.png)

serverless.yml
![serverless iac code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmlyjgee6x2j6ivzguk5.png)

handler.js
![handler code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/caki6rvv3gprqyyww0e1.png)

.gitignore

![gitignore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/al2u19j0csqt814ayo90.png)

Generate - package.json by running npm init

![npm init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c90mup1l8ykodrn2lcfj.png)


buildspec.yml
This buildspec.yml will be used in the AWS CodeBuild step for installing Serverless Framework, relevant dependencies, and deploying the Serverless Framework Project.
![buildspec](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amvoayis0uwtfs0hb38u.png)

#####Deploy Serverless Framework Project Manually without CI/CD

![Deploy Serverless Framework Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lp4qg9m102r3kempmfl.png)

#####Create AWS CodeCommit Repository
Developer Tools → CodeCommit → Repositories → Create Repository

![Create AWS CodeCommit Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gdjihue4vv6eggfe4tt.png)

#####Create IAM User and Generate Git Credentials for AWS CodeCommit
1. IAM → Users → Create User
2. Create IAM User with attached existing AWS IAM policy: AWSCodeCommitPowerUser‍
3. IAM → User Details → Secret Credentials → HTTPS Git credentials for AWS CodeCommit

![Create IAM User](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i66xt41br1cqai8ib4ox.png)

#####In local git repository add remote origin of AWS CodeCommit Repository

![git repository add remote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9vfyzk3wvzjhvgf0dofx.png)

#####Verify pushed code in AWS CodeCommit Repository
Developer Tools → CodeCommit → Repositories → sls-aws-cicd
![Verify pushed code in AWS CodeCommit Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukkmdml0lbzk3ptojoo7.png)

#####Create AWS CodeBuild Project
Developer Tools → CodeBuild → Build projects → Create build project

![Create AWS CodeBuild Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rydjk0i82510jb2o23n0.png)

#####Created new build project: sls-aws-cicd-build

![Created new build project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rajnbpjvmap1l5m8yzwt.png)

**Source provider:** AWS CodeCommit

**Repository:** sls-aws-cicd [Use the same repository which we created]

**Environment variables Name:** STAGE_NAME

**Environment variables Value:** staging


![Created new build project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlharep11wedmi1glrcj.png)

#####Build specifications
Build specifications: Use a buildspec file
Default Buildspec file name: buildspec.yml


![Build specifications](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2p8auyjgfh4fv9c3yme0.png)

Give needful permission to AWS CodeBuild IAM role.

#####Create AWS CodePipeline using already created CodeCommit & CodeBuild
Developer Tools → CodePipeline → Pipelines → Create pipeline

![Create AWS CodePipeline using already created CodeCommit & CodeBuild](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stdvdi58qb1mmy7yii4p.png)

**Created new pipeline:** sls-aws-cicd-staging-pipeline
######Source Stage
**Source provider:** AWS CodeCommit
**Repository name:**  sls-aws-cicd
**Branch name:** master
######Build Stage
**Build provider:** AWS CodeBuild
**Build project name:** sls-aws-cicd-build  [Use the same build project which we created]

######Deploy Stage
We are skipping this stage.

We are using a Serverless Framework deploy command in buildspec.yml as part of the build step for deployment.

![Serverless Framework deploy command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9dairjanpi6n7goaqkir.png)

#####Verify CI/CD Pipeline Flow - On git commit push auto deploy changes
Updated handler.js

![handler](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/394x0rk4kn8phplfd3pm.png)

Commit recent changes and git push same into the master branch


![Commit recent changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tg1bl0sxlfov4v57zq5.png)

Master branch pushed changes Auto Trigger Relevant Deployment Pipeline


![Master branch pushed changes Auto Trigger Relevant Deployment Pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azgkzbjglsn3rvswmrtv.png)

Verify API in Postman - Updated changes auto get reflected in API response


![Verify API in Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hok71ot19k3tfrtu1xsx.png)


#####Conclusion
Utilizing a CI/CD increases early defect detection, increases productivity, and faster release cycles.

AWS CodePipeline, AWS CodeCommit, AWS CodeBuild helps you implement CI/CD automation for Serverless Framework Projects which helps the development team follow standard continuous integration and delivery process.

While using AWS CI/CD Services you don’t need to worry about creating custom build servers manually, which saves your engineering team time and cost because it only charges you when it’s running, you no longer need to worry about scaling, it is easily configurable, secure, and highly available.

#####Sources
[1] https://en.wikipedia.org/wiki/CI/CD

[2] https://aws.amazon.com/codepipeline/

[3] https://aws.amazon.com/codecommit/

[4] https://aws.amazon.com/codebuild/

[5] https://aws.amazon.com/codedeploy/