Dies ist der AWS CDK v2-Entwicklerhandbuch. Die ältere CDK Version 1 wurde am 1. Juni 2022 in die Wartung aufgenommen und der Support wurde am 1. Juni 2023 eingestellt.
Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Eine AWS Cloud Development Kit (AWS CDK) Phase stellt eine Gruppe von einem oder mehreren CDK Stacks dar, die so konfiguriert sind, dass sie zusammen bereitgestellt werden. Verwenden Sie Phasen, um dieselbe Gruppierung von Stacks in mehreren Umgebungen bereitzustellen, z. B. in der Entwicklungs-, Test- und Produktionsumgebung.
Um eine CDK Phase zu konfigurieren, importieren und verwenden Sie das Stage
Konstrukt.
Im Folgenden finden Sie ein einfaches Beispiel, das eine CDK Phase mit dem Namen definiertMyAppStage
. Wir fügen unserer Phase zwei CDK Stapel mit dem Namen AppStack
und DatabaseStack
hinzu. In diesem Beispiel AppStack
enthält es Anwendungsressourcen und DatabaseStack
Datenbankressourcen. Anschließend erstellen wir zwei Instanzen von MyAppStage
für Entwicklungs- und Produktionsumgebungen:
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'
}
});
Bei der Ausführung cdk synth
werden zwei Cloud-Assemblys in erstelltcdk.out
. Diese beiden Cloud-Assemblys enthalten die synthetisierte AWS CloudFormation Vorlage und die Assets für jede Phase. Das Folgende ist ein Auszug aus unserem Projektverzeichnis:
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
Wenn wir unsere Stapel mit auflistencdk list
, sehen wir insgesamt vier Stapel:
$
cdk list
Dev/AppStack (Dev-AppStack) Dev/DatabaseStack (Dev-DatabaseStack) Prod/AppStack (Prod-AppStack) Prod/DatabaseStack (Prod-DatabaseStack)
Um eine bestimmte Phase bereitzustellen, führen wir die Stacks aus cdk deploy
und stellen sie bereit. Im Folgenden finden Sie ein Beispiel, das den *
Platzhalter verwendet, um beide Stacks in unserer Phase bereitzustellen: Dev
$
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