Mastering Blue-Green Deployments with AWS Lambda for Zero-Downtime Releases

Jorge Freitas
4 min readJun 20, 2024

--

blue-green deployment

As software engineers, ensuring continuous delivery with minimal downtime is critical for maintaining user satisfaction and system reliability. One powerful technique to achieve this is through blue-green deployments, and AWS Lambda offers an efficient serverless approach to implement this strategy. This article dives into the advanced concepts, benefits, and implementation strategies of blue-green deployments using AWS Lambda, leveraging expert insights and industry best practices.

What is Blue-Green Deployment?

Blue-green deployment is a software release strategy that reduces downtime and mitigates risks by maintaining two identical production environments, known as the blue and green environments. By switching traffic between these environments, updates can be rolled out seamlessly without disrupting user experience.

Benefits

  • Zero Downtime: The switch between environments is instantaneous, minimizing downtime.
  • Risk Mitigation: Quick rollback capability reduces the impact of any deployment issues.
  • Production-like Testing: The green environment allows for thorough testing under realistic conditions before going live.

Key Concepts

Dual Environments

  • Blue Environment: The current live version of the application.
  • Green Environment: The new version of the application that is being prepared for release.

Traffic Switching

  • Traffic is directed to the green environment once it is ready, using load balancers, DNS changes, or similar mechanisms.
  • If issues arise, traffic can be reverted to the blue environment quickly.

Rollback Capability

  • The ability to switch back to the blue environment ensures that any problems with the green environment do not impact users for long.

Implementation with AWS Lambda

AWS Lambda, a serverless computing service, simplifies the blue-green deployment process by eliminating the need to manage server infrastructure. Here’s how to implement this strategy using AWS Lambda and related services.

Setting Up Lambda Functions

Prepare two versions of your Lambda function. In AWS, Lambda supports versioning and aliases, which can be leveraged to create blue and green environments.

  • Blue Environment: The current live version (e.g., function:1).
  • Green Environment: The new version (e.g., function:2) that you are preparing for release.

Deploying to Green

Deploy the new version of your Lambda function as a new version. Use tools like AWS CodeDeploy and AWS Serverless Application Model (SAM) to automate the deployment process.

  • AWS SAM: A framework for building serverless applications, which simplifies the deployment of Lambda functions and related resources.
  • AWS CodeDeploy: Supports blue-green deployments for Lambda functions, managing the deployment process and traffic shifting.

Below is an example of how you can use the AWS SAM to implement blue-green deployments for an AWS Lambda function.

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >
SAM Template for Blue-Green Deployment with AWS Lambda

Globals:
Function:
Timeout: 30
MemorySize: 128
Runtime: python3.9

Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: app.lambda_handler
CodeUri: .
Description: 'My Lambda Function'
AutoPublishAlias:
Description: 'Production Alias'
Name: 'prod'
DeploymentPreference:
Type: 'Linear10PercentEvery1Minute'
Role: !Ref CodeDeployServiceRole
Environment:
Variables:
ENV: 'production'

CodeDeployServiceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Service: 'codedeploy.amazonaws.com'
Action: 'sts:AssumeRole'
Policies:
- PolicyName: 'CodeDeployPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'lambda:UpdateFunctionCode'
- 'lambda:UpdateAlias'
- 'lambda:CreateAlias'
- 'lambda:GetFunctionConfiguration'
- 'codedeploy:*'
Resource: '*'

Outputs:
FunctionAlias:
Description: "Alias ARN"
Value: !GetAtt MyFunction.ProdAliasArn

The DeploymentPreference in the SAM template is set to Linear10PercentEvery1Minute, which means 10% of the traffic will be shifted to the new version every minute. You can monitor the deployment progress in the AWS CodeDeploy console.

Explanation

  • AutoPublishAlias: This property automatically creates and manages an alias for your Lambda function. The alias points to the version of the function that SAM creates during deployment.
  • DeploymentPreference: Specifies how traffic is shifted from the old version to the new version. In this example, traffic is shifted linearly by 10% every minute.
  • CodeDeployServiceRole: An IAM role that gives AWS CodeDeploy permissions to manage the deployment process.

By using this setup, you can achieve a blue-green deployment strategy with AWS Lambda, ensuring zero-downtime releases and the ability to roll back if something goes wrong.

Industry Insights

According to Martin Fowler, a renowned software development thought leader, blue-green deployments are instrumental in achieving continuous delivery and deployment pipelines (Fowler, 2010). This strategy aligns with the DevOps principles of automating and streamlining deployment processes to enhance reliability and speed.

In a case study by Netflix, blue-green deployments were highlighted as a critical component of their continuous delivery strategy, enabling them to deploy hundreds of times a day without impacting user experience (Asghar, 2018).

Conclusion

Blue-green deployments using AWS Lambda offer a robust framework for achieving zero-downtime releases, providing a safety net through immediate rollback capabilities. By leveraging AWS tools and best practices, software engineers can implement blue-green deployments to enhance their continuous delivery pipelines, ensuring high availability and user satisfaction.

--

--