Lambda function should be triggered when any changes in S3 object with respect to file and apply those findings in anther s3 bucket

  1. Two s3 buckets are created in N.Virgenia region.
  2. Lambda Function created using Python
  3. file data has to be checked in bucket 1 and compare that data with file which is available in bucket 2 if changes found transfer them to bucket 2 from bucket 1 of file
  4. if there are no modifications in file or new file added -- lambda should trigger and that new should be transferred to bucket 2 Trying to implementing the above scenario getting below error, Any idea on below error please

Response { "errorMessage": "'Record'", "errorType": "KeyError", "requestId": "6be014f1-c78c-4a9b-9728-5873b1080812", "stackTrace": [ " File "/var/task/", line 9, in lambda_handler\n file_obj = event["Record"][0]["s3bucket001forlambda01"]\n" ] }

Function Logs START RequestId: 6be014f1-c78c-4a9b-9728-5873b1080812 Version: $LATEST Event : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} [ERROR] KeyError: 'Record'

1 Answer

The key in the Event object is names Records, and not Record as you wrote. You should reference the object using event["Records"][0]["s3"]["bucket"]["name"] and event["Records"][0]["s3"]["object"]["key"].

Further more, you are testing the code from the console, so you need to provide a valid structured event object. Just choose S3-Put for the template in the Test event page.

profile picture
answered 3 months ago
  • import json import urllib.parse import boto3

    print('Loading function') s3 = boto3.client('s3') def lambda_handler(event, context): #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event["Records"][0]["s3"]["bucket"]['s3bucket001forlambda01']
    key = urllib.parse.unquote_plus(event["Records"][0]["s3"]["object"]["Notes"], encoding='utf-8')
        response = s3.get_object(Bucket=bucket, Key=key)
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']
    except Exception as e:
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

    Here is my code -- still getting key error

    Response { "errorMessage": "'s3bucket001forlambda01'", "errorType": "KeyError", "stackTrace": [ " File "/var/task/", line 14, in lambda_handler\n bucket = event["Records"][0]["s3"]["bucket"]['s3bucket001forlambda01']\n" ] }

    Function Logs START RequestId: eb0f25c5-e96f-4db0-83b3-702a23203847 Version: $LATEST [ERROR] KeyError: 's3bucket001forlambda01' Traceback (most recent call last):   File "/var/task/", line 14, in lambda_handler     bucket = event["Records"][0]["s3"]["bucket"]['s3bucket001forlambda01'] END RequestId: eb0f25c5-e96f-4db0-83b3-702a23203847

  • You should not include ['s3bucket001forlambda01'] in there. Instead you should use ["name"], just as I wrote in my example. You get the bucket name and object key from the event object. You should not provide them youself.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions