amazon-web-services - CloudFormation 模板 - 将现有 IAM 角色用于 Lambda 函数

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

我正在尝试使用 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/

相关文章:

amazon-web-services - 如何避免cloudformation与安全组的循环引用?

amazon-web-services - 如何使用 CloudFormation 模板更新 AWS Lambda 函数

amazon-web-services - EKS 支持哪些 EC2 实例类型?

amazon-web-services - Amazon Kubernetes AWS-EKS 未正确创建或未与 kubectl 同步

amazon-web-services - 无法通过 AWS Cognito API 重新发送验证码

mysql - AWS Aurora 仅在登录失败时将换行符记录到错误日志中

aws-lambda - AWS-CDK:跨账户资源访问和资源引用

amazon-web-services - 具有多路径的 AWS API 网关自定义域

amazon-web-services - aws cli cloudformation lambda无效输入错误

amazon-web-services - 用于管理 AWS 无服务器基础设施的 Terraform 或 cloudform