This is the AWS CDK v2 Developer Guide. The older CDK v1 entered maintenance on June 1, 2022 and ended support on June 1, 2023.
An AWS Cloud Development Kit (AWS CDK) stage represents a group of one or more CDK stacks that are configured to deploy together. Use stages to deploy the same grouping of stacks to multiple environments, such as development, testing, and production.
To configure a CDK stage, import and use the Stage
construct.
The following is a basic example that defines a CDK stage named MyAppStage
. We add two CDK
stacks, named AppStack
and DatabaseStack
to our stage. For this example, AppStack
contains application resources and DatabaseStack
contains database resources. We then create two instances
of MyAppStage
, for development and production environments:
In cdk-demo-app/lib/app-stack.ts
:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
// Define the app stack
export class AppStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your application goes here
}
}
In cdk-demo-app/lib/database-stack.ts
:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
// Define the database stack
export class DatabaseStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your database goes here
}
}
In cdk-demo-app/lib/my-stage.ts
:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Stage } from 'aws-cdk-lib';
import { AppStack } from './app-stack';
import { DatabaseStack } from './database-stack';
// Define the stage
export class MyAppStage extends Stage {
constructor(scope: Construct, id: string, props?: cdk.StageProps) {
super(scope, id, props);
// Add both stacks to the stage
new AppStack(this, 'AppStack');
new DatabaseStack(this, 'DatabaseStack');
}
}
In cdk-demo-app/bin/cdk-demo-app.ts
:
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { MyAppStage } from '../lib/my-stage';
// Create a CDK app
const app = new cdk.App();
// Create the development stage
new MyAppStage(app, 'Dev', {
env: {
account: '123456789012',
region: 'us-east-1'
}
});
// Create the production stage
new MyAppStage(app, 'Prod', {
env: {
account: '098765432109',
region: 'us-east-1'
}
});
When we run cdk synth
, two cloud assemblies are created in cdk.out
. These two cloud
assemblies contain the synthesized AWS CloudFormation template and assets for each stage. The following is snippet of our project
directory:
cdk-demo-app ├── bin │ └── cdk-demo-app.ts ├── cdk.out │ ├── assembly-Dev │ │ ├── DevAppStack
unique-hash
.assets.json │ │ ├── DevAppStackunique-hash
.template.json │ │ ├── DevDatabaseStackunique-hash
.assets.json │ │ ├── DevDatabaseStackunique-hash
.template.json │ │ ├── cdk.out │ │ └── manifest.json │ ├── assembly-Prod │ │ ├── ProdAppStackunique-hash
.assets.json │ │ ├── ProdAppStackunique-hash
.template.json │ │ ├── ProdDatabaseStackunique-hash
.assets.json │ │ ├── ProdDatabaseStackunique-hash
.template.json │ │ ├── cdk.out │ │ └── manifest.json └── lib ├── app-stack.ts ├── database-stack.ts └── my-stage.ts
When we list our stacks with cdk list
, we see a total of four stacks:
$
cdk list
Dev/AppStack (Dev-AppStack) Dev/DatabaseStack (Dev-DatabaseStack) Prod/AppStack (Prod-AppStack) Prod/DatabaseStack (Prod-DatabaseStack)
To deploy a specific stage, we run cdk deploy
and provide the stacks to deploy. The following is an
example that uses the *
wildcard to deploy both stacks in our Dev
stage:
$
cdk deploy "Dev/*"
✨ Synthesis time: 3.18s Dev/AppStack (Dev-AppStack) Dev/AppStack (Dev-AppStack): deploying... [1/2] ✅ Dev/AppStack (Dev-AppStack) ✨ Deployment time: 1.11s Stack ARN:...
✨ Total time: 4.29s Dev/DatabaseStack (Dev-DatabaseStack) Dev/DatabaseStack (Dev-DatabaseStack): deploying... [2/2] ✅ Dev/DatabaseStack (Dev-DatabaseStack) ✨ Deployment time: 1.09s Stack ARN:...
✨ Total time: 4.27s