Integrating AWS CodeDeploy with Auto Scaling
AWS CodeDeploy supports Auto Scaling, an AWS service that can launch Amazon EC2 instances automatically according to conditions you define. These conditions can include limits exceeded in a specified time interval for CPU utilization, disk reads or writes, or inbound or outbound network traffic. Auto Scaling terminates the instances when they are no longer needed. For more information, see What Is Auto Scaling?.
When new Amazon EC2 instances are launched as part of an Auto Scaling group, AWS CodeDeploy can deploy your revisions to the new instances automatically. You can also coordinate deployments in AWS CodeDeploy with Amazon EC2 instances registered with Elastic Load Balancing load balancers. For more information, see Elastic Load Balancing.
Be aware that you might encounter issues if you associate multiple deployment groups with a single Auto Scaling group. If one deployment fails, for example, the instance will begin to shut down, but the other deployments that were running can take an hour to time out. For more information, see Avoid associating multiple deployment groups with a single Auto Scaling group and Under the Hood: AWS CodeDeploy and Auto Scaling Integration.
Deploying AWS CodeDeploy Applications to Auto Scaling Groups
To deploy an AWS CodeDeploy application revision to an Amazon EC2 Auto Scaling group:
Create or locate an IAM instance profile that allows the Auto Scaling group to work with Amazon S3.
You can also use AWS CodeDeploy to deploy revisions from GitHub repositories to Auto Scaling groups. Although Amazon EC2 instances still require an IAM instance profile, the profile doesn't need any additional permissions to deploy from a GitHub repository. For more information, see Step 4: Create an IAM Instance Profile.
Create or use an Auto Scaling group, specifying the IAM instance profile.
Create or locate a service role that allows AWS CodeDeploy to create a deployment group that contains the Auto Scaling group.
Create a deployment group with AWS CodeDeploy, specifying the Auto Scaling group name and service role.
Use AWS CodeDeploy to deploy your revision to the deployment group that contains the Auto Scaling group.
For more information, see Tutorial: Deploy to an Auto Scaling Group.
Auto Scaling Behaviors with AWS CodeDeploy
The execution order of custom lifecycle hook events cannot be predetermined
You can add your own lifecycle hooks to Auto Scaling groups to which AWS CodeDeploy deploys.
However, the order in which those custom lifecycle hook events are executed cannot
be predetermined in relation to AWS CodeDeploy default deployment lifecycle events. For
example, if you add a custom lifecycle hook named
ReadyForSoftwareInstall to an Auto Scaling group, you cannot know
beforehand whether it will be executed before the first, or after the last, AWS CodeDeploy
default deployment lifecycle event.
To learn how to add custom lifecycle hooks to an Auto Scaling group, see Adding Lifecycle Hooks.
Deleting Auto Scaling groups causes deployment failures for associated deployment groups
If you add an Auto Scaling group to a deployment group, and then delete the Auto Scaling group, all future deployments to that deployment group will fail.
Using a Custom AMI with AWS CodeDeploy and Auto Scaling
You have two options for specifying the base AMI to use when new Amazon EC2 instances are launched in an Auto Scaling group:
You can specify a base custom AMI that already has the AWS CodeDeploy agent installed. Because the agent is already installed, this option launches new Amazon EC2 instances more quickly than the other option. However, this option provides a greater likelihood that initial deployments of Amazon EC2 instances will fail, especially if the AWS CodeDeploy agent is out of date. If you choose this option, we recommend you regularly update the AWS CodeDeploy agent in your base custom AMI.
You can specify a base AMI that doesn't have the AWS CodeDeploy agent installed and have the agent installed as each new instance is launched in an Auto Scaling group. Although this option launches new Amazon EC2 instances more slowly than the other option, it provides a greater likelihood that initial deployments of instances will succeed. This option uses the most recent version of the AWS CodeDeploy agent.