gpt4 book ai didi

aws-cdk - 如何在 CDK 堆栈中指定参数定义?

转载 作者:行者123 更新时间:2023-12-01 00:11:40 27 4
gpt4 key购买 nike

Use an AWS CloudFormation Parameter AWS CDK 的部分提到了如何自定义您的 AWS CloudFormation 模板。指的是云形成模板 .我想将参数添加到我的 CDK 堆栈并获取合成 CF 模板的参数部分。

我是否正确理解文档建议将参数部分添加到合成模板?如果是,它将在每次运行 cdk synth 时被覆盖。 .

任何其他方式来定义参数部分?

最佳答案

编辑:这是一个从上下文中读取存储桶名称的 typescript 示例: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 通常不鼓励这样做。设计是拥有完全可部署的堆栈,并使用代码/配置作为应该为给定帐户创建的条件。这是来自他们的 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

关于aws-cdk - 如何在 CDK 堆栈中指定参数定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138536/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com