Deploying with CodePipeline Deploy Actions
CodePipeline can deploy to almost every AWS compute and infrastructure service through deploy action providers. Each provider expects particular input artifacts and configuration. This lesson explains the deploy actions for CloudFormation, CodeDeploy, Amazon ECS, Amazon EC2, Amazon EKS, AWS Lambda, Elastic Beanstalk, Amazon S3 and other services, so that you can choose and configure the right action for a scenario.
Learning outcomes
After completing this lesson, you will be able to:
- Explain the CloudFormation deploy action modes and how template configuration files supply parameters.
- Distinguish between the Amazon ECS standard deploy action and the ECS blue/green deploy action, including their input files.
- Describe the CodeDeploy, EC2, EKS, Lambda, Elastic Beanstalk, S3 and AppConfig deploy actions.
- Choose an appropriate deploy action for a given compute platform and release requirement.
AWS CloudFormation Deploy Actions
The CloudFormation action deploys infrastructure from a template in an input artifact. Its ActionMode decides what it does, and choosing the right mode is a frequent exam topic.
- CREATE_UPDATE: creates the stack if it does not exist or updates it directly. Simple, but changes are applied without a preview.
- CHANGE_SET_REPLACE: creates or replaces a change set without executing it. Used with a later CHANGE_SET_EXECUTE action, often with a manual approval between them so that a person can review exactly what will change.
- CHANGE_SET_EXECUTE: executes a named change set.
- REPLACE_ON_FAILURE: creates the stack, or deletes and recreates it if it is in a failed state. Suitable for test environments, never for production with data.
- DELETE_ONLY: deletes the stack, for example to remove a temporary test environment at the end of a pipeline.
Parameters can be supplied as ParameterOverrides in the action, which can reference variables, or in a template configuration file in the artifact. A template configuration file is a JSON file with Parameters, Tags and StackPolicy keys, typically one per environment, such as test.json and prod.json, which lets the same template be promoted with different settings. The function Fn::GetParam in overrides reads a value from a JSON file in an artifact, such as a build output that contains an image URI.
{
"Parameters": {"Environment": "prod", "InstanceType": "m7g.large", "DesiredCount": "6"},
"Tags": {"CostCentre": "4410", "Application": "orders"},
"StackPolicy": {"Statement": [{"Effect": "Deny", "Action": "Update:Replace", "Principal": "*",
"Resource": "LogicalResourceId/OrdersDatabase"},
{"Effect": "Allow", "Action": "Update:*", "Principal": "*", "Resource": "*"}]}
}The Capabilities setting must include CAPABILITY_IAM or CAPABILITY_NAMED_IAM when the template creates IAM resources, and CAPABILITY_AUTO_EXPAND when it uses macros or AWS SAM transforms. A separate CloudFormation StackSets deploy action deploys a template to stack instances across accounts and Regions.
Choose the ActionMode
Amazon ECS Deploy Actions
Two different actions deploy to Amazon ECS, and they expect different files.
Amazon ECS (standard) deploy action
This action performs an ECS rolling update, or uses whatever deployment controller the service has configured, by registering a new task definition revision with an updated image and updating the service. It reads an imagedefinitions.json file that lists container names and image URIs, as produced by the buildspec in the CodeBuild module. ECS native blue/green, linear and canary deployments, configured on the service itself, are also driven by this action, because the deployment strategy belongs to the service rather than to CodePipeline.
Amazon ECS (blue/green) deploy action
The CodeDeployToECS action uses AWS CodeDeploy to perform a blue/green deployment of an ECS service that uses the CodeDeploy deployment controller. It requires an imageDetail.json file containing the image URI, a task definition file (taskdef.json) with a placeholder such as <IMAGE1_NAME> that is replaced with the image, and an AppSpec file that names the container and port that receive traffic and optional lifecycle hook Lambda functions.
The difference in input files is summarised in the following table.
| Action | Deployment performed | Required input |
|---|---|---|
| Amazon ECS (standard) | Rolling update, or the service's native ECS blue/green, linear or canary strategy | imagedefinitions.json |
| Amazon ECS (blue/green) through CodeDeploy | CodeDeploy blue/green with traffic shifting | imageDetail.json, taskdef.json and appspec.yaml |
Mismatched image files
If a blue/green ECS action fails because it cannot find imageDetail.json, the build produced imagedefinitions.json for the standard action. The two actions are not interchangeable.
Deploy action inputs
CodeDeploy, EC2 and EKS Deploy Actions
The AWS CodeDeploy action starts a deployment to an existing CodeDeploy application and deployment group. The input artifact is the revision bundle containing the application files and an appspec.yml. It supports EC2 and on-premises in-place and blue/green deployments and Lambda deployments; the deployment configuration and rollback settings are defined on the deployment group. CodeDeploy is covered in its own module.
The Amazon EC2 deploy action deploys application files to EC2 instances identified by tags and runs scripts on them using AWS Systems Manager, with a configurable maximum batch size and failure threshold. It suits simple instance deployments without setting up a CodeDeploy application. The Amazon EKS deploy action applies Kubernetes manifests or a Helm chart to an EKS cluster, including private clusters, using an EKS access entry for the action role.
Lambda, Elastic Beanstalk, S3, AppConfig and Service Catalog
Several other deploy actions cover specific targets. They are described below with the scenario each one fits.
- AWS Lambda deploy action: updates a function's code and publishes a version, with traffic shifting between alias versions. For serverless applications defined in AWS SAM, the more common approach is a CloudFormation action deploying the transformed template, with SAM
DeploymentPreferencesettings that use CodeDeploy for canary or linear traffic shifting. - AWS Elastic Beanstalk: deploys an application version to an environment, which then applies the environment's deployment policy, such as rolling or immutable.
- Amazon S3: extracts and copies files to a bucket, for example for a static website served by Amazon CloudFront, with options for canned ACLs and cache control. Invalidating the CloudFront cache requires an additional step, such as a Lambda invoke or Commands action.
- AWS AppConfig: deploys a configuration file from the artifact to an AppConfig environment using an AppConfig deployment strategy.
- AWS Service Catalog: updates a product with a new provisioning artifact (version) from a template, so that end users can launch the new version.
Choosing a Deploy Action
The following comparison helps choose between the main deploy actions for application code. Infrastructure changes generally use the CloudFormation action regardless of the compute platform.
Reviewing Infrastructure Changes Before Production
A security team must review the exact list of resources that will change before any production stack update. Updates must not replace the production database.
The production stage contains three actions with increasing run order: a CloudFormation action in CHANGE_SET_REPLACE mode that creates a change set, a manual approval action whose SNS message links to the change set in the console, and a CloudFormation action in CHANGE_SET_EXECUTE mode. The template configuration file for production includes a stack policy that denies Update:Replace on the database resource, and the database resource also has DeletionPolicy and UpdateReplacePolicy set to Snapshot or Retain.
A pipeline must delete a temporary integration test stack after the tests pass. Which CloudFormation action mode should the final action use?
Show answer
DELETE_ONLY. It deletes the named stack and succeeds if the stack does not exist.
Exam Tips
Remember for the exam
- Use CHANGE_SET_REPLACE, a manual approval and CHANGE_SET_EXECUTE when changes must be reviewed before they are applied; CREATE_UPDATE applies changes directly.
- Template configuration files supply per-environment Parameters, Tags and a StackPolicy; Fn::GetParam reads values from JSON files in artifacts.
- The ECS standard action uses imagedefinitions.json; the ECS blue/green (CodeDeployToECS) action uses imageDetail.json, a task definition template and an AppSpec file.
- Templates that create IAM resources need CAPABILITY_IAM or CAPABILITY_NAMED_IAM; macros and SAM need CAPABILITY_AUTO_EXPAND.
- Deploying a static site with the S3 deploy action does not invalidate CloudFront; add a step for invalidation.