I am trying to load this big Json file (over 8k transactions) with the structure below into DynamoDB using the Lambda function.
{
"transactions": [
{
"customerId": "abc",
"transactionId": "123",
"transactionDate": "2020-09-01",
"merchantId": "1234",
"categoryId": "3",
"amount": "5",
"description": "McDonalds"
},
{
"customerId": "def",
"transactionId": "456",
"transactionDate": "2020-09-01",
"merchantId": "45678",
"categoryId": "2",
"amount": "-11.70",
"description": "Tescos"
},
{
"customerId": "jkl",
"transactionId": "gah",
"transactionDate": "2020-09-01",
"merchantId": "9081",
"categoryId": "3",
"amount": "-139.00",
"description": "Amazon"
},
...
The lambda function I am trying to use is going to be triggered upon uploading the Json file into the S3 bucket. That should then automatically load data into DynamoDB. The lambda function currently has the following code:
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']
print(bucket)
print(json_file_name)
print(str(event))
json_object = s3_client.get_object(Bucket=bucket,Key=json_file_name)
jsonFileReader = json_object ['Body'].read()
jsonDict = json.loads(jsonFileReader)
table = dynamodb.Table('CustomerEvents')
table.put_item(Item=jsonDict)
return 'Hello from Lambda'
This works fine if I try to upload one unique transaction into DynamoDB, i.e, if the structure of the file is simply the below:
{
"customerId": "abc",
"transactionId": "123",
"transactionDate": "2020-09-01",
"merchantId": "1234",
"categoryId": "3",
"amount": "5",
"description": "McDonalds"
}
How can I go about tweaking the lambda function to load all the transactions (> 8k) into DynamoDB as per above?