liveBook · Manning

CodeDeploy

As explained in Amazon Web Services in Action, Third Edition: An in-depth guide to AWS

AWS CodeDeploy is a deployment automation service that coordinates application revisions across compute environments.

AWS CodeDeploy automates application deployments to EC2 instances, Fargate, Lambda, and on-premises machines. For EC2, it can update applications in place: the instances keep running while an installed agent carries out the deployment. This is useful when replacing a machine would be slow or would disturb data stored on it. section 15.1

A deployment starts with a revision: a ZIP archive containing application code and deployment instructions. An engineer selects that revision and its target instances. The EC2 agent pulls tasks from CodeDeploy, downloads the archive from S3, runs its instructions, and reports status. section 15.1

Four terms organize a CodeDeploy rollout:

  • Application: a name and compute platform. section 15.1
  • Deployment group: the targets, such as an Auto Scaling group. section 15.1
  • Revision: the code to deploy, referenced from S3 or GitHub. section 15.1
  • Deployment: the rollout of one revision to a deployment group. section 15.1
Listing 15.2 The AppSpec file used to deploy Etherpad with the help of CodeDeploy
version: 0.0                            #1
os: linux                               #2
files:                                  #3
- source: .
  destination: /etherpad
hooks:                                  #4
  BeforeInstall:                        #5
  - location: hook_before_install.sh
    timeout: 60
  AfterInstall:                         #6
  - location: hook_after_install.sh
    timeout: 60
  ApplicationStart:                     #7
  - location: hook_application_start.sh
    timeout: 180
    runas: ec2-user
  ValidateService:                      #8
  - location: hook_validate_service.sh
    timeout: 300
    runas: ec2-user

The Etherpad AppSpec file shows where files are copied and which scripts run before installation, after installation, at application start, and during service validation. section 15.1

Deploy an Etherpad revision to EC2

Set up the web application's infrastructure with CloudFormation: an Application Load Balancer, an Auto Scaling group, and an RDS database instance. Define a CodeDeploy application and a deployment group targeting the Auto Scaling group. Then upload the Etherpad ZIP to the artifact bucket and create a deployment with `aws deploy create-deployment --application-name etherpad-codedeploy --deployment-group-name etherpad-codedeploy --revision "revisionType=S3,s3Location={bucket=$BucketName,key=etherpad-lite-1.8.17.zip,bundleType=zip}"`. Check progress with `aws deploy get-deployment --deployment-id $DeploymentId`. To release the next version, upload its ZIP and create another deployment referencing the new key. section 15.1

Choose a deployment configuration to control how many targets update together: all at once, half at a time, or one at a time. CodeDeploy's in-place approach is fast and accommodates stateful machines, but successive changes can accumulate on them and make a deployment difficult to reproduce. CloudFormation with user data instead launches new machines; Packer packages the application into an AMI for immutable servers. CodeDeploy is free for deployments to EC2, Fargate, and Lambda; on-premises machine updates cost $0.02 each. section 15.1 section 15.4

Table 15.2 Differences between CodeDeploy, CloudFormation and user data, and Packer (view table figure)

 

AWS CodeDeploy

AWS CloudFormation and user data

Packer

Deployment speed

Fast

Slow

Medium

Agility

Medium

High

Low

Advantages

In-place deployments work for stateful machines as well.

Changing the user data script is a flexible way to deploy changes.

Machines are starting really fast, which is important if you want to scale based on demand. Also, you have low risk of failures during the boot process.

Disadvantages

Changes pile up on machines, which make it difficult to reproduce a deployment.

Potential failures during the boot process exist, for example, when third parties like GitHub are down.

Handling the life cycle of AMIs is tricky because you have to clean up unused and old AMIs to avoid storage costs.

Compare CodeDeploy's speed and stateful-machine advantage with the flexibility of CloudFormation and user data and the immutable-server approach enabled by Packer. section 15.4

How Amazon Web Services in Action, Third Edition: An in-depth guide to AWS covers it

How deep it goes
Works through EC2 agent behavior, deployment components, AppSpec hooks, CLI rollout, and configuration choices; does not work through Fargate or Lambda deployments.
Examples
Uses an Etherpad web application, a CloudFormation stack, ZIP revisions, an AppSpec file, and AWS CLI deployment commands.
What it assumes
Assumes basic knowledge of EC2 Auto Scaling and Elastic Load Balancing, and makes extensive use of CloudFormation.
What you can do after
Apply it via AWS CLI and CloudFormation to deploy and check an EC2 application revision, then compare in-place and replacement-based approaches.
How it connects
Connects deployments to EC2, Auto Scaling, S3, CloudFormation, Application Load Balancers, and immutable AMIs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Amazon Web Services in Action, Third Edition: An in-depth guide to AWS. The section links point to the parts of the book it draws on.