Get startedGet started for free

CodeDeploy fundamentals and appspec.yml

1. CodeDeploy fundamentals and appspec.yml

Welcome to Chapter 2. In Chapter 1 you built CI/CD pipelines that produce a build artifact. Now that artifact has to reach production safely. In this video, you'll learn how CodeDeploy works: its core components, the appspec.yml file, and lifecycle events for EC2 and Lambda.

2. What CodeDeploy does

CodeDeploy releases your code to compute targets like EC2 instances, Lambda functions, or ECS services. It starts by pulling the build artifact from a source such as S3, GitHub, or Bitbucket, then runs the deployment script you define through a sequence of lifecycle events. If any event fails along the way, CodeDeploy rolls back automatically to the previous version, so a bad release never stays live.

3. CodeDeploy core components

CodeDeploy has four building blocks, and a real release ties them together. Picture an application called "checkout-api". The deployment group "Env=Prod" selects every EC2 instance with that tag. The deployment configuration is CodeDeployDefault.OneAtATime, which rolls instances over one by one. The revision is "v1.2.zip" sitting in S3. Together, those four pieces describe what to deploy, where, and how.

4. Meet appspec.yml

The appspec.yml file is how you instruct CodeDeploy. It sits at the root of your revision bundle. The first line is always version: 0.0. That number is the appspec schema version that AWS defines; it has nothing to do with your application's version, and it is the only value allowed today, reserved for future schema changes. EC2 deployments also need an os field set to linux or windows. The file's two main jobs: tell CodeDeploy which files to copy from your bundle to disk, and tell CodeDeploy what scripts to run at each lifecycle event.

5. Lifecycle events on EC2

On EC2, a deployment runs through a sequence of lifecycle events. You write scripts for ApplicationStop, which gracefully stops the previous version, plus BeforeInstall, AfterInstall, ApplicationStart, and ValidateService. CodeDeploy itself runs DownloadBundle and Install for you, so you don't write scripts for those. Each hook script runs on the instance, in shell or PowerShell. ValidateService is the last hook; it's where you put smoke tests that decide whether the deployment succeeded.

6. Lifecycle events on Lambda

Lambda deployments work differently. There are only two hooks: BeforeAllowTraffic, which runs before traffic shifts, and AfterAllowTraffic, which runs after. Each hook here is itself a small Lambda function you write; with no server in the picture, there is nothing to run a shell script on. CodeDeploy shifts a Lambda alias from the old version to the new one based on your deployment configuration. There are no instances to log into and no files to copy; the function code is already uploaded to Lambda before the deployment starts.

7. A real appspec.yml for EC2

Here's a working appspec.yml for an EC2 web app. The header sets version 0.0 and os linux. The files section copies app.zip to /var/www/app. BeforeInstall stops the web server so files can be replaced safely, AfterInstall installs any new dependencies, and ApplicationStart kicks the web server back up. ValidateService runs a quick curl against the local endpoint to confirm the app is responding before the deployment is marked successful.

8. Let's practice!

Now let's build an appspec.yml of your own. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.