Automate the Experience of Lambda Versioning with Configuration Change and Code Change Using CloudFormation and Code Pipeline

Gargee Bhatnagar
Gargee Bhatnagar
Published in
8 min readMay 28, 2022

--

“ I have checked the documents of AWS to automate the experience of lambda versioning with change in configuration and code. I have checked for AWS code pipeline service which makes the process easy and automates for everyone. The Pricing of Code pipeline is based on when the pipeline runs. Also price for code build. ”

AWS CloudFormation is a service that helps you model and set up your AWS resources so that you can speed less time managing those resources and more time focusing on your applications that run in AWS. You create a template that describes all the AWS resources that you want (like Amazon EC2 instances or Amazon RDS DB instances), and CloudFormation takes care of provisioning and configuring those resources for you. You don’t need to individually create and configure AWS resources and figure out what’s dependent on what; CloudFormation handles that. The following scenarios demonstrate how CloudFormation can help.

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. You can get started quickly by using prepackaged build environments, or you can create custom build environments that use your own build tools. With CodeBuild, you are charged by the minute for the compute resources you use.

AWS CodeDeploy is a fully managed deployment service that automates software deployments to a variety of compute 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. You can use AWS CodeDeploy to automate software deployments, eliminating the need for error-prone manual operations. The service scales to match your deployment needs.

AWS CodePipeline is a continuous delivery service you can use to model, visualize, and automate the steps required to release your software. You can quickly model and configure the different stages of a software release process. CodePipeline automates the steps required to release your software changes continuously.

Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging. With Lambda, you can run code for virtually any type of application or backend service.

In this post, you will experience the automation of lambda versioning with configuration and code change using CloudFormation and code pipeline. Here I have created a code pipeline to automate lambda creation and its versioning with change in set parameters using cloud formation with artifacts to be stored in s3 bucket.

Prerequisites

You’ll need a Github account and git installed on a local machine. You’ll need AWS CLI installed and configured on your local machine.

Architecture Overview

The architecture diagram shows the overall deployment architecture with data flow, code build, code deploy, code pipeline, s3, sns, cloud formation, lambda.

Solution overview

The blog post consists of the following phases:

  1. Create of IAM Role, S3 Bucket, Buildspec.yml, Template.yaml and Sample HelloWorld.js File
  2. Create of CodePipeline and Build Project
  3. Create SNS Topic with Subscription and Check of CloudFormation with Lambda Creation
  4. Testing of Lambda Configuration Parameters and Code Deploy while Updating in GitHub

Phase 1: Create of IAM Role, S3 Bucket, Buildspec.yml, Template.yaml and Sample HelloWorld.js File

  1. Open the IAM Console and create a role for CloudFormation with AWS lambda execute policy and inline policy with required permission. Create an S3 bucket named as lambda-ci-cd-demo-test. Also configure buildspec.yml file, template.yaml file and helloworld.js file as per the requirement to build a lambda code and configuration part.

Phase 2: Create of CodePipeline and Build Project

  1. Open codepipeline console and create a pipeline with source as github and provider as cloudformation with default IAM role and custom s3 inline policy to be attached with the role. Also build a project with custom settings.

Phase 3: Create SNS Topic with Subscription and Check of CloudFormation with Lambda Creation

  1. Open the Amazon Simple Notification Service and create a topic with subscription for approval notification in the code pipeline. In the code pipeline the stages as source, build, deploy as createchangeset, approvechangeset, executechangeset.

Phase 4: Testing of Lambda Configuration Parameters and Code Deploy while Updating in GitHub

  1. Test the lambda configuration and code in doing changes on various parameters in github.

Clean-up

Delete Code Pipeline, Code Build, IAM Role, S3 Bucket, Cloud Formation, SNS.

Pricing

I review the pricing and estimated cost of this example.

Cost of Code Build = $0.0

Cost of Lambda = $0.0

Cost of Simple Notification Service = $0.0

Cost of Simple Storage Service = $1.0

Total Cost = $(0.0 + 0.0 + 0.0 + 1.0) = $1.0

Summary

In this post, I showed “how to automate the experience of lambda versioning with configuration change and code change using cloudformation and code pipeline”.

For more details on AWS CodeDeploy, Checkout Get started AWS CodeDeploy, open the AWS CodeDeploy console. To learn more, read the AWS CodeDeploy documentation. For more details on AWS CodePipeline, Checkout Get started AWS CodePipeline, open the AWS CodePipeline console. To learn more, read the AWS CodePipeline documentation.

For more details on AWS CodeBuild, Checkout Get started AWS CodeBuild, open the AWS CodeBuild console. To learn more, read the AWS CodeBuild documentation. For more details on AWS CloudFormation, Checkout Get started AWS CloudFormation, open the AWS CloudFormation console. To learn more, read the AWS CloudFormation documentation.

Thanks for reading!

Connect with me: Linkedin

--

--

Gargee Bhatnagar
Gargee Bhatnagar

DevOps Engineer and AWS Solution Architect in Electromech Corporation