I'm trying to write a lambda function that is triggered whenever a json file is uploaded to an s3 bucket. The function is supposed to parse the file and store it immediately in DynamoDB. I created a table called 'data' with the primary key set as 'date'. Here's what I have for the function so far:
import boto3
import json
s3_client = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
json_file_name = event['Records'][0]['s3']['object']['key']
json_object = s3.Bucket(bucket).Object(json_file_name)
jsonFileReader = json_object['Body'].read()
jsonDict = json.loads(jsonFileReader)
table = dynamodb.Table('data')
table.put_item(Item = jsonDict)
Here is an example of a json file I'm trying to use:
{
"date": "2020-06-07 21:00:34.284421",
"ConfirmedCases": 7062067,
"ActiveCases": 3206573,
"RecoveredCases": 3450965,
"Deaths": 404529
}
Unfortunately, whenever I test the code, it throws this error:
[[ERROR] TypeError: string indices must be integers
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 7, in lambda_handler
bucket = event'Records'][0]['s3']['bucket']['name']]
Does anyone know how to resolve this issue? I've wasted so much time trying to figure this out and I still am unable to :/