AWS Lab: Implementing Blue/Green Deployments with CloudFormation and Route 53
Design a deployment strategy to meet business requirements
AWS Lab: Implementing Blue/Green Deployments with CloudFormation and Route 53
This lab provides hands-on experience in designing a deployment strategy that meets business requirements for zero-downtime updates and easy rollbacks. You will use Infrastructure as Code (IaC) with AWS CloudFormation to manage environments and Route 53 Weighted Routing to shift traffic between 'Blue' and 'Green' versions of a static website.
Prerequisites
- An AWS Account with administrative permissions.
- AWS CLI installed and configured with your credentials (
aws configure). - Basic familiarity with YAML syntax and the AWS Management Console.
- A text editor (e.g., VS Code or Notepad++).
[!IMPORTANT] Ensure you are working in a single region (e.g.,
us-east-1) throughout the entire lab.
Learning Objectives
- Author CloudFormation templates to provision repeatable infrastructure.
- Implement a Blue/Green deployment strategy using stack isolation.
- Manage traffic shifting using Route 53 Weighted Records to minimize deployment risk.
- Understand the mechanism for a rapid rollback in case of deployment failure.
Architecture Overview
In this architecture, we decouple the infrastructure from the traffic routing. The 'Blue' stack represents the production environment (v1), while the 'Green' stack represents the new version (v2). Traffic is controlled at the DNS level.
Step-by-Step Instructions
Step 1: Create the 'Blue' Infrastructure (v1)
First, we will deploy the initial version of our application using a CloudFormation template.
- Save the following content as
app-v1.yaml:
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
AppVersion:
Type: String
Default: v1
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "brainybee-lab-app-${AppVersion}-${AWS::AccountId}"
WebsiteConfiguration:
IndexDocument: index.html
Outputs:
WebsiteURL:
Value: !GetAtt S3Bucket.WebsiteURL- Deploy the stack via CLI:
aws cloudformation create-stack --stack-name blue-stack --template-body file://app-v1.yaml --parameters ParameterKey=AppVersion,ParameterValue=blue▶Console Alternative
- Navigate to CloudFormation > Create stack.
- Upload
app-v1.yaml. - Enter Stack name
blue-stack. - Set
AppVersionparameter toblue. - Click Next through the wizard and Submit.
Step 2: Deploy the 'Green' Infrastructure (v2)
Now, we prepare the new version without affecting the current production (Blue) environment.
aws cloudformation create-stack --stack-name green-stack --template-body file://app-v1.yaml --parameters ParameterKey=AppVersion,ParameterValue=green▶Console Alternative
- Repeat the CloudFormation creation steps but use the name
green-stack. - Set the
AppVersionparameter togreen.
Step 3: Shift Traffic (The Deployment)
In a real-world scenario, you would use a Route 53 Weighted Record. For this lab, we will simulate the "cutover" by updating a central 'Routing' stack or manually updating a DNS record to point to the Green bucket URL.
[!TIP] In a production CI/CD pipeline, this step is often automated by AWS CodeDeploy or a Lambda function that updates the Route 53 weights.
Visualizing the Traffic Shift
The following graph illustrates how traffic transitions from Blue to Green over time during a canary or linear deployment.
Checkpoints
- Verify Blue Status: Run
aws s3 website s3://brainybee-lab-app-blue-<YOUR_ID>. You should be able to access the endpoint URL provided in the CloudFormation outputs. - Verify Green Status: Ensure the Green stack is in
CREATE_COMPLETEstatus. - Simulation: If you had a Route 53 Hosted Zone, changing the weight of the Blue record to 0 and Green to 100 would complete the deployment.
Troubleshooting
| Problem | Possible Cause | Fix |
|---|---|---|
AlreadyExistsException | Bucket name is globally unique and already taken. | Change the bucket name prefix in the YAML and redeploy. |
| Access Denied (403) | S3 Bucket Policy is not public. | Ensure the template includes public access settings (Note: Modern AWS accounts block public access by default; check account-level S3 settings). |
| Stack Rollback | Incorrect YAML indentation or missing parameters. | Check the 'Events' tab in CloudFormation Console for the specific error message. |
Challenge
Modify your CloudFormation template to include an AWS Lambda function that acts as a 'Health Check'. Configure this function to trigger a rollback of the traffic shift if it detects a 500 error from the Green environment during the deployment phase.
Cost Estimate
| Service | Usage | Estimated Cost |
|---|---|---|
| AWS CloudFormation | Standard use | $0.00 |
| Amazon S3 | 2 buckets, minimal storage | $0.00 (Free Tier) |
| Route 53 | 1 Hosted Zone (optional) | $0.50 / month |
| Total | <$1.00 |
Concept Review
| Strategy | Pros | Cons |
|---|---|---|
| In-Place | Simple, no extra cost | Downtime during update, hard to rollback |
| Blue/Green | No downtime, easy rollback | Double the resource cost during deployment |
| Canary | Limits blast radius | Complex monitoring/routing logic required |
Clean-Up / Teardown
[!WARNING] Failure to delete these resources may result in minor charges if you exceed free tier limits.
- Empty and Delete S3 Buckets:
aws s3 rm s3://brainybee-lab-app-blue-<YOUR_ACCOUNT_ID> --recursive
aws s3 rm s3://brainybee-lab-app-green-<YOUR_ACCOUNT_ID> --recursive- Delete CloudFormation Stacks:
aws cloudformation delete-stack --stack-name blue-stack
aws cloudformation delete-stack --stack-name green-stack