Repository navigation
Expand file tree
/
Copy pathquery.py
More file actions
71 lines (56 loc) · 1.95 KB
/
Copy pathquery.py
File metadata and controls
71 lines (56 loc) · 1.95 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
61
62
63
64
65
66
67
68
69
70
71
import os
import time
import boto3
import psycopg2
RDS_ENDPOINT = os.environ.get("RDS_ENDPOINT") or "http://localhost:4566"
def run_queries(instance):
print("Run DB queries against RDS instance %s" % instance["DBInstanceIdentifier"])
port = instance["Endpoint"]["Port"]
host = instance["Endpoint"].get("Address") or "localhost.localstack.cloud"
conn = None
for _ in range(30):
try:
conn = psycopg2.connect(
"dbname=test user=test password='test' host=%s port=%s" % (host, port)
)
break
except psycopg2.OperationalError:
time.sleep(2)
if conn is None:
raise RuntimeError("RDS Postgres endpoint did not become ready")
with conn.cursor() as cur:
cur.execute(
'CREATE TABLE person ("id" INTEGER, "name" VARCHAR not null, PRIMARY KEY ("id"))'
)
cur.execute("INSERT INTO person VALUES (1, 'Jane')")
cur.execute("INSERT INTO person VALUES (2, 'Alex')")
cur.execute("INSERT INTO person VALUES (3, 'Maria')")
with conn.cursor() as cur:
cur.execute("SELECT * FROM person")
result = cur.fetchall()
print(result)
def create_db():
print("Creating RDS DB instance")
client = connect_rds()
instance = client.create_db_instance(
Engine="postgres",
DBInstanceClass="c1",
DBInstanceIdentifier="i1",
DBName="test",
MasterUsername="test",
MasterUserPassword="test",
)
return instance["DBInstance"]
def delete_db(instance):
inst_id = instance["DBInstanceIdentifier"]
print("Deleting RDS DB instance %s" % inst_id)
client = connect_rds()
client.delete_db_instance(DBInstanceIdentifier=inst_id, SkipFinalSnapshot=True)
def connect_rds():
return boto3.client("rds", endpoint_url=RDS_ENDPOINT)
def main():
instance = create_db()
run_queries(instance)
delete_db(instance)
if __name__ == "__main__":
main()