1

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.

4
  • 1
    The syntax error is in your python. You're missing a closing parenthesis at the end of the line running cursor.execute. You're also ignoring any errors that come out of mysql. This will make your program difficult to debug. Invest in tests for your lambda and a local environment that it can execute against. Commented Jan 16, 2022 at 1:32
  • 1
    event['Records']['0'] vs event['Records'][0] also looks like an error. Commented Jan 16, 2022 at 1:38
  • Check your cursor.execute. Why run twice. Just pass the INSERT stmt. Commented Jan 16, 2022 at 2:47
  • thanks @DanielFarrell.. Commented Jan 16, 2022 at 23:31

1 Answer 1

1

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)")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.