Lesson 1 of 5 AWS CodeDeploy

Understanding Applications and Deployment Groups in AWS CodeDeploy

AWS CodeDeploy automates the release of application revisions to compute targets and manages how quickly the new version replaces the old one. This lesson introduces the CodeDeploy resource model, the three compute platforms, the AppSpec file, deployment configurations and the roles involved, which later lessons build on for each platform.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the CodeDeploy resources: applications, compute platforms, deployment groups, deployment configurations and revisions.
  • Explain the purpose and structure of the AppSpec file for each compute platform.
  • Distinguish between the CodeDeploy service role, the EC2 instance profile and Lambda hook roles.
  • Explain how CodeDeploy integrates with CodePipeline, Auto Scaling, load balancers and CloudWatch alarms.

What CodeDeploy Does

Deploying a new version safely involves many steps: stopping the old version, copying files, setting permissions, starting the new version, checking that it works, controlling how many servers change at once and undoing the change if something goes wrong. CodeDeploy performs these steps consistently for every deployment. It does not build code or provision infrastructure; it takes a revision that already exists and installs it on targets that already exist, or that it creates as copies in a blue/green deployment. CodeDeploy has no additional charge for EC2, Lambda and ECS deployments; on-premises deployments are charged per instance update.

The Resource Model

CodeDeploy uses a small number of resources, and exam scenarios often hinge on where a setting is configured.

Application
A name that groups deployment groups and revisions for one piece of software. Each application has one compute platform: EC2/On-premises, AWS Lambda or Amazon ECS.
Deployment group
The target and the rules for deploying to it. For EC2, the targets are instances selected by tags or Auto Scaling groups; for Lambda, a function and alias; for ECS, a cluster and service. The group also holds the deployment type, the load balancer, alarms, triggers and rollback settings.
Deployment configuration
The rule that controls the pace of a deployment: how many instances change at once, or how traffic shifts between Lambda versions or ECS task sets. Predefined configurations exist, and custom ones can be created.
Revision
What is deployed: for EC2, an archive in S3 or a GitHub commit containing files and an AppSpec file; for Lambda and ECS, an AppSpec document that names the versions or task definition.
Deployment
One run of a revision to a deployment group with a deployment configuration.

One application usually has several deployment groups, such as test, staging and production, which allows the same revision to be deployed to each environment with different targets, configurations and alarms.

The following diagram shows how the CodeDeploy resources relate.

The CodeDeploy resource model. An application groups the software's deployment groups and revisions and has one compute platform: EC2/On-premises, AWS Lambda or Amazon ECS. Each deployment group holds the targets and the rules for deploying to them: deployment type, deployment configuration, load balancer, alarms, triggers and rollback settings. A deployment runs one revision against one deployment group with a deployment configuration, so the same revision can go to test, staging and production with different settings.
Most settings live on the deployment group.

The AppSpec File

The application specification file, appspec.yml (or JSON for Lambda and ECS), tells CodeDeploy what to deploy and which hooks to run. Its contents differ by platform.

  • EC2/On-premises: a files section mapping source paths in the revision to destination paths on the instance, a permissions section setting owners and modes, and a hooks section mapping lifecycle events to scripts in the revision, each with a timeout and the user to run as. The file must be in the root of the revision archive.
  • AWS Lambda: a resources section naming the function, alias, current version and target version, and a hooks section naming Lambda functions to run before and after traffic shifts.
  • Amazon ECS: a resources section naming the task definition, the container name and port that receive load balancer traffic and optional network settings, and hooks naming Lambda validation functions.

Hooks are the main extension point. They let a deployment stop services cleanly, run database migrations, warm caches and, most importantly, validate the new version. A hook that fails stops the deployment, which is how CodeDeploy measures health beyond simple load balancer checks.

Roles in a CodeDeploy Deployment

Several identities take part in a deployment, and permission problems are diagnosed by identifying which one made the failing call.

IdentityUsed byNeeds permission to
CodeDeploy service roleThe CodeDeploy serviceDescribe and tag instances, manage Auto Scaling groups and lifecycle hooks, register targets with load balancers, update ECS services, invoke hook functions, read alarms
EC2 instance profileThe CodeDeploy agent on each instanceRead the revision from S3 and decrypt it with KMS
Lambda hook execution roleValidation hook functionsCall the services the test uses and codedeploy:PutLifecycleEventHookExecutionStatus
Pipeline or user roleWhoever starts the deploymentcodedeploy:CreateDeployment and related read actions

AWS provides managed policies for the service role: AWSCodeDeployRole for EC2/on-premises, AWSCodeDeployRoleForLambda and AWSCodeDeployRoleForECS. When the EC2 deployment group uses launch templates that specify an instance profile, the service role also needs iam:PassRole and ec2:RunInstances, for example to create the replacement Auto Scaling group in a blue/green deployment.

Sort into groups

Which identity makes the call?

Exam skills 1.4.2 · 1.4.3

Decide which identity needs each permission in a CodeDeploy deployment.

Log in to try this activity Interactive activities are free with an account.

Integrations

CodeDeploy integrates with other services to automate the end-to-end release. The most important integrations are summarised below.

  • CodePipeline: a CodeDeploy or CodeDeployToECS deploy action starts deployments with the revision from the pipeline's artifacts.
  • Elastic Load Balancing: CodeDeploy deregisters instances from target groups before updating them and registers them afterwards, or shifts traffic between target groups for blue/green.
  • EC2 Auto Scaling: CodeDeploy installs a lifecycle hook on the Auto Scaling group, so instances launched by scale-out receive the last successful revision before entering service.
  • CloudWatch alarms: up to ten alarms on a deployment group stop the deployment when any enters the ALARM state and can trigger automatic rollback.
  • Notifications: deployment and instance state changes are published as EventBridge events and can be sent to SNS through triggers or notification rules.
Worked example

Structuring Applications and Groups

A company deploys an order service to EC2 in test and production, and a pricing function to Lambda. It wants test deployments to be fast and production deployments to change one third of the fleet at a time with automatic rollback.

The company creates two applications because a CodeDeploy application has a single compute platform: orders (EC2/On-premises) and pricing (Lambda). The orders application has a test deployment group with CodeDeployDefault.AllAtOnce and a production group targeting the production Auto Scaling group with a custom configuration whose minimum healthy hosts is 67%, two CloudWatch alarms and rollback on failure and alarm. The pricing application has a deployment group for the function's live alias with a canary configuration.

Check yourself

Can one CodeDeploy application contain a deployment group for EC2 instances and another for a Lambda function?

Show answer

No. The compute platform is a property of the application, so EC2/On-premises, Lambda and ECS deployments need separate applications.

Exam Tips

Remember for the exam

  • An application has one compute platform (EC2/On-premises, Lambda or ECS); deployment groups hold targets, alarms, load balancer and rollback settings.
  • The AppSpec file defines files, permissions and hooks for EC2, and versions or task definitions plus hook Lambda functions for Lambda and ECS.
  • The CodeDeploy service role manages Auto Scaling, load balancers and ECS; the instance profile is used by the agent to download revisions.
  • Up to ten CloudWatch alarms per deployment group can stop a deployment and trigger automatic rollback.