【问题标题】:CloudFormation Lambda template, Expecting role to be string, when using Ref or GetAttCloudFormation Lambda 模板,期望角色是字符串,当使用 Ref 或 GetAtt
【发布时间】:2021-09-26 07:48:36
【问题描述】:

在创建我的 lambda 堆栈时,我使用了一个名为 LambdaExecutionRole 的角色,然后我通过 fn::GetAtt 引用了 ARN

"Role": {"Fn::GetAtt": ["LambdaExecutionRole","Arn"]},

,就像文档说的那样,然后我收到错误消息,指出指定的资源不支持 GetAtt。所以我尝试了 GetAtt,我被退回了:

Properties validation failed for resource GetECLambda with message: #/Code/S3Bucket: failed validation constraint for keyword [pattern] #/Role: expected type: String, found: JSONObject

我也试过"Role":{ "!Ref" : "LambdaExecutionRole"},

据我了解,其中一个应该返回一个字符串,因此将提供一个字符串,而不是 JSON 对象。但问题可能是字符串的定义如下:{“The Arn”},但我不确定如何避免这种情况。

我的lambda结构和作用如下:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Description" : "Stack to create the get-EC lambda",
    "Resources" : {
        "LambdaExecutionRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [{ "Effect": "Allow", "Principal": {"Service": ["lambda.amazonaws.com"]}, "Action": ["sts:AssumeRole"] }]
                },
                "Path": "/",
                "Policies": [{
                    "PolicyName": "root",
                    "PolicyDocument": {
                        "Version": "2012-10-17",
                        "Statement": [{ "Effect": "Allow", "Action": ["logs:*"], "Resource": "arn:aws:logs:*:*:*" }]
                    }
                }]
            }
        },
        "GetECLambda" : {
            "Type" : "AWS::Lambda::Function",
            "Properties" : {
                "FunctionName": "get-ecs",
                "Role":{ "!Ref" : "LambdaExecutionRole"},
                "Runtime": "nodejs12.x",
                "Code": {
                    "S3Bucket" : "arn:aws:s3:::flex-fit-lambda-functions-source",
                    "S3Key": "get-ecs.zip"
                }
            }
        }
    }
}

【问题讨论】:

  • 我猜你不能使用 !Ref 作为 JSON 格式。正如你在这里看到的docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/… !Ref 仅适用于 YML 格式
  • @Cyber​​Eternal 是的,我发现了,所以我用 {"Ref" : "LambdaExecutionRole"} 替换了它,我仍然遇到与预期字符串相同的错误。
  • 这很奇怪,因为我可以使用 "Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] }, 运行堆栈
  • @Cyber​​Eternal 抱歉,我说的不太对。我收到此错误:Properties validation failed for resource GetEmergencyContactsLambda with message: #/Code/S3Bucket: failed validation constraint for keyword [pattern] 这可能与我的存储桶与 ACL 中的 zip 文件有关吗?我没有修改它,因为我认为如果我可以访问它并且我正在执行堆栈,它将使用我的权限。最重要的是,我实际上无法理解该错误消息试图告诉我什么。

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


【解决方案1】:

在 JSON 中指定 Cloudformation 模板时,仅支持这种调用内部函数的形式:

{ "Fn::GetAtt" : [ "logicalNameOfResource", "attributeName" ] }

!Ref 表单仅支持 YAML。 因此,请尝试在模板中将当前调用更改为此:

"Role": { "Fn::GetAtt" : [ "LambdaExecutionRole", "Arn" ] }

【讨论】:

  • 不幸的是,它返回以下内容:Properties validation failed for resource GetEmergencyContactsLambda with message: #/Code/S3Bucket: failed validation constraint for keyword [pattern] 那条错误消息实际上告诉我什么?
  • 这个答案修复了字符串问题,另一个问题是我使用 ARN 来指定存储桶,而您只需要指定存储桶名称。
猜你喜欢
  • 2020-02-25
  • 2021-08-20
  • 2021-09-28
  • 2017-06-18
  • 1970-01-01
  • 2020-09-30
  • 2018-12-01
  • 1970-01-01
  • 2020-06-08
相关资源
最近更新 更多