AWS DevOps Blog
Performing Blue/Green Deployments with AWS CodeDeploy and Auto Scaling Groups
Jeff Levine is a Solutions Architect for Amazon Web Services.
Amazon Web Services offers services that enable organizations to leverage the power of the cloud for their development and deployment needs. AWS CodeDeploy makes it possible to automate the deployment of code to either Amazon EC2 or on-premises instances. AWS CodeDeploy now supports blue/green deployments. In this blog post, I will discuss the benefits of blue/green deployments and show you how to perform one.
The benefits of blue/green deployments
Blue/green deployment involves two production environments:
- Blue is the active environment.
- Green is for the release of a new version.
Here are some of the advantages of a blue/green deployment:
- You can perform testing on the green environment without disrupting the blue environment.
- Switching to the green environment involves no downtime. It only requires the redirecting of user traffic.
- Rolling back from the green environment to the blue environment in the event of a problem is easier because you can redirect traffic to the blue environment without having to rebuild it.
- You can incorporate the principle of infrastructure immutability by provisioning fresh instances when you need to make changes. In this way, you avoid configuration drift.
AWS CodeDeploy offers two ways to perform blue/green deployments:
- In the first approach, AWS CodeDeploy makes a copy of an Auto Scaling group. It, in turn, provisions new Amazon EC2 instances, deploys the application to these new instances, and then redirects traffic to the newly deployed code.
- In the second approach, you use instance tags or an Auto Scaling group to select the instances that will be used for the green environment. AWS CodeDeploy then deploys the code to the tagged instances.
So how do you set up your first blue environment? A best practice is to start with an in-place deployment. You can also start with an existing, empty Auto Scaling group.
An example of blue/green deployments
Let’s take a look at an example of how to use Auto Scaling groups to perform a blue/green deployment.
Overview
In the following figure, the example environment includes an Amazon EC2 instance that serves as a workstation for AWS CodeDeploy. A release manager or developer could use this workstation to deploy new versions of code. The blue environment consists of an Auto Scaling group that provisions two more instances to function as web servers. The web servers will initially contain the first version of an application and the AWS CodeDeploy agent. A load balancer directs traffic to the two web servers in a round-robin manner.
The release manager uses the workstation instance to push a new version of the application to AWS CodeDeploy and starts a blue-green deployment. AWS CodeDeploy creates a copy of the Auto Scaling group. It launches two new web server instances just like the original two. AWS CodeDeploy installs the new version of the application and then redirects the load balancer to the new instances. The original instances continue to be part of the original Auto Scaling group. They can be reattached to the load balancer, if needed.
Prerequisites for building the example
Here are the things you will need to build out this example.
- An IAM user with permissions to use Amazon EC2, Amazon S3, Amazon VPC, AWS CodeDeploy, and AWS CloudFormation.
- An AWS region and Availability Zone in which you can provision the environment.
- An Amazon EC2 key pair.
- Working knowledge of the aforementioned services and the AWS Management Console, and familiarity with connecting to an Amazon EC2 instance.
Other Considerations
You will incur charges from AWS for the use of the underlying AWS services in this example. The Amazon EC2 t2.micro instances and Amazon S3 storage might be covered under the AWS Free Tier, depending on your eligibility. The resources provided in this example are for training purposes. Be sure to consider the security needs of your organization when implementing techniques similar to those described in this blog post.
Step 1: Create the initial environment
- Download an archive containing the sample template from this location and save it in a convenient location.
- Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation/.
- If this is a new AWS CloudFormation account, click Create New Stack. Otherwise, click Create Stack.
- Under Upload a template to Amazon S3, click Choose File, choose the YAML file from the archive you downloaded, and then click Next.
- In Specify Details, in Stack name, type
bluegreen
. - In AZName, select one of the Availability Zones. (In this blog post, I am using us-east-1a.)
- In BlueGreenKeyPairName, select the key pair to use.
- In NamePrefix, use the default value of
bluegreen
unless you are already running an application with a name that starts withbluegreen
. The name prefix is used to assign name tags to the created resources. Click Next. - On the Options page, click Next.
- Select the acknowledgement box to allow the creation of IAM resources, and then click Create. It will take CloudFormation about 10 minutes to create the sample environment. In addition to creating the infrastructure resources shown in the diagram, the CloudFormation template also sets up an AWS CodeDeploy application and blue/green deployment group.
Step 2: Review initial environment
- Look at the CloudFormation stack outputs. You should see something similar to the following. WorkstationIP is the IP address of the workstation instance. AutoScalingGroup and LoadBalancer are the DNS names created by CloudFormation for the Auto Scaling group and the Elastic Load Balancing load balancer.
- Copy the LoadBalancer value into your browser and browse to that link. The following application should be displayed. This PHP application queries the Amazon EC2 instance metadata. If you refresh the page, you will see the IP address and instance ID change in accordance with the round-robin load balancing algorithm.
- Go to the EC2 console and display the instances. You will see three running instances associated with this example: the workstation and the two web server instances created by the Auto Scaling group. The web server instances make up the blue environment.
Step 3: Deploy the new version of code
- Connect to the workstation instance at the address displayed in WorkStationIP. This instance is running the Ubuntu operating system, so the user name is ubuntu. After you sign in, you will see two directories. The scripts directory contains Bourne shell scripts. The newversion directory contains an update to the PHP application.
- Here is the PHP code for the new version in newversion/content/index.php. The only difference from the initially installed code is the application version number.
- Now look at the following scripts/pushnewversion.sh shell script. It uses the aws deploy push command to bundle the code and upload it to Amazon S3.
- Run the pushnewversion.sh script. You will see a message that tells you how to deploy the code with the AWS command line interpreter, but we will use the AWS CodeDeploy console to do this instead.
- Open the AWS CodeDeploy console at https://console.aws.amazon.com/codedeploy.
- Click the link for bluegreen-app. If you chose a name other than the default for NamePrefix, click that name instead. Expand Revisions. You will see the revision you just pushed from the AWS CodeDeploy workstation. Click Deploy revision.
- On the Create deployment page, select the bluegreen-app application and the bluegreen-dg deployment group. Leave all the other default values in place, and then click Deploy. AWS CodeDeploy will provision the Auto Scaling group and instances, deploy the code, set up health checks, and redirect traffic to the new instances. This process will take a few minutes. When the deployment is complete, the deployment should appear, as shown here. AWS CodeDeploy skips the termination of the original instances because of the settings in the deployment group.
Step 4: Review the updated environment
- Browse to the DNS name for the load balancer. You should see the new version of the application, as shown here. The application version has changed from 1 to 2, as expected.
- Go to the EC2 console and display the instances. You will see four instances that have been tagged by the Auto Scaling group and launch configuration. The instances with IP addresses 10.200.11.11 and 10.200.11.192 are the ones we saw before in the blue environment. The deployment process created the instances with IP addresses 10.200.11.13 and 10.200.22 that are now part of the green environment.
- Go to the Auto Scaling console. You will see that there are now two Auto Scaling groups, each of which has two instances. The Auto Scaling group whose names begins with CodeDeploy was created during the deployment process.
You have now successfully completed a blue/green deployment using AWS CodeDeploy.
Step 5: Cleanup
- Return to the session on the AWS CodeDeploy workstation.
- Run the scripts/cleanup.sh script. This will remove the deployment bundle and shut down the Auto Scaling groups.
- Go to the CloudFormation console, select the stack you created, and delete it.
Conclusion
AWS CodeDeploy enables developers to automate code deployments to Amazon EC2 and on-premises instances. The blue/green deployment option enables release managers to create a new production environment and makes it easier to roll back to the previous environment if problems arise. For more information about AWS CodeDeploy, see the AWS CodeDeploy documentation. You can get started in just a few clicks.
Enjoy life in the blue/green world!