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

I am trying to get events from SQS Queue into S3 bucket using Lambda function. I am getting below error while trying to deploy using cloudFormation template. What am i missing in my Lambda Execution role?

Error:

- 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, 

enter image description here

I am using below Lambda function and Lambda execution role in my cloudFormation Template.

    "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": "*"
            }
          ]
        }
      }
    ]
  }
}

And Lambda Function is :

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")

Solution 1:

"S3Key": "S3Bucket" is incorrect. S3Bucket should be the name of your lambda zip file in the S3. So you have to provide valid name for that, after uploading zip with your source code to S3. For example:

`"S3Key": "myfunction.zip"`