1010def run_queries (instance ):
1111 print ("Run DB queries against RDS instance %s" % instance ["DBInstanceIdentifier" ])
1212 port = instance ["Endpoint" ]["Port" ]
13- conn = psycopg2 .connect (
14- "dbname=test user=test password='test' host=localhost port=%s" % port
15- )
13+ host = instance ["Endpoint" ].get ("Address" ) or "localhost.localstack.cloud"
14+ conn = None
15+ for _ in range (30 ):
16+ try :
17+ conn = psycopg2 .connect (
18+ "dbname=test user=test password='test' host=%s port=%s" % (host , port )
19+ )
20+ break
21+ except psycopg2 .OperationalError :
22+ time .sleep (2 )
23+ if conn is None :
24+ raise RuntimeError ("RDS Postgres endpoint did not become ready" )
25+
1626 with conn .cursor () as cur :
1727 cur .execute (
1828 'CREATE TABLE person ("id" INTEGER, "name" VARCHAR not null, PRIMARY KEY ("id"))'
@@ -30,7 +40,12 @@ def create_db():
3040 print ("Creating RDS DB instance" )
3141 client = connect_rds ()
3242 instance = client .create_db_instance (
33- Engine = "postgres" , DBInstanceClass = "c1" , DBInstanceIdentifier = "i1"
43+ Engine = "postgres" ,
44+ DBInstanceClass = "c1" ,
45+ DBInstanceIdentifier = "i1" ,
46+ DBName = "test" ,
47+ MasterUsername = "test" ,
48+ MasterUserPassword = "test" ,
3449 )
3550 return instance ["DBInstance" ]
3651
@@ -48,7 +63,6 @@ def connect_rds():
4863
4964def main ():
5065 instance = create_db ()
51- time .sleep (7 ) # wait for the instance to be ready
5266 run_queries (instance )
5367 delete_db (instance )
5468
0 commit comments