選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

AWS CDK 階段簡介

焦點模式
AWS CDK 階段簡介 - AWS Cloud Development Kit (AWS CDK) v2

這是 AWS CDK v2 開發人員指南。較舊的 CDK v1 已於 2022 年 6 月 1 日進入維護,並於 2023 年 6 月 1 日結束支援。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

這是 AWS CDK v2 開發人員指南。較舊的 CDK v1 已於 2022 年 6 月 1 日進入維護,並於 2023 年 6 月 1 日結束支援。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS Cloud Development Kit (AWS CDK) 階段代表一或多個 CDK 堆疊的群組,這些堆疊設定為一起部署。使用階段將相同的堆疊群組部署到多個環境,例如開發、測試和生產。

若要設定 CDK 階段,請匯入並使用 Stage 建構。

以下是定義名為 之 CDK 階段的基本範例MyAppStage。我們新增兩個 CDK 堆疊,名為 AppStackDatabaseStack到我們的階段。在此範例中, AppStack包含應用程式資源, DatabaseStack包含資料庫資源。然後MyAppStage,我們為開發和生產環境建立兩個 執行個體:

TypeScript

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 } }

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 } }

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'); } }

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' } });
JavaScript

cdk-demo-app/lib/app-stack.js 中:

const { Stack } = require('aws-cdk-lib'); class AppStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // The code that defines your application goes here } } module.exports = { AppStack }

cdk-demo-app/lib/database-stack.js 中:

const { Stack } = require('aws-cdk-lib'); class DatabaseStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // The code that defines your database goes here } } module.exports = { DatabaseStack }

cdk-demo-app/lib/my-stage.js 中:

const { Stage } = require('aws-cdk-lib'); const { AppStack } = require('./app-stack'); const { DatabaseStack } = require('./database-stack'); // Define the stage class MyAppStage extends Stage { constructor(scope, id, props) { super(scope, id, props); // Add both stacks to the stage new AppStack(this, 'AppStack'); new DatabaseStack(this, 'DatabaseStack'); } } module.exports = { MyAppStage };

cdk-demo-app/bin/cdk-demo-app.js 中:

#!/usr/bin/env node const cdk = require('aws-cdk-lib'); const { MyAppStage } = require('../lib/my-stage'); // Create the 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', }, });
Python

cdk-demo-app/cdk_demo_app/app_stack.py 中:

from aws_cdk import Stack from constructs import Construct # Define the app stack class AppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # The code that defines your application goes here

cdk-demo-app/cdk_demo_app/database_stack.py 中:

from aws_cdk import Stack from constructs import Construct # Define the database stack class DatabaseStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # The code that defines your database goes here

cdk-demo-app/cdk_demo_app/my_stage.py 中:

from aws_cdk import Stage from constructs import Construct from .app_stack import AppStack from .database_stack import DatabaseStack # Define the stage class MyAppStage(Stage): def __init__(self, scope: Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) # Add both stacks to the stage AppStack(self, "AppStack") DatabaseStack(self, "DatabaseStack")

cdk-demo-app/app.py 中:

#!/usr/bin/env python3 import os import aws_cdk as cdk from cdk_demo_app.my_stage import MyAppStage # Create a CDK app app = cdk.App() # Create the development stage MyAppStage(app, 'Dev', env=cdk.Environment(account='123456789012', region='us-east-1'), ) # Create the production stage MyAppStage(app, 'Prod', env=cdk.Environment(account='098765432109', region='us-east-1'), ) app.synth()
Java

cdk-demo-app/src/main/java/com/myorg/AppStack.java 中:

