Pilih preferensi cookie Anda

Kami menggunakan cookie penting serta alat serupa yang diperlukan untuk menyediakan situs dan layanan. Kami menggunakan cookie performa untuk mengumpulkan statistik anonim sehingga kami dapat memahami cara pelanggan menggunakan situs dan melakukan perbaikan. Cookie penting tidak dapat dinonaktifkan, tetapi Anda dapat mengklik “Kustom” atau “Tolak” untuk menolak cookie performa.

Jika Anda setuju, AWS dan pihak ketiga yang disetujui juga akan menggunakan cookie untuk menyediakan fitur situs yang berguna, mengingat preferensi Anda, dan menampilkan konten yang relevan, termasuk iklan yang relevan. Untuk menerima atau menolak semua cookie yang tidak penting, klik “Terima” atau “Tolak”. Untuk membuat pilihan yang lebih detail, klik “Kustomisasi”.

Konfigurasikan dan lakukan sintesis tumpukan CDK

Mode fokus
Konfigurasikan dan lakukan sintesis tumpukan CDK - AWS Cloud Development Kit (AWS CDK) v2

Ini adalah Panduan Pengembang AWS CDK v2. CDK v1 yang lebih lama memasuki pemeliharaan pada 1 Juni 2022 dan mengakhiri dukungan pada 1 Juni 2023.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Ini adalah Panduan Pengembang AWS CDK v2. CDK v1 yang lebih lama memasuki pemeliharaan pada 1 Juni 2022 dan mengakhiri dukungan pada 1 Juni 2023.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Sebelum Anda dapat menyebarkan AWS Cloud Development Kit (AWS CDK) tumpukan, itu harus disintesis terlebih dahulu. Stack synthesis adalah proses memproduksi AWS CloudFormation template dan artefak penyebaran dari tumpukan CDK. Template dan artefak dikenal sebagai perakitan cloud. Perakitan cloud adalah apa yang digunakan untuk menyediakan sumber daya Anda. AWS Untuk informasi selengkapnya tentang cara kerja penerapan, lihat. Cara AWS CDK kerja penerapan

Bagaimana sintesis dan bootstrap bekerja sama

Agar aplikasi CDK Anda dapat diterapkan dengan benar, CloudFormation templat yang dihasilkan selama sintesis harus menentukan sumber daya yang dibuat dengan benar selama bootstrap. Oleh karena itu, bootstrap dan sintesis harus saling melengkapi agar penerapan berhasil:

  • Bootstrapping adalah proses satu kali menyiapkan lingkungan untuk penerapan. AWS AWS CDK Ini mengonfigurasi AWS sumber daya tertentu di lingkungan Anda yang digunakan oleh CDK untuk penerapan. Ini biasanya disebut sebagai sumber daya bootstrap. Untuk petunjuk tentang bootstrap, lihat. Bootstrap lingkungan Anda untuk digunakan dengan AWS CDK

  • CloudFormation template yang dihasilkan selama sintesis mencakup informasi tentang sumber daya bootstrap mana yang akan digunakan. Selama sintesis, CDK CLI tidak tahu secara spesifik bagaimana AWS lingkungan Anda telah di-bootstrap. Sebaliknya, CDK CLI menghasilkan CloudFormation template berdasarkan synthesizer yang Anda konfigurasikan untuk setiap tumpukan CDK. Agar penerapan berhasil, synthesizer harus menghasilkan CloudFormation template yang mereferensikan sumber daya bootstrap yang benar untuk digunakan.

CDK dilengkapi dengan synthesizer default dan konfigurasi bootstrap yang dirancang untuk bekerja sama. Jika Anda menyesuaikan satu, Anda harus menerapkan penyesuaian yang relevan ke yang lain.

Cara mengkonfigurasi sintesis tumpukan CDK

Anda mengonfigurasi sintesis tumpukan CDK menggunakan synthesizer properti Stack instance Anda. Properti ini menentukan bagaimana tumpukan CDK Anda akan disintesis. Anda memberikan instance dari kelas yang mengimplementasikan IStackSynthesizer atauIReusableStackSynthesizer. Metodenya akan dipanggil setiap kali aset ditambahkan ke tumpukan atau ketika tumpukan disintesis. Berikut ini adalah contoh dasar menggunakan properti ini dalam tumpukan Anda:

