这是编写由SQS触发的lambda的CFT的正确方法吗?

时间:2019-04-17 15:48:37

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

您好,我最近为自己的lambda和sqs(都在相同的帐户/区域)创建了CFT,但是我注意到当我转到aws控制台时,它在“输入端”和“输出”。这是故意的吗?还是我做的CFT不正确。以下是相关的JSON代码。

 "MyLambdaFunctionRole": {
      "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": [
                    "sqs:ReceiveMessage",
                    "sqs:DeleteMessage",
                    "sqs:GetQueueAttributes"
                    ],
                  "Resource": {
                    "Fn::Sub": "arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:test-${Environment}-my-queue"
                  }
                }
              ]
            }
          }
        ]
      }
    },

    "OutputRouterEventSource": {
      "Type": "AWS::Lambda::EventSourceMapping",
      "Properties": {
        "BatchSize": 10,
        "Enabled" : true,
        "EventSourceArn": {
          "Fn::Sub": "arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:test-${Environment}-my-queue"
        },
        "FunctionName": {
          "Fn::GetAtt": [
            "MyLambdaFunction",
            "Arn"
          ]
        }
      }
    },
    "OutputRouterLambdaInvokePermission": {
    "Type": "AWS::Lambda::Permission",
    "Properties": {
      "Action": "lambda:InvokeFunction",
      "Principal": "sns.amazonaws.com",
      "SourceArn": {
        "Fn::Sub": "arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:test-${Environment}-my-queue"
      },
      "FunctionName": {
          "Fn::Sub": "test-${Environment}-my-lambda"
        }
      }
    }

1 个答案:

答案 0 :(得分:0)

在这种情况下,在两侧都具有SQS是正常的:SQS触发Lambda函数(左侧),并且Lambda函数的角色必须具有从队列接收和删除消息的权限(右侧)。

(我认为您正在OutputRouterLambdaInvokePermission资源中混合服务,其中主体是SNS,而源引用了SQS队列的地址,您可以删除此资源,因为SQS触发器不需要此资源)