如何在CDK栈中指定参数定义?
How to specify parameter definition in CDK stack?
AWS CDK 的 Use an AWS CloudFormation Parameter 部分提到了如何自定义您的 AWS CloudFormation 模板。它指的是 CloudFormation 模板。我想将参数添加到我的 CDK 堆栈并获取合成的 CloudFormation 模板的参数部分。
我是否正确理解文档建议将参数部分添加到合成模板?如果是,它将被 cdk synth
的每个 运行 覆盖。
还有其他定义参数部分的方法吗?
编辑: 这是一个从上下文中读取存储桶名称的打字稿示例:https://github.com/cloudshiftstrategies/aws-cdk-examples/tree/master/context-example-typescript-app
您可以使用 CfnParameter 类型向 CDK 添加参数,如下所示:
new cdk.CfnParameter(this, 'MyParameter', {
type: 'String',
default: 'Blah',
noEcho: true,
});
但 CDK 通常不鼓励这样做。该设计旨在拥有完全可部署的堆栈,并使用 code/config 作为应为给定帐户创建的内容的条件。这是来自他们的 documentation:
When you run the cdk synth command for an app with multiple stacks, the cloud assembly includes a separate template for each stack instance. Even if the two stacks are instances of the same class, the AWS CDK emits them as two individual templates.
You can synthesize each template by specifying the stack name in the cdk synth command. The following example synthesizes the template for stack1.
This approach is conceptually different from how AWS CloudFormation templates are normally used, where a template can be deployed multiple times and parameterized through AWS CloudFormation parameters. Although AWS CloudFormation parameters can be defined in the AWS CDK, they are generally discouraged because AWS CloudFormation parameters are resolved only during deployment. This means that you cannot determine their value in your code. For example, to conditionally include a resource in your app based on the value of a parameter, you must set up an AWS CloudFormation condition and tag the resource with this condition. Because the AWS CDK takes an approach where concrete templates are resolved at synthesis time, you can use an if statement to check the value to determine whether a resource should be defined or some behavior should be applied.
cdk 中的参数将通过命令行或通过 cdk.json 的上下文值来完成,如此处所述:https://docs.aws.amazon.com/cdk/latest/guide/get_context_var.html
Use an AWS CloudFormation Parameter 部分提到了如何自定义您的 AWS CloudFormation 模板。它指的是 CloudFormation 模板。我想将参数添加到我的 CDK 堆栈并获取合成的 CloudFormation 模板的参数部分。
我是否正确理解文档建议将参数部分添加到合成模板?如果是,它将被 cdk synth
的每个 运行 覆盖。
还有其他定义参数部分的方法吗?
编辑: 这是一个从上下文中读取存储桶名称的打字稿示例:https://github.com/cloudshiftstrategies/aws-cdk-examples/tree/master/context-example-typescript-app
您可以使用 CfnParameter 类型向 CDK 添加参数,如下所示:
new cdk.CfnParameter(this, 'MyParameter', {
type: 'String',
default: 'Blah',
noEcho: true,
});
但 CDK 通常不鼓励这样做。该设计旨在拥有完全可部署的堆栈,并使用 code/config 作为应为给定帐户创建的内容的条件。这是来自他们的 documentation:
When you run the cdk synth command for an app with multiple stacks, the cloud assembly includes a separate template for each stack instance. Even if the two stacks are instances of the same class, the AWS CDK emits them as two individual templates.
You can synthesize each template by specifying the stack name in the cdk synth command. The following example synthesizes the template for stack1.
This approach is conceptually different from how AWS CloudFormation templates are normally used, where a template can be deployed multiple times and parameterized through AWS CloudFormation parameters. Although AWS CloudFormation parameters can be defined in the AWS CDK, they are generally discouraged because AWS CloudFormation parameters are resolved only during deployment. This means that you cannot determine their value in your code. For example, to conditionally include a resource in your app based on the value of a parameter, you must set up an AWS CloudFormation condition and tag the resource with this condition. Because the AWS CDK takes an approach where concrete templates are resolved at synthesis time, you can use an if statement to check the value to determine whether a resource should be defined or some behavior should be applied.
cdk 中的参数将通过命令行或通过 cdk.json 的上下文值来完成,如此处所述:https://docs.aws.amazon.com/cdk/latest/guide/get_context_var.html