SlideShare a Scribd company logo
AWS Blue/Green Deployment Using Terraform Guide​
​
​
​
Introduction
With the rapid development on how applications are build & shipped,
adopting the right deployment strategy is pivotal for ensuring robust
Continuous Deployment (CD) and maintaining high software
quality standards. Deployment strategies play a crucial role in DevOps
practices, offering varied approaches to software release and
infrastructure management. In this blog, we’ll explore several key
deployment strategies, emphasising their relevance in Continuous
Integration and Continuous Deployment pipelines, before focusing on
the Blue-Green deployment method, particularly its
implementation on AWS using Terraform, a leading Infrastructure
as Code (IaC) tool.
1.​ Rolling Deployment: This technique, integral to
Continuous Deployment, involves incrementally updating
servers with the new version. It’s highly compatible with
Agile methodologies, ensuring minimal downtime and
facilitating a stable Continuous Delivery process.
2.​Canary Deployment: A strategic fit for Continuous
Deployment, canary deployment targets a small segment of
the production environment first. Its gradual approach aligns
well with Agile and DevOps principles, allowing for real-time
monitoring and quick rollback if needed.
3.​A/B Testing Deployment: This strategy is crucial for
user-centric Continuous Deployment, providing direct
feedback on user engagement and experience. It’s a
data-driven approach, often used in conjunction with
Continuous Testing practices.
4.​Recreate Deployment: Simple yet effective, this strategy
involves downtime but is sometimes used in Continuous
Deployment when zero-downtime isn’t a critical factor. It’s
straightforward and suitable for applications with flexible
availability requirements.
5.​ Shadow Deployment: Often used in Continuous
Deployment and Continuous Testing, this strategy involves
duplicating real traffic to a shadow version. It’s excellent for
performance testing under real conditions without impacting
the end-user experience.
Focusing on Blue-Green Deployment, this strategy is used for
Continuous Deployment with zero downtime. It involves maintaining
two identical environments: the Blue (current production) and Green
(new version). At any given time, only one of these environments is
live, serving all production traffic. When it’s time to release a new
version of the software, the update is first deployed to the inactive
environment (e.g., green). The switch from Blue to Green ensures
minimal downtime and provides a quick rollback mechanism in case
of issues, aligning seamlessly with Continuous Deployment and
Continuous Integration (CI) practices.
Integrating Terraform, a prominent Infrastructure as Code tool, into
Blue-Green deployment on AWS enhances the strategy. Terraform
automates the creation and management of both environments,
ensuring consistency and alignment with DevOps, Continuous
Integration, and Continuous Deployment principles. This integration
is particularly beneficial in AWS cloud environments, where managing
complex infrastructures requires both precision and flexibility.
When to use Blue-Green Deployment?
There are several benefits to using blue-green deployment:
●​ Zero downtime: By routing traffic to the new environment
before taking the old one out of service, you can ensure that
there is no disruption to the end users.
●​ Easy rollback: If there are any issues with the new version
of the software, you can quickly roll back by routing traffic
back to the old environment.
●​ Improved reliability: By testing the new version of the
software in a separate environment before releasing it to
production, you can catch and fix any issues before they affect
the end users.
●​ Confidence in Release: Blue-green deployment allows you
to release software updates with confidence, knowing that
you have a fallback plan in case anything goes wrong.
Integrating Terraform with EC2 Autoscaling for
Blue-Green Deployments
While Blue-Green deployments offer significant advantages,
integrating this strategy with tools like Terraform and EC2
Autoscaling groups presents its own set of challenges. In this section,
we’ll delve into these challenges and outline the effective solutions
we’ve developed at SquareOps
The problem with Terraform and EC2 Autoscaling
groups
When implementing Blue-Green deployment using Terraform on AWS
a key challenge emerges with EC2 Auto Scaling groups & how
terraform operates. This challenge is crucial for DevOps engineers and
cloud architects who rely on Terraform for infrastructure as code (IaC)
practices and AWS CodeDeploy for seamless deployment processes.
Addressing this issue is essential for optimizing Continuous
Integration/Continuous Deployment (CI/CD) pipelines and ensuring
efficient cloud resource management.
The core of the problem lies in how Terraform interacts with AWS
Auto Scaling groups during a Blue-Green deployment orchestrated by
AWS CodeDeploy. AWS CodeDeploy, a critical service in AWS for
automating software deployments, plays a vital role in this setup.
According to the AWS CodeDeploy documentation, during a
Blue-Green deployment, a new Auto Scaling group is created to
transition to the new version of the application.
However, when Terraform is used to create and manage these Auto
Scaling groups, it does not automatically recognize or incorporate the
new Auto Scaling group created by CodeDeploy into its state
management. This discrepancy leads to Terraform attempting to
recreate the Auto Scaling group with its original configuration during
subsequent terraform apply operations. As a result, cloud engineers
face errors and inconsistencies, which can disrupt the deployment
process and lead to potential downtime or resource mismanagement.
To delve deeper into this topic, it’s essential to understand the
intricacies of Terraform’s state management and how it interacts with
AWS services. Terraform’s state file is crucial for tracking the
current state of the infrastructure it manages. When external changes
are made to the infrastructure that Terraform manages (in this case,
by AWS CodeDeploy), Terraform’s state file does not automatically
update to reflect these changes. This leads to a state mismatch,
causing Terraform to try to enforce the configuration as defined in its
code, which doesn’t account for the new Auto Scaling group.
Solution to Seamless Blue-Green Deployment for EC2
Autoscaling Groups with Terraform and AWS
CodeDeploy
To navigate this challenge, we’ve developed an approach that ensures
Terraform, AWS CodeDeploy, and EC2 Autoscaling groups work in
harmony. This section provides a detailed step-by-step
implementation of the solution.
1. Modify the official terraform module ( here
Terraform-aws-module) to accommodate the solution
requirements
Add support for an additional variable to ignore resource tag-related
changes.
2. Sample Terraform code to deploy an EC2
Autoscaling group
To avoid the AWS Autoscaling group module from creating
auto-scaling groups with random names, we have set use_name_prefix
to false​
Then using the terraform data source feature, we fetched the name of
the new auto-scaling group with the help of tags and referred to it
while calling the module again for any changes.
This code snippet assumes that the VPC network and AWS Application
Loadbalancers are already created. To get a complete example of
creating an autoscaling group using Terraform, that includes VPC and
ALB as well, send an email to consult@squareops.com to get full
access to the Terraform library maintained by SquareOps​
Configuring AWS CodeDeploy for Blue-Green Deployment
3. We have used Terraform to create the AWS code
deploy service resources also and its configurations
AWS Blue_Green Deployment Using Terraform Guide.pdf
This terraform code snippet creates an IAM Role and Policy for
CodeDeploy that grants AWS CodeDeploy the necessary permissions
to perform deployments across EC2 instances and Autoscaling groups.
This role will be assumed by the CodeDeploy service. It also creates
the CodeDeploy Application and sets up deployment groups ( one for
each of the Blue and Green environments. )
4. Solution to the terraform state deviation problem
We also created a script that needs to be run before any terraform
operations. This will import new auto-scaling groups created by AWS
Codedeploy Service’s Blue-Green Deployment strategy and replace the
older auto-scaling group details. Now the terraform plan and terraform
apply will not create a new auto-scaling group after the CI/CD
deployments.
Let us go through all the commands in this script:
a.terraform refresh command refreshes the state of terraform to
identify any changes.
b. This command will match the name in outputs and state
Conclusion
In this blog, we’ve navigated through the challenges of setting up
Blue-Green deployments using AWS, Terraform, and AWS
CodeDeploy. Blue-Green deployment is more than just a deployment
strategy; it’s a pathway to ensuring zero downtime, enhancing the
reliability of your applications, and providing a safety net through easy
rollbacks.
By integrating this approach with Terraform’s powerful infrastructure
as code capabilities and AWS’s scalable cloud infrastructure, we offer a
solution that not only optimizes your deployment processes but also
aligns with the best practices in cloud computing and DevOps.
At SquareOps Technologies, our commitment to innovation and
excellence in cloud services is unwavering. We understand that every
organization’s needs are unique, and our team of experts is equipped
to provide tailored solutions that meet your specific requirements.
Whether you’re just starting your cloud journey or looking to optimize
existing systems, our comprehensive suite of services in Kubernetes,
CI/CD, Observability, Infrastructure as Code, Security, and Cost
Optimizations is designed to guide and support you at every step.
We invite you to reach out to us for any assistance in implementing
Blue-Green deployments or other cloud and DevOps solutions. Let’s
work together to transform your deployment strategy and propel your
business toward greater efficiency and success.
Source Url:
https://medium.com/@nitinyadav745/aws-blue-green-deployment-u
sing-terraform-guide-86131362ee67

