Professional Documents
Culture Documents
The following post will walk you through how to build a CI/CD pipeline to automate the
deployment process of your Serverless applications and how to use features like code
promotion, rollbacks, versions, aliases and blue/green deployment.
At the end of this post, you will be able to build a pipeline similar to the following gure:
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 1/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
https://github.com/mlabouardy/lambda-ci
For the sake of simplicity, I wrote a simple Go based Lambda function that calculates the
Fibonacci number:
I implemented also a couple of unit tests for both the Fibonacci recursive and Lambda
handler functions:
To create the function in AWS Lambda and all the necessary AWS services, I used
Terraform. An S3 bucket is needed to store all the deployment packages generated
through the development lifecycle of the Lambda function:
The build server needs to interact with S3 bucket and Lambda functions. Therefore, an
IAM instance role must be created with S3 and Lambda permissions:
Finally, a Go-based Lambda function will be created with the following properties:
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 2/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 3/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
Terraform output
Sign in to AWS Management Console and navigate to Lambda Console, a new function
called “Fibonacci” should be created:
You can test it out, by mocking the input from the “Select a test event” dropdown list:
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 4/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
So far our function is working as expected. However, how can we ensure each changes to
our codebase doesn't break things ? That’s where CI/CD comes into play, the idea is
making all code changes and features go through a complex pipeline before integrating
them to the master branch and deploying it to production.
You need a Jenkins cluster with at least a single worker (with Go preinstalled), you can
follow my previous post for a step by step guide on how to build a Jenkins cluster on
AWS from scratch:
medium.com
Prior to the build, the IAM instance role (created with Terraform) with the write access
to S3 and the update operations to Lambda must be con gured on the Jenkins workers:
Jump back to Jenkins Dashboard and create new multi-branch project and con gure the
GitHub repository where the code source is versioned as follows:
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 5/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
Create a new le called Jenkins le, it de nes a set of steps that will be executed on
Jenkins (This de nition le must be committed to the Lambda function’s code
repository):
Test: check whether our code is well formatted and follows Go best practices and
run unit tests.
Deploy: update the Lambda function’s code with the new artifact.
Note the usage of the git commit ID as a name for the deployment package to give a
meaningful and signi cant name for each release and be able to roll back to a speci c
commit if things go wrong.
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 6/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
Once the pipeline is completed, all stages should be passed, as shown in the next
screenshot:
At the end, Jenkins will update the Lambda function’s code with the update-function-
code command:
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 7/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
If you open the S3 Console, then click on the bucket used by the pipeline, a new
deployment package should be stored with a key name identical to the commit ID:
Finally, to make Jenkins trigger the build when you push to the code repository, click on
“Settings” from your GitHub repository, then create a new webhook from “Webhooks”,
and ll it in with a URL similar to the following:
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 8/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
In case you’re using Git branching work ows (you should), Jenkins will discover
automatically the new branches:
Hence, you must separate your deployment environments to test new changes without
impacting your production. Therefore, having multiple versions of your Lambda
functions makes sense.
Update the Jenkins le to add a new stage to publish a new Lambda function’s version,
every-time you push (or merge) to the master branch:
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 9/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
As a result, a new version will be published based on the master branch source code:
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 10/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
The alias is a pointer to a speci c version, it allows you to promote a function from one
environment to another (such as staging to production). Aliases are mutable, unlike
versions, which are immutable.
That being said, create an alias for the production environment that points to the latest
version published using the AWS command line:
You can now easily promote the latest version published into production by updating the
production alias pointer’s value:
Like what you’re reading? Check out my book and learn how to build, secure, deploy and
manage production-ready Serverless applications in Golang with AWS Lambda.
www.packtpub.com
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 11/12
9/21/2020 CI/CD for Lambda Functions with Jenkins | by Mohamed Labouardy | A Cloud Guru
https://read.acloud.guru/ci-cd-for-lambda-functions-with-jenkins-1c682a6c8d33 12/12