11import os
2+ import time
3+
24import boto3
35import psycopg2
46
5- RDS_ENDPOINT = os .environ .get (' RDS_ENDPOINT' ) or ' http://localhost:4566'
7+ RDS_ENDPOINT = os .environ .get (" RDS_ENDPOINT" ) or " http://localhost:4566"
68
79
810def run_queries (instance ):
9- print ('Run DB queries against RDS instance %s' % instance ['DBInstanceIdentifier' ])
10- port = instance ['Endpoint' ]['Port' ]
11- conn = psycopg2 .connect ("dbname=test user=test password='test' host=localhost port=%s" % port )
11+ print ("Run DB queries against RDS instance %s" % instance ["DBInstanceIdentifier" ])
12+ port = instance ["Endpoint" ]["Port" ]
13+ conn = psycopg2 .connect (
14+ "dbname=test user=test password='test' host=localhost port=%s" % port
15+ )
1216 with conn .cursor () as cur :
13- cur .execute ('CREATE TABLE person ("id" INTEGER, "name" VARCHAR not null, PRIMARY KEY ("id"))' )
17+ cur .execute (
18+ 'CREATE TABLE person ("id" INTEGER, "name" VARCHAR not null, PRIMARY KEY ("id"))'
19+ )
1420 cur .execute ("INSERT INTO person VALUES (1, 'Jane')" )
1521 cur .execute ("INSERT INTO person VALUES (2, 'Alex')" )
1622 cur .execute ("INSERT INTO person VALUES (3, 'Maria')" )
@@ -21,28 +27,31 @@ def run_queries(instance):
2127
2228
2329def create_db ():
24- print (' Creating RDS DB instance' )
30+ print (" Creating RDS DB instance" )
2531 client = connect_rds ()
26- instance = client .create_db_instance (Engine = 'postgres' , DBInstanceClass = 'c1' , DBInstanceIdentifier = 'i1' )
27- return instance ['DBInstance' ]
32+ instance = client .create_db_instance (
33+ Engine = "postgres" , DBInstanceClass = "c1" , DBInstanceIdentifier = "i1"
34+ )
35+ return instance ["DBInstance" ]
2836
2937
3038def delete_db (instance ):
31- inst_id = instance [' DBInstanceIdentifier' ]
32- print (' Deleting RDS DB instance %s' % inst_id )
39+ inst_id = instance [" DBInstanceIdentifier" ]
40+ print (" Deleting RDS DB instance %s" % inst_id )
3341 client = connect_rds ()
3442 client .delete_db_instance (DBInstanceIdentifier = inst_id )
3543
3644
3745def connect_rds ():
38- return boto3 .client (' rds' , endpoint_url = RDS_ENDPOINT )
46+ return boto3 .client (" rds" , endpoint_url = RDS_ENDPOINT )
3947
4048
4149def main ():
4250 instance = create_db ()
51+ time .sleep (7 ) # wait for the instance to be ready
4352 run_queries (instance )
4453 delete_db (instance )
4554
4655
47- if __name__ == ' __main__' :
56+ if __name__ == " __main__" :
4857 main ()
0 commit comments