I am attempting to use the following Lambda Function on AWS to automatically import objects from an S3 bucket into an RDS instance as they are uploaded into the bucket.
import boto3
import pymysql
s3_cient = boto3.client('s3')
# Read CSV file content from S3 bucket
def read_data_from_s3(event):
bucket_name = event["Records"][0]["s3"]["bucket"]["name"]
s3_file_name = event["Records"][0]["s3"]["object"]["key"]
resp = s3_cient.get_object(Bucket="bucket_name", Key=s3_file_name)
data = resp['Body'].read().decode('utf-8')
data = data.split("\n")
return data
def lambda_handler(event, context):
rds_endpoint = "rds-endpoint"
username = "name"
password = "pass" # RDS Mysql password
db_name = "db-name" # RDS MySQL DB name
conn = None
try:
conn = pymysql.connect(rds_endpoint, user=username, passwd=password, db=db_name, connect_timeout=5)
except pymysql.MySQLError as e:
print("ERROR: Unexpected error: Could not connect to MySQL instance.")
try:
cur = conn.cursor()
cur.execute("INSERT INTO `db`.`table`(`sensor`,`sIP`,`dIP`,`sPort`) VALUES(<{sensor: }>,<{sIP: }>,<{dIP: }>,<{sPort: }>);")
conn.commit()
except:
pass
data = read_data_from_s3(event)
with conn.cursor() as cur:
for ent in data: # Iterate over S3 csv file content and insert into MySQL database
try:
ent = ent.replace("\n","").split(",")
print (">>>>>>>"+str(ent))
cur.execute('insert into db`.`table (sensor,Sip,Dip,sPort) values("'+str(ent[1])+'")')
conn.commit()
except:
continue
cur.execute("select * from table")
# Display table records
for row in cur:
print (row)
if conn:
conn.commit()
Whenever I run this, I get the following error and I do not understand what I am doing wrong? Would someone be able to clarify my errors?
[ERROR] TypeError: __init__() takes 1 positional argument but 2 positional arguments (and 4 keyword-only arguments) were given
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 23, in lambda_handler
conn = pymysql.connect(rds_endpoint, user=username, passwd=password, db=db_name, connect_timeout=5)
It seems that pymysql.connect
only takes in keyword arguments. You are passing rds_endpoint
in as a positional argument. The rds_endpoint
argument should probably passed in as host=rds_endpoint
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.