Menu
AWS CodeDeploy
User Guide (API Version 2014-10-06)

Integrating AWS CodeDeploy with Elastic Load Balancing

Elastic Load Balancing provides two types of load balancers, Classic load balancers and Application load balancers:

  • A Classic load balancer makes routing and load balancing decisions either at the transport layer (TCP/SSL) or the application layer (HTTP/HTTPS). It supports either EC2-Classic or a VPC.

  • An Application load balancer makes routing and load balancing decisions at the application layer (HTTP/HTTPS) and supports path-based routing. It can route requests to ports on each EC2 instance or container instance in your virtual private cloud (VPC).

For general information about Elastic Load Balancing load balancers, see What Is Elastic Load Balancing?, What Is a Classic Load Balancer?, and What Is an Application Load Balancer?.

You can also set up deployments in AWS CodeDeploy with Amazon EC2 instances that are part of an Auto Scaling group. For more information, see Auto Scaling.

Load balancing for blue/green deployments

Rerouting instance traffic behind an Elastic Load Balancing load balancer is fundamental to AWS CodeDeploy blue/green deployments. As instances in a replacement environment are registered with a load balancer, instances from the original environment are deregistered and, if you choose, terminated.

For a blue/green deployment, you must use a Classic load balancer. You use the AWS CodeDeploy console or AWS CLI to add the load balancer to a deployment group.

For more information, see Set Up a Classic Load Balancer in Elastic Load Balancing for AWS CodeDeploy Deployments, Create an Application for a Blue/Green Deployment (Console), and Create a Deployment Group for a Blue/Green Deployment (Console).

Load balancing for in-place deployments

Using a load balancer with an in-place deployment allows AWS CodeDeploy to prevent internet traffic from being routed to instances that are currently being deployed to. If the deployment is not set up with a load balancer, internet traffic can still be directed to an instance during a deployment. Your customers might encounter broken, incomplete, or outdated web applications. When you use an Elastic Load Balancing load balancer with an in-place deployment, instances in a deployment group are taken offline, updated with the latest application revision, and then placed back online as part of the same deployment group. Specifically, each instance is deregistered from the load balancer at the start of its deployment process, and then re-registered after the deployment is successful.

Both Classic load balancers and Application load balancers are supported for in-place deployments. You can choose from the following options:

Use a script to set up a load balancer for an in-place deployment

Use the steps in the following procedure to use deployment lifecycle scripts to set up load balancing for in-place deployments.

In the AWS CodeDeploy Samples repository on GitHub, we provide instructions and samples you can adapt to use AWS CodeDeploy with the Classic load balancer and the Application load balancer. These repositories include three sample scripts—register_with_elb.sh, deregister_from_elb.sh, and common_functions.sh—that provide all of the code you need to get going. Simply edit the placeholders in these three scripts, and then reference these scripts from your appspec.yml file.

To set up in-place deployments in AWS CodeDeploy with Amazon EC2 instances that are registered with Elastic Load Balancing load balancers, do the following:

  1. Download the samples for the type of load balancer you want to use for an in-place deployment:

  2. Make sure each of your target Amazon EC2 instances has the AWS CLI installed.

  3. Make sure each of your target Amazon EC2 instances has an IAM instance profile attached with, at minimum, the elasticloadbalancing:* and autoscaling:* permissions.

  4. Include in your application's source code directory the deployment lifecycle event scripts (register_with_elb.sh, deregister_from_elb.sh, and common_functions.sh).

  5. In the appspec.yml for the application revision, provide instructions for AWS CodeDeploy to run the register_with_elb.sh script during the ApplicationStart event and the deregister_from_elb.sh script during the ApplicationStop event.

  6. If the instance is part of an Auto Scaling group, you can skip this step.

    In the common_functions.sh script:

    • If you are using the Classic load balancer, specify the names of the Elastic Load Balancing load balancers in ELB_LIST="", and make any changes you need to the other deployment settings in the file.

    • If you are using the Application load balancer, specify the names of the Elastic Load Balancing target group names in TARGET_GROUP_LIST="", and make any changes you need to the other deployment settings in the file.

  7. Bundle your application's source code, the appspec.yml, and the deployment lifecycle event scripts into an application revision, and then upload the revision. Deploy the revision to the Amazon EC2 instances. During the deployment, the deployment lifecycle event scripts will deregister the Amazon EC2 instance with the load balancers, wait for the connection to drain, and then re-register the Amazon EC2 instance with the load balancers after the deployment is complete.