Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

379
Views
AWS Lambda python function to parse json from S3 and store in DynamoDB

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 :/

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your error is from either of these lines.

bucket = event['Records'][0]['s3']['bucket']['name']
json_file_name = event['Records'][0]['s3']['object']['key']

However, they are correct. This is the valid way of access bucket name and object key from the event generated by S3 Notifications.

It seems to me that something else is trigger your function. Either you use Test option in the console and provide incorrect event object, or there is some other event that triggers the lambda with non-S3 event.

As a quick fix, you can do the following. The code below will finish if event object does not contain Records:

def lambda_handler(event, context): 

    if 'Records' not in event:
        print(event) # good to check what event you get  
        return

    # and the rest of code  

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!