-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrds
29 lines (28 loc) · 1018 Bytes
/
rds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import json
import pymysql
# RDS settings
rds_host = "" #os.environ['RDS_HOST']
username = "" #os.environ['DB_USERNAME']
password = "" #os.environ['DB_PASSWORD']
db_name = "" #os.environ['DB_NAME']
import pymysql
# Establishing a connection to the RDS instance
def lambda_handler(event, context):
try:
connection = pymysql.connect(host=rds_host, user=username, passwd=password, db=db_name, connect_timeout=5)
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM your_table_name")
result = cursor.fetchall()
print(result)
connection.close()
except pymysql.MySQLError as e:
print(f"Error connecting to MySQL Platform: {e}")
return {
'statusCode': 500,
'body': json.dumps('Error connecting to database')
}
return {
'statusCode': 200,
'body': json.dumps('Data retrieved successfully')
}