-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathapp.py
More file actions
60 lines (48 loc) · 1.46 KB
/
app.py
File metadata and controls
60 lines (48 loc) · 1.46 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#SPDX-License-Identifier: MIT-0
import json
import aurora_dsql_psycopg2 as dsql
import os
cluster_endpoint = os.environ['cluster_endpoint']
region = os.environ['AWS_REGION']
def lambda_handler(event, context):
config = {
'host': cluster_endpoint,
'region': region,
'user': "admin",
}
# Make a connection to the cluster
conn = dsql.connect(**config)
try:
with conn.cursor() as cur:
conn.commit()
except Exception as e:
conn.close()
raise e
conn.set_session(autocommit=True)
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS users")
cur.execute(b"""
CREATE TABLE IF NOT EXISTS users(
id uuid NOT NULL DEFAULT gen_random_uuid(),
name varchar(30) NOT NULL,
city varchar(80) NOT NULL,
telephone varchar(20) DEFAULT NULL,
PRIMARY KEY (id))"""
)
# Insert some rows
cur.execute("INSERT INTO users(name, city, telephone) VALUES('John', 'LA', '555-555-0150')")
# Read back what we have inserted
cur.execute("SELECT * FROM users")
row = cur.fetchone()
print(row)
# return JSON back to API Gateway
return {
'statusCode': 200,
'body': json.dumps({
'id': str(row[0]),
'name': row[1],
'city': row[2],
'telephone': row[3]
})
}