我在 CloudFormation 模板中有一个映射,让我们说它是:
"Mappings" : {
"RegionMap" : {
"us-east-1" : { "Name" : "Aaaa" },
"ap-northeast-1" : { "Name" : "Bbbb" }
}
}
现在我想将此映射作为环境变量传递给此模板中的 lambda:
"Environment": {
"Variables": {
"regions": {
"Ref": "RegionMap"
}
}
}
但这给了我错误:
Template contains errors.: Template format error: Unresolved resource dependencies [RegionMap] in the Resources block of the template
有什么办法让它发挥作用吗?
最佳答案
CloudFormation 映射部分数据只能使用内部函数 Fn::FindInMap
来提取。这意味着您将无法将整个映射对象作为环境变量发送。此外,无论上下文如何,环境变量始终是字符串。 CloudFormation 必须以 json 或 yaml 或其他格式序列化此对象,这可能就是它不允许您这样做的原因。
如果您的意图是创建 Custom Resource使用 Lambda 函数,然后您可以将整个 JSON 对象作为参数传递给资源。例如:
"AMIInfo": {
"Type": "Custom::AMIInfo",
"Properties": {
"ServiceToken": { "Fn::GetAtt" : ["AMIInfoFunction", "Arn"] },
"RegionMap" : {
"us-east-1" : { "Name" : "Aaaa" },
"ap-northeast-1" : { "Name" : "Bbbb" }
}
}
}
关于amazon-web-services - 使用 CloudFormation Mapping 作为自定义资源 lambda 的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46731464/