Search code examples
amazon-web-servicesamazon-s3aws-lambdaamazon-rdsamazon-sqs

Read JSON from S3 and upload it to RDS using Lambda through


I am trying to read JSON from S3 and upload it to RDS using Lambda. The code in Lambda is as follows:

import json
import boto3
import pymysql
s3_client = boto3.client('s3')
def lambda_handler(event, context):
    bucket = event['Records']['0']['s3']['bucket']['name']
    json_file_name = event['Records'][0]['s3']['object']['key']
    json_object = s3_client.get_object(Bucket=bucket,Key=json_file_name)
    jsonFileReader = json_object['Body'].read()
    jsonDict = json.loads(jsonFileReader)
    results = []
    for item in (jsonDict):
         results.append(row.values())
    print(results)
    conn = pymysql.connect(host = 'rchd.cypgvdgyrj6p.us-east-1.rds.amazonaws.com',user = 'rchd',passwd = 'rchdrchd',db = 'rchd')
    cursor = conn.cursor()
    pymysql_insert = cursor.execute("INSERT INTO rchd2(uniquedataid, platformdetails, systemname, processorname, architecturaldetail, nodename) VALUES (%s, %s, %s, %s, %s, %s)"
    cursor.execute(pymysql_insert,results)
    conn.commit()
    cursor.close()
    conn.close()
    print(cursor.rowcount, "record inserted successfully into employee table")
    return {
        'statusCode' : 200,
        #'body' json.dumps('hello from lambda!')
       
    }

However, Lambda is giving the following error in Line 18:

Response
{
  "errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 18)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "requestId": "52039e38-4d6a-4a6f-a29f-b47ae91c7f37",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 18\n        cursor.execute(pymysql_insert,results)\n"
  ]
}

Function Logs
START RequestId: 52039e38-4d6a-4a6f-a29f-b47ae91c7f37 Version: $LATEST
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 18)
Traceback (most recent call last):
  File "/var/task/lambda_function.py" Line 18
        cursor.execute(pymysql_insert,results)END RequestId: 52039e38-4d6a-4a6f-a29f-b47ae91c7f37

Based on the error i think its in my SQL insertion command. I tried many times to fix it. But unable to do so.

Can anyone let me know how to correct it?

Any help would be appreciated.


Solution

  • You are missing closing parentheses:

        pymysql_insert = cursor.execute("INSERT INTO rchd2(uniquedataid, platformdetails, systemname, processorname, architecturaldetail, nodename) VALUES (%s, %s, %s, %s, %s, %s)")