在CodeDeploy发生部署时,使用ELB,可以防止实例正在部署应用时接入流量。

如果使用蓝绿部署,则必须使用ELB! 对于in-place部署,ELB是可选项

Blue/Green deployments

在发生蓝绿部署时,当新的版本部署完成后,ELB将流量转发到新的实例上,并且block原来版本的流量,将原来版本的机器从ELB上摘掉

In-place deployments

如果在in-place部署时没有使用ELB,在发生部署时(部署未完成),用户可能会刷到不完整的应用、旧版本的应用。如果使用ELB,则先将旧版本的机器摘掉,再把新版本的机器注册到上面

Specify in-place deployment load balancer using a script

In the CodeDeploy Samples repository on GitHub, we provide instructions and samples you can adapt to use CodeDeploy Elastic Load Balancing load balancers. 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 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 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 Amazon EC2 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 or Network 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.