More Related Content

Similar to AWS Blue_Green Deployment Using Terraform Guide.pdf (20)

PDF
Workshop Infrastructure as Code - Suestra
Mario IC
 
PDF
A Hands-on Introduction on Terraform Best Concepts and Best Practices
Nebulaworks
 
PDF
Building infrastructure as code using Terraform - DevOps Krakow
Anton Babenko
 
PDF
Hashicorp-Terraform-Deep-Dive-with-no-Fear-Victor-Turbinsky-Texuna.pdf
ssuser705051
 
PDF
Terraform-2.pdf
rutiksankapal21
 
PDF
Terraform GitOps on Codefresh
Codefresh
 
PPTX
Terraform in production - experiences, best practices and deep dive- Piotr Ki...
PROIDEA
 
PPTX
How to start using aws
Catalin Dumitras
 
PDF
Aws blue green_deployments
saifam
 
PPTX
AWS EC2 Blue-Green Deployment using code deploy
HarpalGohil4
 
PPTX
terraform cours intéressant et super fort
amar719595
 
PDF
DevOps Braga #9: Introdução ao Terraform
DevOps Braga
 
PDF
Terraform AWS modules and some best practices - September 2019
Anton Babenko
 
PDF
Terraform modules and (some of) best practices
Anton Babenko
 
