Lesson 2 of 5 AWS CI/CD Services

Deploying Applications with AWS CodeDeploy

This lesson explains how AWS CodeDeploy automates deployments to EC2 and on-premises servers, Lambda functions and ECS services, with automatic rollback.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how CodeDeploy works on each compute platform.
  • Describe the AppSpec file and the order of lifecycle hooks on EC2.
  • Distinguish canary, linear and all-at-once traffic shifting for Lambda and ECS.
  • Explain what triggers an automatic rollback.

Deployment targets

CodeDeploy deploys new versions of an application to three compute platforms, and the way it works differs for each.

On EC2 instances and on-premises servers, the CodeDeploy agent must run on every instance. The agent downloads the revision from S3 or GitHub and follows the appspec.yml file in it, which lists the files to copy and the scripts to run at lifecycle events such as BeforeInstall, AfterInstall and ValidateService. An in-place deployment updates the existing instances, a few at a time according to the deployment configuration, such as OneAtATime or HalfAtATime. A blue/green deployment installs the revision on new instances and then switches the load balancer to them, so the old instances remain available for a quick rollback.

For Lambda, no agent is needed: CodeDeploy moves the weights of an alias from the current version to the new one. For ECS, CodeDeploy starts a new task set with the new task definition and moves the load balancer's traffic from the old task set to the new one, which requires an Application or Network Load Balancer. ECS also offers native blue/green deployments without CodeDeploy. The following table summarises the platforms.

PlatformDeployment typesRequirements
EC2 and on-premisesIn-place or blue/greenThe CodeDeploy agent on each instance
LambdaTraffic shifting between versions behind an aliasNone
ECSBlue/green traffic shifting between task setsA load balancer

AppSpec and lifecycle hooks on EC2

An appspec.yml lists the files to copy and the scripts to run at each hook. On EC2, the hooks run in this order:

  1. ApplicationStop

    Stop the version that is currently running.

  2. BeforeInstall

    Prepare for installation, for example by backing up the current version.

  3. AfterInstall

    Configure the new files, for example by setting permissions. CodeDeploy copies the files between these two hooks.

  4. ApplicationStart

    Start the services that were stopped in ApplicationStop.

  5. ValidateService

    Check that the new version works before the deployment is marked as successful.

version: 0.0
os: linux
files:
  - source: /build
    destination: /var/www/orders
hooks:
  ApplicationStop:
    - location: scripts/stop.sh
      timeout: 60
  AfterInstall:
    - location: scripts/install_dependencies.sh
  ApplicationStart:
    - location: scripts/start.sh
  ValidateService:
    - location: scripts/health_check.sh
      timeout: 120

CodeDeploy also runs the DownloadBundle and Install events between ApplicationStop and AfterInstall; these are reserved for the agent and cannot run scripts. When the deployment group uses a load balancer, BeforeBlockTraffic, AfterBlockTraffic, BeforeAllowTraffic and AfterAllowTraffic surround the removal of each instance from, and its return to, the target group.

When an EC2 deployment fails, the usual causes are an agent that is not running, an instance profile that cannot read the revision from S3, or a hook script that fails or times out. The agent's logs on the instance show which.

Traffic shifting for Lambda and ECS

For Lambda and ECS, the deployment configuration decides how quickly traffic moves to the new version. The three types work as described in the lesson Releasing Function Changes with Versions and Aliases in the AWS Lambda module: a canary sends a small share of traffic first and the rest after a waiting period, a linear configuration moves traffic in equal steps, and all at once moves everything immediately. CodeDeploy watches CloudWatch alarms named in the deployment group throughout, and if one enters the ALARM state, it moves all traffic back to the original version automatically. The following table summarises the configurations.

ConfigurationBehaviour
Canary, e.g. Canary10Percent5Minutes10% of traffic for 5 minutes, then the rest
Linear, e.g. Linear10PercentEvery1MinuteEqual increments at regular intervals
All at onceEverything immediately

Lifecycle hooks run tests at fixed points, and a failed hook stops and rolls back the deployment. The following list describes the hooks.

  • Lambda hooks: BeforeAllowTraffic and AfterAllowTraffic run validation functions.
  • ECS hook: AfterAllowTestTraffic tests the new task set on a test listener before production traffic moves.
version: 0.0
Resources:
  - OrdersFunction:
      Type: AWS::Lambda::Function
      Properties:
        Name: orders-api
        Alias: live
        CurrentVersion: "5"
        TargetVersion: "6"
Hooks:
  - BeforeAllowTraffic: orders-pre-traffic-check
  - AfterAllowTraffic: orders-post-traffic-check

Automatic rollback

Deployments roll back automatically when they fail, or when a CloudWatch alarm attached to the deployment group fires. Rollback redeploys the last good revision.

Worked example

A deployment that fails on every instance

A CodeDeploy in-place deployment to ten EC2 instances fails within seconds on all of them. The developer works through the usual causes before looking at application code:

CheckHow
Is the CodeDeploy agent installed and running?sudo systemctl status codedeploy-agent
Can the instance profile read the revision?Instance role needs s3:GetObject on the artifact bucket
Is appspec.yml at the root of the bundle?Inspect the zip
Did a hook script fail?Logs under /opt/codedeploy-agent/deployment-root/

Here the instances were launched from a new AMI without the agent. Installing it through user data (or Systems Manager) fixes every instance. Failures on only some instances, by contrast, usually point to a hook script or the application.

Check yourself

Which deployment configuration sends 10% of a Lambda function's traffic to the new version for 5 minutes, then shifts the rest?

Show answer

CodeDeployDefault.LambdaCanary10Percent5Minutes. Linear configurations shift traffic in equal steps instead, and AllAtOnce shifts it immediately.

Exam Tips

Remember for the exam

  • Know the order of the EC2 lifecycle hooks.
  • A canary deployment shifts traffic in two steps; a linear deployment shifts equal increments at intervals.
  • To roll back automatically on errors, attach CloudWatch alarms to the deployment group.
  • To run smoke tests before traffic shifts to a new Lambda version, use the BeforeAllowTraffic hook.
  • An EC2 deployment that fails immediately points to the CodeDeploy agent or the instance profile's permissions.