传入模板参数
Passing in template parameters
我们正在考虑在我们的 CI/CD 管道中使用 aws-cdk。我们需要能够在构建期间将参数传递到模板中,以便它可以生成要在部署期间使用的工件。我看到我们可以使用 cdk.json 文件来指定上下文属性,但这实际上并没有将值放入 CloudFormation 模板本身。只是让您在代码中访问它们。
我试过这样的方法:
const servicenameprop = new PipelinePrerequisitesProps();
servicenameprop.default = 'hello';
servicenameprop.type = 'String';
const serviceNameParameter = new Parameter(this, 'servicename', servicenameprop);
serviceNameParameter.value = new Token(servicename, 'servicename');
这会导致参数出现在 CloudFormation 仪表板选项卡中,但没有设置任何值,只有默认值。目前支持吗?如果没有,未来有计划吗?
CDK 目前 不支持将参数作为 cdk deploy
的一部分传入。如果您在堆栈中利用参数,则必须自己管理 CloudFormation 提交,至少现在是这样。例如,您可以将 AWS CLI 与 运行 cdk synth
的结果一起使用(您可以使用 cdk synth -o <directory>
)。
一般来说,我们鼓励创建尽可能具体的 CDK 堆栈。在 "synth" 时间直接将上下文传递给您的应用程序将允许您编写代码来推理它们并生成更简单、更可预测的模板(例如,您可以 而不是 将资源放在模板而不是添加条件和带有条件的资源)。
随着 CDK 版本 1.28.0 的发布,现在可以将 CloudFormation 参数传递给 deploy
命令。
public class ExampleApp {
public static void main(final String[] args) {
App app = new App();
new ExampleStack(app, "ExampleStack");
app.synth();
}
}
这是一个简单的什么都不做的堆栈:
public class ExampleStack extends Stack {
public ExampleStack(final Construct scope, final String id) {
this(scope, id, null);
}
public ExampleStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
CfnParameter someVar = CfnParameter.Builder.create(this, "SomeVar")
.description("Some variable that can be passed in at deploy-time.")
.type("String")
.build();
// rest of stack here
}
}
你可以运行cdk synth
然后把模板输出到某处,然后运行
cdk --app path/to/cdk.out deploy ExampleStack --parameters "ExampleStack:SomeVar=SomeValue"
并且参数将在部署时传递到堆栈中。
我们正在考虑在我们的 CI/CD 管道中使用 aws-cdk。我们需要能够在构建期间将参数传递到模板中,以便它可以生成要在部署期间使用的工件。我看到我们可以使用 cdk.json 文件来指定上下文属性,但这实际上并没有将值放入 CloudFormation 模板本身。只是让您在代码中访问它们。
我试过这样的方法:
const servicenameprop = new PipelinePrerequisitesProps();
servicenameprop.default = 'hello';
servicenameprop.type = 'String';
const serviceNameParameter = new Parameter(this, 'servicename', servicenameprop);
serviceNameParameter.value = new Token(servicename, 'servicename');
这会导致参数出现在 CloudFormation 仪表板选项卡中,但没有设置任何值,只有默认值。目前支持吗?如果没有,未来有计划吗?
CDK 目前 不支持将参数作为 cdk deploy
的一部分传入。如果您在堆栈中利用参数,则必须自己管理 CloudFormation 提交,至少现在是这样。例如,您可以将 AWS CLI 与 运行 cdk synth
的结果一起使用(您可以使用 cdk synth -o <directory>
)。
一般来说,我们鼓励创建尽可能具体的 CDK 堆栈。在 "synth" 时间直接将上下文传递给您的应用程序将允许您编写代码来推理它们并生成更简单、更可预测的模板(例如,您可以 而不是 将资源放在模板而不是添加条件和带有条件的资源)。
随着 CDK 版本 1.28.0 的发布,现在可以将 CloudFormation 参数传递给 deploy
命令。
public class ExampleApp {
public static void main(final String[] args) {
App app = new App();
new ExampleStack(app, "ExampleStack");
app.synth();
}
}
这是一个简单的什么都不做的堆栈:
public class ExampleStack extends Stack {
public ExampleStack(final Construct scope, final String id) {
this(scope, id, null);
}
public ExampleStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
CfnParameter someVar = CfnParameter.Builder.create(this, "SomeVar")
.description("Some variable that can be passed in at deploy-time.")
.type("String")
.build();
// rest of stack here
}
}
你可以运行cdk synth
然后把模板输出到某处,然后运行
cdk --app path/to/cdk.out deploy ExampleStack --parameters "ExampleStack:SomeVar=SomeValue"
并且参数将在部署时传递到堆栈中。