首页 > 解决方案 > 传入模板参数

问题描述

我们正在研究在我们的 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 仪表板选项卡中,但没有设置任何值,只有默认值。目前支持这个吗?如果没有,是否为未来计划?

标签: aws-cdk

解决方案


随着 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"

并且参数将在部署时传递到堆栈中。


推荐阅读