package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; public class AppStack extends Stack { public AppStack(final Construct scope, final String id) { this(scope, id, null); } public AppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // The code that defines your application goes here } }

cdk-demo-app/src/main/java/com/myorg/DatabaseStack.java 中:

package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; public class DatabaseStack extends Stack { public DatabaseStack(final Construct scope, final String id) { this(scope, id, null); } public DatabaseStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // The code that defines your database goes here } }

cdk-demo-app/src/main/java/com/myorg/MyAppStage.java 中:

package com.myorg; import software.amazon.awscdk.Stage; import software.amazon.awscdk.StageProps; import software.constructs.Construct; // Define the stage public class MyAppStage extends Stage { public MyAppStage(final Construct scope, final String id, final software.amazon.awscdk.Environment env) { super(scope, id, StageProps.builder().env(env).build()); // Add both stacks to the stage new AppStack(this, "AppStack"); new DatabaseStack(this, "DatabaseStack"); } }

cdk-demo-app/src/main/java/com/myorg/CdkDemoAppApp.java 中:

package com.myorg; import software.amazon.awscdk.App; import software.amazon.awscdk.Environment; import software.amazon.awscdk.StackProps; import java.util.Arrays; public class CdkDemoAppApp { public static void main(final String[] args) { // Create a CDK app App app = new App(); // Create the development stage new MyAppStage(app, "Dev", Environment.builder() .account("123456789012") .region("us-east-1") .build()); // Create the production stage new MyAppStage(app, "Prod", Environment.builder() .account("098765432109") .region("us-east-1") .build()); app.synth(); } }
C#

cdk-demo-app/src/CdkDemoApp/AppStack.cs 中:

using Amazon.CDK; using Constructs; namespace CdkDemoApp { public class AppStack : Stack { internal AppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // The code that defines your application goes here } } }

cdk-demo-app/src/CdkDemoApp/DatabaseStack.cs 中:

using Amazon.CDK; using Constructs; namespace CdkDemoApp { public class DatabaseStack : Stack { internal DatabaseStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // The code that defines your database goes here } } }

cdk-demo-app/src/CdkDemoApp/MyAppStage.cs 中:

using Amazon.CDK; using Constructs; namespace CdkDemoApp { // Define the stage public class MyAppStage : Stage { internal MyAppStage(Construct scope, string id, Environment env) : base(scope, id, new StageProps { Env = env }) { // Add both stacks to the stage new AppStack(this, "AppStack"); new DatabaseStack(this, "DatabaseStack"); } } }

cdk-demo-app/src/CdkDemoApp/program.cs 中:

using Amazon.CDK; using System; using System.Collections.Generic; using System.Linq; namespace CdkDemoApp { sealed class Program { public static void Main(string[] args) { // Create a CDK app var app = new App(); // Create the development stage new MyAppStage(app, "Dev", new Amazon.CDK.Environment { Account = "123456789012", Region = "us-east-1" }); // Create the production stage new MyAppStage(app, "Prod", new Amazon.CDK.Environment { Account = "098765432109", Region = "us-east-1" }); app.Synth(); } } }
Go

cdk-demo-app/cdk-demo-app.go 中:

package main import ( "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) // Define the app stack type AppStackProps struct { awscdk.StackProps } func NewAppStack(scope constructs.Construct, id string, props *AppStackProps) awscdk.Stack { stack := awscdk.NewStack(scope, &id, &props.StackProps) // The code that defines your application goes here return stack } // Define the database stack type DatabaseStackProps struct { awscdk.StackProps } func NewDatabaseStack(scope constructs.Construct, id string, props *DatabaseStackProps) awscdk.Stack { stack := awscdk.NewStack(scope, &id, &props.StackProps) // The code that defines your database goes here return stack } // Define the stage type MyAppStageProps struct { awscdk.StageProps } func NewMyAppStage(scope constructs.Construct, id string, props *MyAppStageProps) awscdk.Stage { stage := awscdk.NewStage(scope, &id, &props.StageProps) // Add both stacks to the stage NewAppStack(stage, "AppStack", &AppStackProps{ StackProps: awscdk.StackProps{ Env: props.Env, }, }) NewDatabaseStack(stage, "DatabaseStack", &DatabaseStackProps{ StackProps: awscdk.StackProps{ Env: props.Env, }, }) return stage } func main() { defer jsii.Close() // Create a CDK app app := awscdk.NewApp(nil) // Create the development stage NewMyAppStage(app, "Dev", &MyAppStageProps{ StageProps: awscdk.StageProps{ Env: &awscdk.Environment{ Account: jsii.String("123456789012"), Region: jsii.String("us-east-1"), }, }, }) // Create the production stage NewMyAppStage(app, "Prod", &MyAppStageProps{ StageProps: awscdk.StageProps{ Env: &awscdk.Environment{ Account: jsii.String("098765432109"), Region: jsii.String("us-east-1"), }, }, }) app.Synth(nil) } func env() *awscdk.Environment { return nil }

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 } }

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 } }

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'); } }

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' } });

執行 時cdk synth,會在 中建立兩個雲端組件cdk.out。這兩個雲端組件包含每個階段的合成 AWS CloudFormation 範本和資產。以下是我們專案目錄的程式碼片段:

TypeScript
cdk-demo-app ├── bin │   └── cdk-demo-app.ts ├── cdk.out │   ├── assembly-Dev │   │   ├── DevAppStackunique-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
JavaScript
cdk-demo-app ├── bin │   └── cdk-demo-app.js ├── cdk.out │   ├── assembly-Dev │   │   ├── DevAppStackunique-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.js    ├── database-stack.js    └── my-stage.js
Python
cdk-demo-app ├── app.py ├── cdk.out │   ├── assembly-Dev │   │   ├── DevAppStackunique-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 │   ├── cdk.out │   ├── manifest.json │   └── tree.json └── cdk_demo_app    ├── __init__.py    ├── app_stack.py    ├── database_stack.py    └── my_stage.py
Java

cdk-demo-app ├── cdk.out │   ├── assembly-Dev │   │   ├── DevAppStackunique-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 │   ├── cdk.out │   ├── manifest.json │   └── tree.json └── src    └── main       └── java       └── com       └── myorg       ├── AppStack.java       ├── CdkDemoAppApp.java       ├── DatabaseStack.java       └── MyAppStage.java
C#

cdk-demo-app ├── cdk.out │   ├── assembly-Dev │   │   ├── DevAppStackunique-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 │   ├── cdk.out │   ├── manifest.json │   └── tree.json └── src └── CdkDemoApp    ├── AppStack.cs    ├── DatabaseStack.cs    ├── MyAppStage.cs    └── Program.cs
Go

cdk-demo-app ├── cdk-demo-app.go └── cdk.out    ├── assembly-Dev    │   ├── DevAppStackunique-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    ├── cdk.out    ├── manifest.json    └── tree.json
cdk-demo-app ├── bin │   └── cdk-demo-app.ts ├── cdk.out │   ├── assembly-Dev │   │   ├── DevAppStackunique-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

使用 列出堆疊時cdk list,我們總共看到四個堆疊:

$ cdk list Dev/AppStack (Dev-AppStack) Dev/DatabaseStack (Dev-DatabaseStack) Prod/AppStack (Prod-AppStack) Prod/DatabaseStack (Prod-DatabaseStack)

若要部署特定階段,我們會執行cdk deploy並提供要部署的堆疊。以下是使用*萬用字元在我們的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

下一個主題:

建構

上一個主題:

CDK 堆疊
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。