PDF
The hitchhiker's guide to terraform your infrastructure
Fernanda Martins
 
PPTX
Managing AWS infrastructure using CloudFormation
Anton Babenko
 
PPTX
Final terraform
Gourav Varma
 
PDF
Terraform AWS modules and some best-practices - May 2019
Anton Babenko
 
PPTX
Efficient way to manage environments in AWS
amii894
 
PPTX
Terraform
Pathum Fernando ☁
 
Workshop Infrastructure as Code - Suestra
Mario IC
 
A Hands-on Introduction on Terraform Best Concepts and Best Practices
Nebulaworks
 
Building infrastructure as code using Terraform - DevOps Krakow
Anton Babenko
 
Hashicorp-Terraform-Deep-Dive-with-no-Fear-Victor-Turbinsky-Texuna.pdf
ssuser705051
 
Terraform-2.pdf
rutiksankapal21
 
Terraform GitOps on Codefresh
Codefresh
 
Terraform in production - experiences, best practices and deep dive- Piotr Ki...
PROIDEA
 
How to start using aws
Catalin Dumitras
 
Aws blue green_deployments
saifam
 
AWS EC2 Blue-Green Deployment using code deploy
HarpalGohil4
 
terraform cours intéressant et super fort
amar719595
 
DevOps Braga #9: Introdução ao Terraform
DevOps Braga
 
Terraform AWS modules and some best practices - September 2019
Anton Babenko
 
Terraform modules and (some of) best practices
Anton Babenko
 
The hitchhiker's guide to terraform your infrastructure
Fernanda Martins
 
Managing AWS infrastructure using CloudFormation
Anton Babenko
 
Final terraform
Gourav Varma
 
Terraform AWS modules and some best-practices - May 2019
Anton Babenko
 
Efficient way to manage environments in AWS
amii894
 

Recently uploaded (20)

PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Top Managed Service Providers in Los Angeles
Captain IT
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Ad

