[英]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.