【问题标题】:CloudFormation template - Using existing IAM role in for Lambda functionsCloudFormation 模板 - 将现有 IAM 角色用于 Lambda 函数
【发布时间】:2021-09-28 11:15:12
【问题描述】:

我正在尝试使用 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 而不是我在此文档中看到的 RoleNameaws-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]

我想知道我是否在这里遗漏了什么?

【问题讨论】:

  • arn 看起来不错。也许您实际使用的那个不正确,此处未显示。
  • 另外,我猜AccountID 只是一个占位符,而不是您真实代码中的实际字符串?

标签: amazon-web-services aws-lambda amazon-cloudformation


【解决方案1】:

The Ref of an IAM Role “returns the resource name”,而不是它的 ARN。但是您可以在角色的Arn 属性上使用GetAtt

在 JSON 中:

{"Fn::GetAtt": ["MyRole", "Arn"]}

在 YAML 中:

!GetAtt MyRole.Arn

【讨论】:

    【解决方案2】:

    引用 iam 角色 arn 的格式
    "角色" : { "Fn::Sub" : "arn:aws:iam::${AWS::AccountId}:role/MyCustomRole" }

    【讨论】:

    • 感谢您的回复,我想这也可以。
    【解决方案3】:

    这对我有用,

    “角色”:{“Fn::Join”:[“”,[“arn:aws:iam::”,{“Ref”:“AWS::AccountId”},“:role/MyCustomRole”] ] }

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 2017-06-18
      • 2015-12-03
      • 2017-09-21
      • 2019-12-29
      • 2023-04-10
      • 2020-09-30
      • 2017-06-17
      • 2017-07-26
      相关资源
      最近更新 更多