Master Blue-Green Deployment in AWS
Master Blue-Green Deployment in AWS
Overview:
● CodeCommit or GitHub: These are version control services where the code is
stored.
● CodeDeploy: A service that automates the deployment of your applications to
services like EC2 or Lambda.
● CodePipeline: Used to automate the entire CI/CD pipeline, from code changes to
deployment.
● Elastic Load Balancer (ELB): Distributes incoming traffic across multiple targets
(like EC2 instances), ensuring your app stays available.
● Auto Scaling Group (ASG): Automatically adjusts the number of EC2 instances to
handle the load efficiently.
● IAM: Manages access and permissions for your AWS resources.
● You will also use Auto Scaling Groups, which help maintain the right number of
EC2 instances based on your application’s needs.
● Templates will be used to automate the launch of EC2 instances.
● Load Balancers will help direct traffic to the correct set of instances (Blue or Green).
● Load Balancer: A Load Balancer distributes network traffic across multiple instances
to ensure no single server gets overwhelmed. It helps keep your application available
and responsive.
● Auto Scaling Group: An Auto Scaling Group helps you automatically launch or
terminate EC2 instances based on the traffic your application receives. This helps
you save costs and ensures you always have enough resources available.
● In the real world We usually launch EC2 instances manually by going through several
steps, such as selecting an Amazon Machine Image (AMI), instance type, network
settings, security group, etc. While this method works, it can be repetitive.
● Means when you want to launch an EC2 instance (a virtual server in AWS), you
normally fill in all the details like the operating system, instance type, and security
settings manually every time. However, if you plan to launch the same kind of
instance multiple times, doing this process again and again can be time-consuming.
● To avoid this repetition, we can create a launch template or use Auto Scaling
Groups to automatically handle the process of launching instances based on
predefined conditions.
● Using a Launch Template: Instead of manually entering the details every time, you
can create a Launch Template. A Launch Template is like a reusable blueprint where
you define everything once, and then whenever you need to launch an instance, you
can simply select this template and AWS will use it to launch the instance
automatically.
● To create a Launch Template for that go to the EC2 instance and scroll down and
find the launch Template after that click on the launch Template
● When creating a launch template, you’ll specify several details, including AMI
(Amazon Machine Image, Instance Type, Key Pair, Network Settings, Security Group
(Firewall) Etc
● After that select the AMI
● AMI (Amazon Machine Image): The operating system you want to use (like Amazon
Linux).
● After that give the name of the Security Group and add the description
● After that i allow the firewall rules
● After that select the IAM role for that EC2 instance if you don’t have this role you
can’t access the EC2
● IAM Role: If you want your instance to interact with other AWS services (like S3 or
CodeDeploy), you need to assign an IAM Role. This role gives the instance
permission to perform specific tasks, like accessing files in an S3 bucket or deploying
code
● User Data Script: You can also automate the setup of your instance further by
adding a User Data Script. This script runs automatically when the instance starts.
For example, you could use it to install a web server (like Apache) so that your
website is ready as soon as the instance is launched.
● Also in that Script i Setup the code deploy agent
● After that select the template name and select the version default they select 1
● If you want to modify the template you can modify it for that Click on the template
name
● After that click on the action button then click on the modify template
● In that template we change the tag of the instance good practice is always to give the
tags
● If you want to launch which version they give the options for that click on the action
and then click on the launch instance from the template
● As we can see i select the version of the template you want to use
● The instance is set up to automatically install and configure a web server upon
launch using a script (user data).
● The web server will host a homepage with specific content, such as "Welcome to My
Blue-Green Deployment Project Version - 1".
● To ensure the instance can be managed in the future using AWS CodeDeploy, he
installs the CodeDeploy agent during the instance launch process. This eliminates
the need for manual installation later.
● Instead of manually configuring each instance, a launch template allows for reusable,
automated configurations. Each template can have different versions, which can be
updated and modified based on new requirements. He highlights the ease of
deploying instances with a single click after the template is set up.
● For quick check instance is configured or not
● For that click on the Ec2 instance and connect with Ec2 terminal
● After that check the httpd software and HTML file also check the code deploy the
agent
● As we know two access two services in the AWS we need to attach the IAM role
● But in our case i automatically attach the IAM role to check and follow the steps
● After that select the template name also select the version of the template then click
on the next
● After that select the Subnet then click on the next
● After that we use an application load balancer and give the name of the load
balancer also click
● Now if you want to check the web server then copy the Public IP then paste it into the
browser
● But in the real world we do not share the public IP with clients for that we give a load
balancer DNS name
● After that copy the DNS name link and paste it on the browser
Now we start our main concept of blue-green deployment
● If any issues arise, it’s easy to revert to the previous version by shifting traffic back to
the "blue" environment.
● This strategy ensures smooth, low-risk updates without service disruption, as traffic
can be switched between the environments instantly.
● The main idea is to ensure that new code releases are handled in a safe, low- risk
manner by gradually switching traffic from the old system (blue) to the new system
(green)
● No Guarantee in Production: Even if code is thoroughly tested before deployment,
there’s no guarantee it will work perfectly in production due to differences in system
environments such as OS versions, Python, PHP versions, etc
● Risks of In-place Code Release: Directly releasing new code to production (without
careful traffic management) can lead to issues like session crashes, bugs, and
problems with ongoing customer activities.
● Blue-Green Deployment Strategy:
➢ The current running system (blue) handles all live traffic.
➢ A new version of the application (green) is deployed on a separate set of
instances.
➢ Initially, all traffic goes to the blue environment, while the green
environment is tested.
➢ After successful testing, traffic is slowly switched from the blue to the
green environment, either in phases (e.g., 10% green, 90% blue, then
gradually shifting to 100% green) or all at once.
● This approach minimizes the risk of deploying faulty code to production, as it allows
for gradual migration and the ability to switch back to the previous version if issues
arise
● The process of switching traffic between the two environments can be automated
and managed using tools that control network traffic, ensuring a smooth transition
● This strategy is particularly useful for minimizing downtime and reducing the risk of
errors when deploying new code.
● In our case who does this for you
● For that we use code deploy service
● implement a Blue-Green Deployment strategy using AWS services like
CodeDeploy, Auto Scaling, and Load Balancers
● Initially, deployment involved manually launching instances, deploying new code
(version 2), and then switching traffic from the old instance (version 1) to the new
instance using traffic redirection or switching
● AWS CodeDeploy automates this process. When a new code version is available, it
automatically launches a new (green) instance, deploys the code, switches the traffic
to the new instance, and terminates the old instance (blue) to save costs.
● Then we create a folder for the blue-green deployment project then paste all the
commands
● Now we need to create a code deployment for that to go into the code deploy service
● Then create an application
● After that you run the pipeline they fail because we don’t have the build spec and app
spec files in the GitHub repo
● Note:- Building spec and app spec files is a very important thing
● Now i create an app spec file and upload on GitHub
● But we need to create two more files in Git Hub one is before install.sh and
application start.sh
● Now i have all the files
● As we can see they change the traffic from the blue to green
● After some time they stop the old instances and shift all traffic Into the new instances