Blue-Green Deployments Using Elastic Beanstalk
What is AWS Elastic Beanstalk?
EB is an orchestration service offered by AWS for deploying applications. Developers have to just focus on writing code for their application rather than spending time on managing and configuring servers, databases, firewalls, and networks.
With Elastic Beanstalk, you just have to upload your code and Elastic Beanstalk automatically handles the deployment, from capacity provisioning, load balancing, auto-scaling to application health monitoring.
AWS Beanstalk Blue/Green deployment is a popular deployment strategy. It allows us to update our application on Elastic Beanstalk with zero downtime and minimal risk.
It follows the blue/green deployment model, where two separate environments run concurrently. Let’s take a look at how it works:
- Blue Environment:
- This is the currently running environment hosting our application.
- Green Environment:
- This represents the new environment created with the updated version of our application.
Furthermore, the Blue/Green deployment process includes these steps:
- First, we have to prepare the Green Environment.
- Then, we must test the Green Environment.
- After that, we will swap the URLs of the Blue and Green environments. In other words, the Green environment becomes the active environment while the Blue environment is deactivated.
- The next step is routing traffic to the new Green environment.
In case there are any issues arise after the swap, we can easily roll back by swapping the URLs again to revert to the previous Blue environment.
Benefits of Blue/Green Deployment
Some of the key advantages offered by AWS Blue Green Deployment include:
- Zero Downtime
- Rollback Capability
- Reduced Risk
- Easy Rollout
At the end of the day, the Blue/Green deployment strategy in AWS Beanstalk offers a robust and reliable approach for updating our application.
Base Scenario (Use Case)
AWS Elastic Beanstalk performs normal deployments in the current production environment. Due to this, the application will be unavailable to users until the update is complete. This downtime can be avoided by performing a blue/green deployment. In this scenario, we will do the following steps to overcome this issue
Before we start creating the Blue/Green deployments in Beanstalk, we need to create a policy and attach it to an IAM role. Please go to the IAM console and create the policy below:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "BucketAccess",
"Action": [
"s3:Get*",
"s3:List*",
"s3:PutObject"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "XRayAccess",
"Action": [
"xray:PutTraceSegments",
"xray:PutTelemetryRecords",
"xray:GetSamplingRules",
"xray:GetSamplingTargets",
"xray:GetSamplingStatisticSummaries"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "CloudWatchLogsAccess",
"Action": [
"logs:PutLogEvents",
"logs:CreateLogStream",
"logs:DescribeLogStreams",
"logs:DescribeLogGroups"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "ElasticBeanstalkHealthAccess",
"Action": [
"elasticbeanstalk:PutInstanceStatistics"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
Now create an EC2-role and attach the EC2-policy to it.
Now that the role is created, we can head over to the Elastic Beanstalk console.
Creating the application:
In the Elastic Beanstalk console, click on Create application.
On the next screen, select Web server environment and type in boon-app for the Application name.
For Platform type, leave Managed platform selected and select the latest PHP branch and recommended Platform version.
Leave the rest as default and click Next.
On the next screen, click on Create and use new service role and select the EC2-Role that was created earlier in the dropdown and click Next.
In the networking, database and tags screen, make sure to select your VPC, check the Public IP address box and select all subnets and click Next.
On the next screen scroll down to Instance types and remove the ones that are already there and select t2.nano from the dropdown. Now click on Next.
On the next configuration page, scroll down to Managed platform updates and uncheck the box. After this click Next to review then click on Submit.
Creating the Blue environment:
Click on the application name after it has finished creating.
Then click on Create environment.
In the Configure environment screen, choose Web server environment and type the name boon-Blue-Env
For Platform, choose PHP and just like we did when we created the App, choose the latest Platform branch and the recommended version then click Next.
In the Configure service access screen, select Use an existing service role and make sure the aws-elasticbeanstalk-service-role is selected. For EC2 instance profile, select EC2-role then click Next.
On the next screen select your VPC, check the box for Public IP address, select all subnets then click Next.
On the Configure instance traffic screen, Scroll down to Instance types and remove the instances there then add t2.nano and click Next.
On the following screen, uncheck Managed updates checkbox then click on Next. Then review and Submit. The environment will take anywhere from 5–10 minutes to complete.
When it completes, you will be be provided with a domain for the environment. Clicking on it will open the application.
Creating the Green environment:
Please follow the same steps as above to create the Green environment with the following 2 exceptions:
Environment name = boon-Green-Env
Under Managed platform, choose Node.js
Just like with the Blue environment, you will see a Domain URL when completed. Click on it when you are ready.
Swapping Domains from Blue to Green:
Now that we have 2 environments, we are going to swap environments. To do this please go to the Green environment dashboard and click on Actions and select Swap environment domain.
In the next screen, select the Blue environment from the dropdown and click on Swap.
Please give the swap a few seconds to complete. If you pay attention to the Events, you will see when the swap has been completed.
To test if the swap was successful, we can click on the Blue environment Domain URL. It should direct us to the Green environment (node.js).
conclusion:
By using blue green deployment with Elastic Beanstalk and AWS services, the company was able to deploy a new version of their application without causing any downtime or disruption to their customers. The deployment process was automated, which saved time and reduced the risk of errors. The company was also able to test the new version of the application before switching over to the green environment, which helped to ensure that everything was working correctly. Overall, the deployment was successful, and the company was able to update their application seamlessly.
thank you gor reading!!!