TypeScript
new MyStack(this, 'MyStack', { // stack properties synthesizer: new DefaultStackSynthesizer({ // synthesizer properties }), });
JavaScript
new MyStack(this, 'MyStack', { // stack properties synthesizer: new DefaultStackSynthesizer({ // synthesizer properties }), });
Python
MyStack(self, "MyStack", # stack properties synthesizer=DefaultStackSynthesizer( # synthesizer properties ))
Java

new MyStack(app, "MyStack", StackProps.builder() // stack properties .synthesizer(DefaultStackSynthesizer.Builder.create() // synthesizer properties .build()) .build();
C#
new MyStack(app, "MyStack", new StackProps // stack properties { Synthesizer = new DefaultStackSynthesizer(new DefaultStackSynthesizerProps { // synthesizer properties }) });
Go
func main() { app := awscdk.NewApp(nil) NewMyStack(app, "MyStack", &MyStackProps{ StackProps: awscdk.StackProps{ Synthesizer: awscdk.NewDefaultStackSynthesizer(&awscdk.DefaultStackSynthesizerProps{ // synthesizer properties }), }, }) app.Synth(nil) }
new MyStack(this, 'MyStack', { // stack properties synthesizer: new DefaultStackSynthesizer({ // synthesizer properties }), });

Anda juga dapat mengonfigurasi synthesizer untuk semua tumpukan CDK di aplikasi CDK menggunakan properti instance Anda: defaultStackSynthesizer App

TypeScript

import { App, Stack, DefaultStackSynthesizer } from 'aws-cdk-lib'; const app = new App({ // Configure for all stacks in this app defaultStackSynthesizer: new DefaultStackSynthesizer({ /* ... */ }), });
JavaScript

const { App, Stack, DefaultStackSynthesizer } = require('aws-cdk-lib'); const app = new App({ // Configure for all stacks in this app defaultStackSynthesizer: new DefaultStackSynthesizer({ /* ... */ }), });
Python

from aws_cdk import App, Stack, DefaultStackSynthesizer app = App( default_stack_synthesizer=DefaultStackSynthesizer( # Configure for all stacks in this app # ... ) )
Java

import software.amazon.awscdk.App; import software.amazon.awscdk.Stack; import software.amazon.awscdk.DefaultStackSynthesizer; public class Main { public static void main(final String[] args) { App app = new App(AppProps.builder() // Configure for all stacks in this app .defaultStackSynthesizer(DefaultStackSynthesizer.Builder.create().build()) .build() ); } }
C#

using Amazon.CDK; using Amazon.CDK.Synthesizers; namespace MyNamespace { sealed class Program { public static void Main(string[] args) { var app = new App(new AppProps { // Configure for all stacks in this app DefaultStackSynthesizer = new DefaultStackSynthesizer(new DefaultStackSynthesizerProps { // ... }) }); } } }
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" ) func main() { defer jsii.Close() app := awscdk.NewApp(&awscdk.AppProps{ // Configure for all stacks in this app DefaultStackSynthesizer: awscdk.NewDefaultStackSynthesizer(&awscdk.DefaultStackSynthesizerProps{ // ... }), }) }

import { App, Stack, DefaultStackSynthesizer } from 'aws-cdk-lib'; const app = new App({ // Configure for all stacks in this app defaultStackSynthesizer: new DefaultStackSynthesizer({ /* ... */ }), });

Secara default, AWS CDK penggunaannyaDefaultStackSynthesizer. Jika Anda tidak mengkonfigurasi synthesizer, synthesizer ini akan digunakan.

Jika Anda tidak memodifikasi bootstrap, seperti membuat perubahan pada tumpukan atau template bootstrap, Anda tidak perlu memodifikasi sintesis tumpukan. Anda bahkan tidak perlu menyediakan synthesizer. CDK akan menggunakan DefaultStackSynthesizer kelas default untuk mengonfigurasi sintesis tumpukan CDK untuk berinteraksi dengan benar dengan tumpukan bootstrap Anda.

Cara mensintesis tumpukan CDK

Untuk mensintesis tumpukan CDK, gunakan Antarmuka Baris AWS CDK Perintah (AWS CDK CLI) cdk synth perintah. Untuk informasi selengkapnya tentang perintah ini, termasuk opsi yang dapat Anda gunakan dengan perintah ini, lihatcdk synthesize.

Jika aplikasi CDK berisi satu tumpukan, atau untuk mensintesis semua tumpukan, Anda tidak perlu memberikan nama tumpukan CDK sebagai argumen. Secara default, CDK CLI akan mensintesis tumpukan CDK Anda ke dalam templat. AWS CloudFormation Template json yang diformat untuk setiap tumpukan disimpan ke cdk.out direktori. Jika aplikasi Anda berisi satu tumpukan, templat yang yaml diformat akan dicetak. stdout Berikut adalah contohnya:

$ cdk synth Resources: CDKMetadata: Type: AWS::CDK::Metadata Properties: Analytics: v2:deflate64:H4sIAAAAAAAA/unique-identifier Metadata: aws:cdk:path: CdkAppStack/CDKMetadata/Default Condition: CDKMetadataAvailable ...

Jika aplikasi CDK berisi beberapa tumpukan, Anda dapat memberikan ID logis tumpukan untuk mensintesis satu tumpukan. Berikut adalah contohnya:

$ cdk synth MyStackName

Jika Anda tidak mensintesis tumpukan dan menjalankannyacdk deploy, CDK CLI akan secara otomatis mensintesis tumpukan Anda sebelum penerapan.

Bagaimana sintesis bekerja secara default

Dihasilkan logis IDs di AWS CloudFormation template Anda

Saat Anda mensintesis tumpukan CDK untuk menghasilkan CloudFormation templat, logis IDs dihasilkan dari sumber berikut, diformat sebagai: <construct-path><construct-ID><unique-hash>

  • Construct path — Seluruh path ke build di aplikasi CDK Anda. Jalur ini mengecualikan ID konstruksi L1, yang selalu Resource atauDefault, dan ID tumpukan tingkat atas yang menjadi bagiannya.

  • Construct ID — ID yang Anda berikan sebagai argumen kedua saat membuat instance konstruksi Anda.

  • Hash unik — Ini AWS CDK menghasilkan hash unik 8 karakter menggunakan algoritma hashing deterministik. Hash unik ini membantu memastikan bahwa nilai ID logis dalam template Anda unik satu sama lain. Perilaku deterministik dari generasi hash ini memastikan bahwa nilai ID logis yang dihasilkan untuk setiap konstruksi tetap sama setiap kali Anda melakukan sintesis. Nilai hash hanya akan berubah jika Anda memodifikasi nilai konstruksi tertentu seperti ID konstruksi Anda atau jalurnya.

Logika IDs memiliki panjang maksimum 255 karakter. Oleh karena itu, AWS CDK akan memotong jalur konstruksi dan membangun ID jika perlu untuk tetap dalam batas itu.

Berikut ini adalah contoh konstruksi yang mendefinisikan bucket Amazon Simple Storage Service (Amazon S3) Simple Storage Service (Amazon S3). Di sini, kami lulus myBucket sebagai ID untuk konstruksi kami:

TypeScript
import * as cdk from 'aws-cdk-lib'; import { Construct} from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; export class MyCdkAppStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define the S3 bucket new s3.Bucket(this, 'myBucket', { versioned: true, removalPolicy: cdk.RemovalPolicy.DESTROY, }); } }
JavaScript

const cdk = require('aws-cdk-lib'); const s3 = require('aws-cdk-lib/aws-s3'); class MyCdkAppStack extends cdk.Stack { constructor(scope, id, props) { super(scope, id, props); new s3.Bucket(this, 'myBucket', { versioned: true, removalPolicy: cdk.RemovalPolicy.DESTROY, }); } } module.exports = { MyCdkAppStack }
Python
import aws_cdk as cdk from constructs import Construct from aws_cdk import Stack from aws_cdk import aws_s3 as s3 class MyCdkAppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) s3.Bucket(self, 'MyBucket', versioned=True, removal_policy=cdk.RemovalPolicy.DESTROY )
Java

package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; import software.amazon.awscdk.services.s3.Bucket; import software.amazon.awscdk.services.s3.BucketProps; import software.amazon.awscdk.RemovalPolicy; public class MyCdkAppStack extends Stack { public MyCdkAppStack(final Construct scope, final String id) { this(scope, id, null); } public MyCdkAppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); Bucket.Builder.create(this, "myBucket") .versioned(true) .removalPolicy(RemovalPolicy.DESTROY) .build(); } }
C#
using Amazon.CDK; using Constructs; using Amazon.CDK.AWS.S3; namespace MyCdkApp { public class MyCdkAppStack : Stack { public MyCdkAppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { new Bucket(this, "myBucket", new BucketProps { Versioned = true, RemovalPolicy = RemovalPolicy.DESTROY }); } } }
Go
package main import ( "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/aws-cdk-go/awscdk/v2/awss3" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) type MyCdkAppStackProps struct { awscdk.StackProps } func NewMyCdkAppStack(scope constructs.Construct, id string, props *MyCdkAppStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) awss3.NewBucket(stack, jsii.String("myBucket"), &awss3.BucketProps{ Versioned: jsii.Bool(true), RemovalPolicy: awscdk.RemovalPolicy_DESTROY, }) return stack } // ...
import * as cdk from 'aws-cdk-lib'; import { Construct} from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; export class MyCdkAppStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define the S3 bucket new s3.Bucket(this, 'myBucket', { versioned: true, removalPolicy: cdk.RemovalPolicy.DESTROY, }); } }

Ketika kita menjalankancdk synth, ID logis dalam format myBucketunique-hash akan dihasilkan. Berikut ini adalah contoh sumber daya ini dalam AWS CloudFormation template yang dihasilkan:

Resources: myBucket5AF9C99B: Type: AWS::S3::Bucket Properties: VersioningConfiguration: Status: Enabled UpdateReplacePolicy: Delete DeletionPolicy: Delete Metadata: aws:cdk:path: S3BucketAppStack/myBucket/Resource

Berikut ini adalah contoh konstruksi kustom bernama Bar yang mendefinisikan bucket Amazon S3. BarKonstruksi mencakup konstruksi khusus Foo di jalurnya:

TypeScript

import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; // Define the Bar construct export class Bar extends Construct { constructor(scope: Construct, id: string) { super(scope, id); // Define an S3 bucket inside of Bar new s3.Bucket(this, 'Bucket', { versioned: true, removalPolicy: cdk.RemovalPolicy.DESTROY, } ); } } // Define the Foo construct export class Foo extends Construct { constructor(scope: Construct, id: string) { super(scope, id); // Create an instance of Bar inside Foo new Bar(this, 'Bar'); } } // Define the CDK stack export class MyCustomAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Instantiate Foo construct in the stack new Foo(this, 'Foo'); } }
JavaScript

const cdk = require('aws-cdk-lib'); const s3 = require('aws-cdk-lib/aws-s3'); const { Construct } = require('constructs'); // Define the Bar construct class Bar extends Construct { constructor(scope, id) { super(scope, id); // Define an S3 bucket inside of Bar new s3.Bucket(this, 'Bucket', { versioned: true, removalPolicy: cdk.RemovalPolicy.DESTROY, }); } } // Define the Foo construct class Foo extends Construct { constructor(scope, id) { super(scope, id); // Create an instance of Bar inside Foo new Bar(this, 'Bar'); } } // Define the CDK stack class MyCustomAppStack extends cdk.Stack { constructor(scope, id, props) { super(scope, id, props); // Instantiate Foo construct in the stack new Foo(this, 'Foo'); } } module.exports = { MyCustomAppStack }
Python

import aws_cdk as cdk from constructs import Construct from aws_cdk import ( Stack, aws_s3 as s3, RemovalPolicy, ) # Define the Bar construct class Bar(Construct): def __init__(self, scope: Construct, id: str) -> None: super().__init__(scope, id) # Define an S3 bucket inside of Bar s3.Bucket(self, 'Bucket', versioned=True, removal_policy=RemovalPolicy.DESTROY ) # Define the Foo construct class Foo(Construct): def __init__(self, scope: Construct, id: str) -> None: super().__init__(scope, id) # Create an instance of Bar inside Foo Bar(self, 'Bar') # Define the CDK stack class MyCustomAppStack(Stack): def __init__(self, scope: Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) # Instantiate Foo construct in the stack Foo(self, 'Foo')
Java

Dalam my-custom-app/src/main/java/com/myorg/Bar.java:

package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.services.s3.Bucket; import software.amazon.awscdk.services.s3.BucketProps; import software.amazon.awscdk.RemovalPolicy; public class Bar extends Construct { public Bar(final Construct scope, final String id) { super(scope, id); // Define an S3 bucket inside Bar Bucket.Builder.create(this, "Bucket") .versioned(true) .removalPolicy(RemovalPolicy.DESTROY) .build(); } }

Dalam my-custom-app/src/main/java/com/myorg/Foo.java:

package com.myorg; import software.constructs.Construct; public class Foo extends Construct { public Foo(final Construct scope, final String id) { super(scope, id); // Create an instance of Bar inside Foo new Bar(this, "Bar"); } }

Dalam my-custom-app/src/main/java/com/myorg/MyCustomAppStack.java:

package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; public class MyCustomAppStack extends Stack { public MyCustomAppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Instantiate Foo construct in the stack new Foo(this, "Foo"); } // Overload constructor in case StackProps is not provided public MyCustomAppStack(final Construct scope, final String id) { this(scope, id, null); } }
C#
using Amazon.CDK; using Constructs; using Amazon.CDK.AWS.S3; namespace MyCustomApp { // Define the Bar construct public class Bar : Construct { public Bar(Construct scope, string id) : base(scope, id) { // Define an S3 bucket inside Bar new Bucket(this, "Bucket", new BucketProps { Versioned = true, RemovalPolicy = RemovalPolicy.DESTROY }); } } // Define the Foo construct public class Foo : Construct { public Foo(Construct scope, string id) : base(scope, id) { // Create an instance of Bar inside Foo new Bar(this, "Bar"); } } // Define the CDK Stack public class MyCustomAppStack : Stack { public MyCustomAppStack(Construct scope, string id, StackProps props = null) : base(scope, id, props) { // Instantiate Foo construct in the stack new Foo(this, "Foo"); } } }
Go

package main import ( "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/aws-cdk-go/awscdk/v2/awss3" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) // Define the Bar construct type Bar struct { constructs.Construct } func NewBar(scope constructs.Construct, id string) constructs.Construct { bar := constructs.NewConstruct(scope, &id) // Define an S3 bucket inside Bar awss3.NewBucket(bar, jsii.String("Bucket"), &awss3.BucketProps{ Versioned: jsii.Bool(true), RemovalPolicy: awscdk.RemovalPolicy_DESTROY, }) return bar } // Define the Foo construct type Foo struct { constructs.Construct } func NewFoo(scope constructs.Construct, id string) constructs.Construct { foo := constructs.NewConstruct(scope, &id) // Create an instance of Bar inside Foo NewBar(foo, "Bar") return foo } // Define the CDK Stack type MyCustomAppStackProps struct { awscdk.StackProps } func NewMyCustomAppStack(scope constructs.Construct, id string, props *MyCustomAppStackProps) awscdk.Stack { stack := awscdk.NewStack(scope, &id, &props.StackProps) // Instantiate Foo construct in the stack NewFoo(stack, "Foo") return stack } // Define the CDK App func main() { app := awscdk.NewApp(nil) NewMyCustomAppStack(app, "MyCustomAppStack", &MyCustomAppStackProps{ StackProps: awscdk.StackProps{}, }) app.Synth(nil) }

import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; // Define the Bar construct export class Bar extends Construct { constructor(scope: Construct, id: string) { super(scope, id); // Define an S3 bucket inside of Bar new s3.Bucket(this, 'Bucket', { versioned: true, removalPolicy: cdk.RemovalPolicy.DESTROY, } ); } } // Define the Foo construct export class Foo extends Construct { constructor(scope: Construct, id: string) { super(scope, id); // Create an instance of Bar inside Foo new Bar(this, 'Bar'); } } // Define the CDK stack export class MyCustomAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Instantiate Foo construct in the stack new Foo(this, 'Foo'); } }

Ketika kita menjalankancdk synth, ID logis dalam format FooBarBucketunique-hash akan dihasilkan. Berikut ini adalah contoh sumber daya ini dalam AWS CloudFormation template yang dihasilkan:

Resources: FooBarBucketBA3ED1FA: Type: AWS::S3::Bucket Properties: VersioningConfiguration: Status: Enabled UpdateReplacePolicy: Delete DeletionPolicy: Delete # ...

Sesuaikan sintesis tumpukan CDK

Jika perilaku sintesis CDK default tidak sesuai dengan kebutuhan Anda, Anda dapat menyesuaikan sintesis CDK. Untuk melakukan ini, Anda memodifikasiDefaultStackSynthesizer, menggunakan synthesizer bawaan lain yang tersedia, atau membuat synthesizer Anda sendiri. Untuk instruksi, lihat Sesuaikan sintesis tumpukan CDK.

PrivasiSyarat situsPreferensi cookie
© 2025, Amazon Web Services, Inc. atau afiliasinya. Semua hak dilindungi undang-undang.