adey27
adey27

Reputation: 469

KeyError: 'Records' while trying to read CSV file from S3 bucket - Lambda trigger

I have the following lambda function code for simply printing out the column name of CSV file from S3 bucket.

import json
import boto3
import csv

s3_client = boto3.client('s3')
def lambda_handler(event, context):
    # TODO implement
    print(event)
    
    bucket = event['Records'][0]['s3']['bucket']['name']
    csv_file = event['Records'][0]['s3']['object']['key']
    response = s3_client.get_object(Bucket=bucket, Key=csv_file)
    lines = response['Body'].read().decode('utf-8').split()
    results = []
    for row in csv.DictReader(lines):
        results.append(row.name())
    print(results)
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

However, I am getting the following error while testing:

[ERROR] KeyError: 'Records'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 10, in lambda_handler
    bucket = event['Records'][0]['s3']['bucket']['name']

Not sure where I am going wrong! Can anyone tell me how to resolve this?

After updating "Test" event manually, I am getting this error;

Test Event Name
TestEvent2

Response
{
  "errorMessage": "An error occurred (AccessDenied) when calling the GetObject operation: Access Denied",
  "errorType": "ClientError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 13, in lambda_handler\n    response = s3_client.get_object(Bucket=bucket, Key=csv_file)\n",
    "  File \"/var/runtime/botocore/client.py\", line 386, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 705, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n"
  ]
}

I have full access to my S3 bucket. Also, while printing the event its showing correct object name, s3 bucket name, records[] etc.

Upvotes: 1

Views: 1311

Answers (1)

Marcin
Marcin

Reputation: 238329

The error happens because you are using Test in AWS console. In that case some default event is provided. For your code you have to provide valid S3 event for your use-case. The S3 event structure is given here so you have to setup your Test to use valid event structure.

Alternatively, you can use AWS console provided events:

enter image description here

Upvotes: 1

Related Questions