Integrating AWS CodeDeploy with Elastic Load Balancing
Elastic Load Balancing is an AWS service that automatically distributes incoming application traffic across multiple Amazon EC2 instances in multiple Availability Zones. Both types of load balancers supported by Elastic Load Balancing (Classic and Application) can be used in AWS CodeDeploy in-place deployments. In blue/green deployments, a Classic load balancer is required.
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 more information about Elastic Load Balancing, see the following topics:
For information about using Elastic Load Balancing load balancers in blue/green deployments, see Create a Deployment.
When you run an application, including a web service, your Amazon EC2 instances will most likely be registered with Elastic Load Balancing load balancers. When you use AWS CodeDeploy to deploy code to one of those Amazon EC2 instances, you don't want the load balancers to send traffic to the instance during the deployment. You can use deployment lifecycle event scripts in AWS CodeDeploy to deploy code to Amazon EC2 instances that are registered with load balancers.
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.
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—
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
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:
Download the samples for the type of load balancer you want to use for an in-place deployment:
Make sure each of your target Amazon EC2 instances has the AWS CLI installed.
Make sure each of your target Amazon EC2 instances has an IAM instance profile attached with, at minimum, the elasticloadbalancing:* and autoscaling:* permissions.
Include in your application's source code directory the deployment lifecycle event scripts (
appspec.ymlfor the application revision, provide instructions for AWS CodeDeploy to run the
register_with_elb.shscript during the ApplicationStart event and the
deregister_from_elb.shscript during the ApplicationStop event.
If the instance is part of an Auto Scaling group, you can skip this step.
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.
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.