Zero-Downtime Blue/Green Deployment Strategy with AWS CodeDeploy and Lambda
Software deployment is the process of making it accessible to users or systems, and the strategy used will vary depending on the program type, user needs, and risk tolerance.
Here are the popular deployment strategies used widely,
- Blue-Green Deployment: To reduce downtime, switch traffic between the new and existing environments.
- Canary Deployment: Before a major release, gradually distribute changes to a selected few users.
- Rolling Deployment: Roll out updates across instances incrementally without downtime.
- Recreate Deployment: There will be a temporary downtime as the new version is replaced with the old one.
Here we will mainly focus on achieving Blue Green Deployment
strategy using AWS CodeDeploy
and Lambda
.
What is Blue/Green Deployment?
Blue-green deployment uses two environments to release new versions of an application. The goal is to minimize downtime and risk while deploying updates.
How it works?
- Create two environments, one called
blue
and the other calledgreen
. - The
blue
environment runs the current version of the application. - The
green
environment runs the latest version of the application. - Test the
green
environment. - Once the
green
environment is ready, shift traffic fromblue
togreen
. - If necessary roll back to the
blue
environment in case of errors or failure. - After the
green
environment has no potential issues, consider it as the newblue
environment and use the oldblue
asgreen
environment for next deployments.
Next, we will go through the procedures to create a Lambda function and set up CodeDeploy.
Step 1: Create Lambda Function.
Step 2: Create a version 1 sample code and deploy it and navigate to the versions tab to publish it as version 1.
continue step 2 to create a version 2 code and publish it as version 2.
Step 3: Navigate to the Alias tab to create an alias of version 1.
A Lambda alias is a pointer to a function version that you can update. The function’s users can access the function version using the alias arn. When deploying a new version, we can update the alias to use the new version, or split traffic between two versions.
Step 4: To hit the lambda function here we are going to use api gateway. Alternatively, you can use lambda’s function URL.
Select HTTP API -> under Integration select lambda dropdown.
Before creating api gateway endpoints copy the function ARN of the alias we created (
Alias:blueGreenDeployment
) and paste it in the lambda function dropdown.
Step 5: Now we will set up CodeDeploy. Before that, we need an IAM role to perform the creation of the code deploy applications. Here I created a role called codeDeployForLambda
and attached the policy AWSCodeDeployRoleForLambda
. Copy the arn of the role we created and keep it ready.
Step 6: Go to code deploy -> click create Application -> give application name -> select AWS Lambda
under Compute Platform.
Step 7: Navigate into the code deploy application created and click on create Deployment Group give the deployment group name and paste the role arn we copied earlier under service role and select Deployment settings.
The Deployment Settings comes with Linear, Canary and All-At-Once. In simple terms these are traffic switching methods from
Blue
toGreen
environments. Here i am selectinglinear10PercentEvery1Minute
Step 8: As a final step we have to create the deployment, there we need to have appSpec either in .json or .yml.
The AppSpec or revision type is used to manage each deployment as a series of lifecycle event hooks, which are defined in the file.
For Lambda deployments we can use it to specify,
- Lambda function version to deploy.
- The functions are to be used as validation tests during the deployment if you are running scripts or Lambda validation functions as part of the deployment.
Here is the basic app Spec configuration which needed to point tothe current version V1(Blue
) and target version v2 (Green
). Paste this configuration under the Revision Type
.
{
"version": 0.0,
"Resources": [
{
"my-test-app": { //lambda function name
"Type": "AWS::Lambda::Function",
"Properties": {
"Name": "my-test-app", //lambda function name
"Alias": "blueGreenDeployment", //lambda alias name
"CurrentVersion": "1", //Blue
"TargetVersion": "2" //Green
}
}
}
]
}
Once the deployment is created you can see the deployment process.
Now, you can check the traffic switching happening between the version by hitting the API gateway endpoints created. if the response is shifting between Hello from version 1
and Hello from version 2
then the deployment you created is working correctly.
After the successful deployment, the lambda alias will point to the latest version.
Pros of Blue Green Deployment:
- Zero downtime.
- Easy rollback to the previous version.
- Isolated testing of the new version.
Cons of Blue Green Deployment:
- Cost: Maintaining two identical environments can be expensive since we can use this strategy for
EC2
andECS
. This includes the cost of infrastructure and the effort to keep both environments up-to-date. - Complexity: Setting up blue-green deployment can be complex. It requires careful planning and coordination.
We can still Automate the deployments with AWS SAM and CI/CD pipelines. Note that the sam build or the codeBuild should contain the appSpecTemplate with the correct versions need to be targeted.
Conclusion:
Blue/Green deployment saves downtime by keeping separate environments for current (Blue) and new (Green) versions, allowing seamless traffic switching and rapid rollbacks. AWS CodeDeploy streamlines this process with automated traffic shifting providing greater control and reliability in deployments.
About The Author:
Sankarra Narayanan G is a software developer at CodeStax.ai with over two years of experience in Vue.js, Node.js, and AWS services. He specializes in building scalable and efficient applications. Passionate about backend development and cloud architecture, he actively explores new technologies to deliver innovative and high-impact solutions.
About CodeStax.Ai
At CodeStax.AI, we stand at the nexus of innovation and enterprise solutions. We offer technology partnerships that empower businesses to drive efficiency, innovation, and growth, harnessing the transformative power of no-code platforms and advanced AI integrations.
But what is the real magic? It’s our tech tribe behind the scenes. If you have a knack for innovation and a passion for redefining the norm, we have the perfect tech playground. CodeStax. Ai offers more than a job — it’s a journey into the heart of what’s next. Join us and be part of the revolution that’s redefining the enterprise tech landscape.