我正在尝试使用 cloudformation 模板中的现有角色(存在于 AWS 账户中)来设置 lambda 函数,我计划在多个 AWS 账户中使用它。
在 CF 模板中,我使用参数来设置角色的名称,然后在 Lambda 函数的角色属性中使用 Ref。这就是我的模板的样子,
"Parameters" : {
"ExistingRoleName" : {
"Type" : "String",
"Default" : "MyCustomRole"
}
"Resources" : {
"CustomLambdaFunction" : {
"Type" : "AWS::Lambda::Function",
"Properties" : {
"MemorySize" : "128",
"Role" : { "Ref" : "ExistingRoleName" },
}
},
...
但是,CF 模板失败并出现以下错误:
Properties validation failed for resource CustomLambdaFunction with message: #/Role: failed validation constraint for keyword [pattern]
这是因为 Cloudformation 中的 Lambda 资源需要角色 arn 而不是 RoleName,正如我在本文档 aws-resource-lambda-function 中看到的那样
基于此我像这样更新了 CF,
"Resources" : {
"CustomLambdaFunction" : {
"Type" : "AWS::Lambda::Function",
"Properties" : {
"MemorySize" : "128",
"Role" : "arn:aws:iam::AccountID:role/MyCustomRole",
}
},
但是,我仍然看到同样的错误。
Properties validation failed for resource CustomLambdaFunction with message: #/Role: failed validation constraint for keyword [pattern]
我想知道我是否在这里遗漏了一些东西?
最佳答案
The Ref
of an IAM Role “returns the resource name” ,而不是其 ARN。但您可以在角色的 Arn
属性上使用 GetAtt
。
在 JSON 中:
{"Fn::GetAtt": ["MyRole", "Arn"]}
在 YAML 中:
!GetAtt MyRole.Arn
关于amazon-web-services - CloudFormation 模板 - 将现有 IAM 角色用于 Lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68463393/