尝试使用 cloudFormation 模板创建堆栈时获取 'Error occurred while GetObject. S3 Error Code: NoSuchKey'

Getting 'Error occurred while GetObject. S3 Error Code: NoSuchKey' while trying to create stack using cloudFormation template

我正在尝试使用 Lambda 函数将事件从 SQS 队列获取到 S3 存储桶中。 尝试使用 cloudFormation 模板进行部署时出现以下错误。 我的 Lambda 执行角色缺少什么?

错误:

- ERROR - Stack  shows a rollback status ROLLBACK_IN_PROGRESS.
- INFO - The following root cause failure event was found in the stack for resource 'EventLambda':
- INFO - Resource handler returned message: "Error occurred while GetObject. S3 Error Code: NoSuchKey. 
        S3 Error Message: The specified key does not exist. (Service: Lambda, Status Code: 400, Request ID: 6b19aec2-6b0e-437a-8f19-7d699f3b3c52, 

我在我的 cloudFormation 模板中使用以下 Lambda 函数和 Lambda 执行角色。

    "EventLambda": {
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Code": {
      "S3Bucket": {
        "Ref": "S3Bucket"
      },
      "S3Key": "S3Bucket"
    },
    "Description": "Copy events from SQS Queue into s3 bucket",
    "Environment": {
      "Variables": {
        "FinalBucket": {
          "Ref": "EventDeployS3Bucket"
        }
      }
    },
    "Handler": "sqs_to_s3_lambda.lambda_handler",
    "Layers": [
      {
        "Ref": "LambdaLayerVersion"
      }
    ],
    "MemorySize": 128,
    "Role": {
      "Fn::GetAtt": [
        "LambdaExecutionRole",
        "Arn"
      ]
    },
    "Runtime": "python3.7",
    "Timeout": 300
  }
},
"LambdaExecutionRole": {
  "Type": "AWS::IAM::Role",
  "Properties": {
    "AssumeRolePolicyDocument": {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "Service": [
              "lambda.amazonaws.com"
            ]
          },
          "Action": [
            "sts:AssumeRole"
          ]
        }
      ]
    },
    "ManagedPolicyArns": [
      "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
    ],
    "Policies": [
      {
        "PolicyName": "LambdaPolicy",
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "sqs:ReceiveMessage",
                "sqs:DeleteMessage",
                "sqs:GetQueueAttributes",
                "sqs:ChangeMessageVisibility"
              ],
              "Resource": "*"
            }
          ]
        }
      }
    ]
  }
}

而 Lambda 函数是:

import json
import logging
import os

import boto3

logger = logging.getLogger()
logger.info("init")
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")



def copy_files_to_s3(event, context):
    # Setup the client
    s3_bucket = boto3.resource("s3")

    logger.info(f"lambda_handler -- event: {json.dumps(event)}")

    events = json.loads(event["Records"][0]["body"])
    print(events)
    s3_bucket.put_object(Bucket=os.environ['S3BucketEvents'], key="data.json", Body=json.dumps(events))
    logger.info("done")


def lambda_handler(event, context):
    logger.info("copied events data")

    copy_files_to_s3(event, context)
    logger.info("done")

"S3Key": "S3Bucket" 不正确。 S3Bucket 应该是 S3 中的 lambda zip 文件的名称。因此,在将带有源代码的 zip 上传到 S3 后,您必须为此提供有效名称。例如:

`"S3Key": "myfunction.zip"`