Hands-On Lab895 words

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.

Loading Diagram...

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.

  1. Save the following content as app-v1.yaml:
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
  1. Deploy the stack via CLI:
bash
aws cloudformation create-stack --stack-name blue-stack --template-body file://app-v1.yaml --parameters ParameterKey=AppVersion,ParameterValue=blue
Console Alternative
  1. Navigate to CloudFormation > Create stack.
  2. Upload app-v1.yaml.
  3. Enter Stack name blue-stack.
  4. Set AppVersion parameter to blue.
  5. 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.

bash
aws cloudformation create-stack --stack-name green-stack --template-body file://app-v1.yaml --parameters ParameterKey=AppVersion,ParameterValue=green
Console Alternative
  1. Repeat the CloudFormation creation steps but use the name green-stack.
  2. Set the AppVersion parameter to green.

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.

Compiling TikZ diagram…
Running TeX engine…
This may take a few seconds

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_COMPLETE status.
  • 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

ProblemPossible CauseFix
AlreadyExistsExceptionBucket 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 RollbackIncorrect 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

ServiceUsageEstimated Cost
AWS CloudFormationStandard use$0.00
Amazon S32 buckets, minimal storage$0.00 (Free Tier)
Route 531 Hosted Zone (optional)$0.50 / month
Total<$1.00

Concept Review

StrategyProsCons
In-PlaceSimple, no extra costDowntime during update, hard to rollback
Blue/GreenNo downtime, easy rollbackDouble the resource cost during deployment
CanaryLimits blast radiusComplex monitoring/routing logic required

Clean-Up / Teardown

[!WARNING] Failure to delete these resources may result in minor charges if you exceed free tier limits.

  1. Empty and Delete S3 Buckets:
bash
aws s3 rm s3://brainybee-lab-app-blue-<YOUR_ACCOUNT_ID> --recursive aws s3 rm s3://brainybee-lab-app-green-<YOUR_ACCOUNT_ID> --recursive
  1. Delete CloudFormation Stacks:
bash
aws cloudformation delete-stack --stack-name blue-stack aws cloudformation delete-stack --stack-name green-stack

Ready to study AWS Certified Solutions Architect - Professional (SAP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free