AWS Blue_Green Deployment Using Terraform Guide.pdf

  • 1. AWS Blue/Green Deployment Using Terraform Guide​ ​ ​ ​ Introduction With the rapid development on how applications are build & shipped, adopting the right deployment strategy is pivotal for ensuring robust Continuous Deployment (CD) and maintaining high software quality standards. Deployment strategies play a crucial role in DevOps practices, offering varied approaches to software release and infrastructure management. In this blog, we’ll explore several key deployment strategies, emphasising their relevance in Continuous Integration and Continuous Deployment pipelines, before focusing on the Blue-Green deployment method, particularly its
  • 2. implementation on AWS using Terraform, a leading Infrastructure as Code (IaC) tool. 1.​ Rolling Deployment: This technique, integral to Continuous Deployment, involves incrementally updating servers with the new version. It’s highly compatible with Agile methodologies, ensuring minimal downtime and facilitating a stable Continuous Delivery process. 2.​Canary Deployment: A strategic fit for Continuous Deployment, canary deployment targets a small segment of the production environment first. Its gradual approach aligns well with Agile and DevOps principles, allowing for real-time monitoring and quick rollback if needed. 3.​A/B Testing Deployment: This strategy is crucial for user-centric Continuous Deployment, providing direct feedback on user engagement and experience. It’s a data-driven approach, often used in conjunction with Continuous Testing practices.
  • 3. 4.​Recreate Deployment: Simple yet effective, this strategy involves downtime but is sometimes used in Continuous Deployment when zero-downtime isn’t a critical factor. It’s straightforward and suitable for applications with flexible availability requirements. 5.​ Shadow Deployment: Often used in Continuous Deployment and Continuous Testing, this strategy involves duplicating real traffic to a shadow version. It’s excellent for performance testing under real conditions without impacting the end-user experience. Focusing on Blue-Green Deployment, this strategy is used for Continuous Deployment with zero downtime. It involves maintaining two identical environments: the Blue (current production) and Green (new version). At any given time, only one of these environments is live, serving all production traffic. When it’s time to release a new version of the software, the update is first deployed to the inactive environment (e.g., green). The switch from Blue to Green ensures
  • 4. minimal downtime and provides a quick rollback mechanism in case of issues, aligning seamlessly with Continuous Deployment and Continuous Integration (CI) practices. Integrating Terraform, a prominent Infrastructure as Code tool, into Blue-Green deployment on AWS enhances the strategy. Terraform automates the creation and management of both environments, ensuring consistency and alignment with DevOps, Continuous Integration, and Continuous Deployment principles. This integration is particularly beneficial in AWS cloud environments, where managing complex infrastructures requires both precision and flexibility. When to use Blue-Green Deployment? There are several benefits to using blue-green deployment: ●​ Zero downtime: By routing traffic to the new environment before taking the old one out of service, you can ensure that there is no disruption to the end users.
  • 5. ●​ Easy rollback: If there are any issues with the new version of the software, you can quickly roll back by routing traffic back to the old environment. ●​ Improved reliability: By testing the new version of the software in a separate environment before releasing it to production, you can catch and fix any issues before they affect the end users. ●​ Confidence in Release: Blue-green deployment allows you to release software updates with confidence, knowing that you have a fallback plan in case anything goes wrong.
  • 6. Integrating Terraform with EC2 Autoscaling for Blue-Green Deployments While Blue-Green deployments offer significant advantages, integrating this strategy with tools like Terraform and EC2 Autoscaling groups presents its own set of challenges. In this section, we’ll delve into these challenges and outline the effective solutions we’ve developed at SquareOps The problem with Terraform and EC2 Autoscaling groups
  • 7. When implementing Blue-Green deployment using Terraform on AWS a key challenge emerges with EC2 Auto Scaling groups & how terraform operates. This challenge is crucial for DevOps engineers and cloud architects who rely on Terraform for infrastructure as code (IaC) practices and AWS CodeDeploy for seamless deployment processes. Addressing this issue is essential for optimizing Continuous Integration/Continuous Deployment (CI/CD) pipelines and ensuring efficient cloud resource management. The core of the problem lies in how Terraform interacts with AWS Auto Scaling groups during a Blue-Green deployment orchestrated by AWS CodeDeploy. AWS CodeDeploy, a critical service in AWS for automating software deployments, plays a vital role in this setup. According to the AWS CodeDeploy documentation, during a Blue-Green deployment, a new Auto Scaling group is created to transition to the new version of the application.
  • 8. However, when Terraform is used to create and manage these Auto Scaling groups, it does not automatically recognize or incorporate the new Auto Scaling group created by CodeDeploy into its state management. This discrepancy leads to Terraform attempting to recreate the Auto Scaling group with its original configuration during subsequent terraform apply operations. As a result, cloud engineers face errors and inconsistencies, which can disrupt the deployment process and lead to potential downtime or resource mismanagement. To delve deeper into this topic, it’s essential to understand the intricacies of Terraform’s state management and how it interacts with AWS services. Terraform’s state file is crucial for tracking the current state of the infrastructure it manages. When external changes are made to the infrastructure that Terraform manages (in this case, by AWS CodeDeploy), Terraform’s state file does not automatically update to reflect these changes. This leads to a state mismatch, causing Terraform to try to enforce the configuration as defined in its code, which doesn’t account for the new Auto Scaling group.
  • 9. Solution to Seamless Blue-Green Deployment for EC2 Autoscaling Groups with Terraform and AWS CodeDeploy To navigate this challenge, we’ve developed an approach that ensures Terraform, AWS CodeDeploy, and EC2 Autoscaling groups work in harmony. This section provides a detailed step-by-step implementation of the solution. 1. Modify the official terraform module ( here Terraform-aws-module) to accommodate the solution requirements Add support for an additional variable to ignore resource tag-related changes.
  • 10. 2. Sample Terraform code to deploy an EC2 Autoscaling group
  • 11. To avoid the AWS Autoscaling group module from creating auto-scaling groups with random names, we have set use_name_prefix to false​ Then using the terraform data source feature, we fetched the name of
  • 12. the new auto-scaling group with the help of tags and referred to it while calling the module again for any changes. This code snippet assumes that the VPC network and AWS Application Loadbalancers are already created. To get a complete example of creating an autoscaling group using Terraform, that includes VPC and ALB as well, send an email to [email protected] to get full access to the Terraform library maintained by SquareOps​ Configuring AWS CodeDeploy for Blue-Green Deployment 3. We have used Terraform to create the AWS code deploy service resources also and its configurations
  • 14. This terraform code snippet creates an IAM Role and Policy for CodeDeploy that grants AWS CodeDeploy the necessary permissions to perform deployments across EC2 instances and Autoscaling groups. This role will be assumed by the CodeDeploy service. It also creates the CodeDeploy Application and sets up deployment groups ( one for each of the Blue and Green environments. ) 4. Solution to the terraform state deviation problem
  • 15. We also created a script that needs to be run before any terraform operations. This will import new auto-scaling groups created by AWS Codedeploy Service’s Blue-Green Deployment strategy and replace the older auto-scaling group details. Now the terraform plan and terraform apply will not create a new auto-scaling group after the CI/CD deployments. Let us go through all the commands in this script: a.terraform refresh command refreshes the state of terraform to identify any changes.
  • 16. b. This command will match the name in outputs and state Conclusion In this blog, we’ve navigated through the challenges of setting up Blue-Green deployments using AWS, Terraform, and AWS CodeDeploy. Blue-Green deployment is more than just a deployment strategy; it’s a pathway to ensuring zero downtime, enhancing the reliability of your applications, and providing a safety net through easy rollbacks. By integrating this approach with Terraform’s powerful infrastructure as code capabilities and AWS’s scalable cloud infrastructure, we offer a solution that not only optimizes your deployment processes but also aligns with the best practices in cloud computing and DevOps.
  • 17. At SquareOps Technologies, our commitment to innovation and excellence in cloud services is unwavering. We understand that every organization’s needs are unique, and our team of experts is equipped to provide tailored solutions that meet your specific requirements. Whether you’re just starting your cloud journey or looking to optimize existing systems, our comprehensive suite of services in Kubernetes, CI/CD, Observability, Infrastructure as Code, Security, and Cost Optimizations is designed to guide and support you at every step. We invite you to reach out to us for any assistance in implementing Blue-Green deployments or other cloud and DevOps solutions. Let’s work together to transform your deployment strategy and propel your business toward greater efficiency and success. Source Url: https://medium.com/@nitinyadav745/aws-blue-green-deployment-u sing-terraform-guide-86131362ee67