Day 17 - AWS Elastic Beanstalk Blue Green Deployment with Terraform
Today I worked on Blue Green Deployment using AWS Elastic Beanstalk and Terraform.
The main goal was to understand how to release a new version of an application without taking production down. In Azure App Service, this is commonly done using deployment slots. In AWS Elastic Beanstalk, the same idea can be implemented by running two separate environments and swapping their environment URLs.
What I Built
In this project, I created two Elastic Beanstalk environments.
The Blue environment represents production and runs application version 1.0.
The Green environment represents staging and runs application version 2.0.
Both environments are created using Terraform. Each environment has its own Elastic Beanstalk setup with load balancing, auto scaling, health checks, and application version deployment.
Architecture
Why Blue Green Deployment Matters
In a normal deployment, we update the existing application in place. If something goes wrong during the deployment, users may see downtime or errors.
Blue Green Deployment avoids this by keeping the current version running while the new version is deployed separately.
This is similar to database failover thinking. You do not modify the active system blindly. You prepare another environment, validate it, and only then move traffic.
Terraform Flow
First, I packaged both versions of the application.
Then I initialized Terraform.
terraform init
After that, I validated and reviewed the plan.
terraform fmt
terraform validate
terraform plan
Then I deployed the infrastructure.
Once the deployment completed, Terraform displayed the URLs for both environments.
Testing Blue and Green
I opened the Blue environment URL first. This showed application version 1.0, which represented the current production version.
Then I opened the Green environment URL. This showed application version 2.0, which represented the new release.
At this stage, both versions were running independently.
Performing the Swap
After validating the Green environment, I performed the CNAME swap.
This can be done from the AWS Console or using the AWS CLI.
aws elasticbeanstalk swap-environment-cnames \
--source-environment-name my-app-bluegreen-blue \
--destination-environment-name my-app-bluegreen-green \
--region us-east-1
After the swap, the production URL started serving version 2.0.
The old version was still available in the other environment, which means rollback is simple. If version 2.0 has an issue, I can swap again and move traffic back to version 1.0.
Key Learnings
Blue Green Deployment helps reduce deployment risk.
Elastic Beanstalk provides a managed way to run application environments without manually building every EC2, load balancer, and auto scaling component.
Terraform makes the infrastructure repeatable.
CNAME swap is the key step that moves traffic from one environment to another.
The main tradeoff is cost because two environments run at the same time.
Cleanup
Since this demo creates Elastic Beanstalk environments, EC2 instances, load balancers, and other resources, cleanup is important.
terraform destroy
Final Thoughts
This was a good practical lesson in production safe deployments. The important idea is not just deploying code, but deploying it in a way that gives us validation, rollback, and less risk.
Comments
Post a Comment