Continuous Integration / Continuous Delivery for CDK Applications
This library includes a CodePipeline action for deploying AWS CDK Applications.
This module is part of the AWS Cloud Development Kit project.
Limitations
The construct library in it's current form has the following limitations:
- It can only deploy stacks that are hosted in the same AWS account and region as the CodePipeline is.
- Stacks that make use of
Asset
s cannot be deployed successfully.
Getting Started
In order to add the PipelineDeployStackAction
to your CodePipeline, you need to have a CodePipeline artifact that
contains the result of invoking cdk synth -o <dir>
on your CDK App. You can for example achieve this using a
CodeBuild project.
The example below defines a CDK App that contains 3 stacks:
CodePipelineStack
manages the CodePipeline resources, and self-updates before deploying any other stackServiceStackA
and ServiceStackB
are service infrastructure stacks, and need to be deployed in this order
┏━━━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Source ┃ ┃ Build ┃ ┃ Self-Update ┃ ┃ Deploy ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┌────────────┐ ┃ ┃ ┌────────────┐ ┃ ┃ ┌─────────────┐ ┃ ┃ ┌─────────────┐ ┌─────────────┐ ┃
┃ │ GitHub ┣━╋━━╋━▶ CodeBuild ┣━╋━━╋━▶Deploy Stack ┣━╋━━╋━▶Deploy Stack ┣━▶Deploy Stack │ ┃
┃ │ │ ┃ ┃ │ │ ┃ ┃ │PipelineStack│ ┃ ┃ │ServiceStackA│ │ServiceStackB│ ┃
┃ └────────────┘ ┃ ┃ └────────────┘ ┃ ┃ └─────────────┘ ┃ ┃ └─────────────┘ └─────────────┘ ┃
┗━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
index.ts
import codebuild = require('@aws-cdk/aws-codebuild');
import codepipeline = require('@aws-cdk/aws-codepipeline');
import cdk = require('@aws-cdk/cdk');
import cicd = require('@aws-cdk/cicd');
const app = new cdk.App();
const pipelineStack = new cdk.Stack(app, 'PipelineStack');
const pipeline = new codepipeline.Pipeline(pipelineStack, 'CodePipeline', {
restartExecutionOnUpdate: true,
});
const source = new codepipeline.GitHubSourceAction(pipelineStack, 'GitHub', {
stage: pipeline.addStage('source'),
});
const project = new codebuild.PipelineProject(pipelineStack, 'CodeBuild', {
});
const synthesizedApp = project.outputArtifact;
const selfUpdateStage = pipeline.addStage('SelfUpdate');
new cicd.PipelineDeployStackAction(pipelineStack, 'SelfUpdatePipeline', {
stage: selfUpdateStage,
stack: pipelineStack,
inputArtifact: synthesizedApp,
});
const deployStage = pipeline.addStage('Deploy');
const serviceStackA = new MyServiceStackA(app, 'ServiceStackA', { });
const serviceStackB = new MyServiceStackB(app, 'ServiceStackB', { });
new cicd.PipelineDeployStackAction(pipelineStack, 'DeployServiceStackA', {
stage: deployStage,
stack: serviceStackA,
inputArtifact: synthesizedApp,
});
new cicd.PipelineDeployStackAction(pipelineStack, 'DeployServiceStackB', {
stage: deployStage,
stack: serviceStackB,
inputArtifact: synthesizedApp,
createChangeSetRunOrder: 998,
});
buildspec.yml
The PipelineDeployStackAction
expects it's inputArtifact
to contain the result of synthesizing a CDK App using the
cdk synth -o <directory>
command.
For example, a TypeScript or Javascript CDK App can add the following buildspec.yml
at the root of the repository
configured in the Source
stage:
version: 0.2
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
- npm run cdk synth -- -o dist
artifacts:
base-directory: dist
files: '**/*'