简体   繁体   English

如何在 CfnInclude 期间将(可选)参数直接从 CloudFormation 模板传递到 CDK 资源而不对参数列表进行硬编码?

[英]How can I pass (optional) parameters directly from CloudFormation template to CDK resource during CfnInclude without hardcoding the parameter list?

Kind of new to these syntax, so not sure if it's achieve-able.这些语法有点新,所以不确定它是否可以实现。

So we have a bunch of CloudFormation template written and we'd like to deploy them via CDK so we write code to construct them using CfnInclude .所以我们编写了一堆 CloudFormation 模板,我们想通过 CDK 部署它们,所以我们编写代码来使用CfnInclude构造它们。 The problem is CfnInclude always require explicit parameters argument if there are parameters.问题是如果有参数, CfnInclude总是需要显式parameters参数。

Is there a way to write these in a generic way, so we can create a whole bunch of resources via CfnInclude in just one for loop for all the CF templates, each may or may not have Parameters and the number and content of parameters is different?有没有办法以通用方式编写这些,因此我们可以通过CfnInclude在所有 CF 模板的一个 for 循环中创建一大堆资源,每个模板可能有也可能没有参数,参数的数量和内容不同? Cos otherwise, I'll have to supply all possible Parameters to all CF templates and then write them all out during CfnInclude .因为否则,我将不得不向所有 CF 模板提供所有可能的参数,然后在CfnInclude期间将它们全部写出。

The actual parameters' value are derived from another place and say then are put in a map, so say we know in total these CF templates may need 0, 1, 2 or 3 possible parameters from below list:实际参数的值是从另一个地方派生出来的,然后放在 map 中,所以说我们总共知道这些 CF 模板可能需要来自以下列表的 0、1、2 或 3 个可能的参数:

        let cfnParameters = {
            "param1": value1,
            "param2": value2,
            "param3": value3,
        };

Then can we somehow by supplying the template files themselves, write CDK in a way that it figures out how many and what parameters each file has, and then substitute them with proper values by looking up from the map (so that basically I'm passing a dynamically constructed Parameters list for each different template)?那么我们能否以某种方式通过自己提供模板文件,以某种方式编写 CDK,使其计算出每个文件有多少参数和什么参数,然后通过从 map 中查找来用适当的值替换它们(这样基本上我就通过了每个不同模板的动态构造的Parameters列表)? It seems like all those getParameters API only operate on the already constructed CF template as an object after CfnInclude is already called?似乎所有这些getParameters CfnInclude仅在已调用 CfnInclude 之后作为 object 在已构建的 CF 模板上运行?

And I don't think we can first construct a CfnInclude without passing Parameters argument and then do GetParameters and somehow replace them with actual values right?而且我不认为我们可以先构造一个CfnInclude而不传递Parameters参数,然后执行GetParameters并以某种方式用实际值替换它们,对吗? Doc seems to say Parameter Replacement can only be done at construction time? Doc好像说Parameter Replacement只能在构造时做?

You are right, CfnInclude needs the right keys in the parameters prop, or will throw an error.你是对的, CfnInclude需要parameters prop 中的正确键,否则会抛出错误。 No problem.没问题。 Make the template's parameter replacement map before constructing CfnInclude , using language features.在构造CfnInclude之前,使用语言特性将模板的参数替换为 map。 Read the template file.阅读模板文件。 Create a map of the template's keys and the desired values.创建模板键和所需值的 map。 Pass the resulting map to CfnInclude .将生成的 map 传递给CfnInclude

// MyTemplateIncluder.ts

// values we want to apply
const parameters = {
  InstanceType: 'm1.large',
  KeyName: 'my-key-name',
  SSHLocation: '0.0.0.0/0',
  AnotherKey: 'anotherValue',
};

// read the template
const templatePath = path.join(__dirname, 'template.json');
const file = fs.readFileSync(templatePath, 'utf-8');
const templateParams = JSON.parse(file)?.['Parameters'] ?? {};

// new object with keys from the template and values from parameters
const replacementParameters = Object.keys(templateParams).reduce((acc, curr) => {
  acc[curr] = parameters[curr];
  return acc;
}, {});

new include.CfnInclude(this, 'MyTemplate', {
  templateFile: templatePath,
  parameters: replacementParameters,
});

Encapsulate this logic in a reusable Construct subclass.将此逻辑封装在可重用的Construct子类中。 Accept parameter values and the template filename as props.接受参数值和模板文件名作为道具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 CloudFormation 中将字符串列表作为参数传递? - How do I pass a list of strings as a parameter in CloudFormation? 如何使用 lambda 删除和重新创建 CDK 堆栈 (Cloudformation)? - How can I delete and recreate a CDK Stack (Cloudformation) using a lambda? 如何在 AWS CDK 中引用其他区域的资源? - How can I reference a resource from other region in AWS CDK? Cloudformation / ECS - 如何直接从模板中检索任务定义 IP? - Cloudformation / ECS - How to retrieve taskdefinition IP directly from the template? 如何检查资源是否由 CloudFormation 创建? - How can I check if a resource was created by CloudFormation? 使用 jenkins 管道进行部署时,如何将参数从第一个 cdk 堆栈的 output 传递到另一个 cdk 堆栈的输入? - How do i pass parameters from first cdk stack's output to another cdk stack's input, when deploying using jenkins pipeline? 如何从 CDK/CloudFormation 重新创建手动删除的资源 - How to recreate manually deleted resources from the CDK/CloudFormation 如何在 CloudFormation 模板中将多个 URL 传递给 AllowOrigins - How to Pass Multiple URLs to AllowOrigins in CloudFormation Template 将 CloudFormation 模板 (yaml) 转换为 cdk python 代码 - Convert CloudFormation template (yaml) to cdk python code 手动更改资源时如何修复 cloudformation 更新 - How can I fix cloudformation update when resource was changed manually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM