These artifacts are associated with the blog post on Blue/Green Deployments with CodeDeploy located at:
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.
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.