@@ -31,7 +31,8 @@ def lambda_handler(event, context):
31
31
'username': <required: username>,
32
32
'password': <required: password>,
33
33
'dbname': <optional: database name, default to 'postgres'>,
34
- 'port': <optional: if not specified, default port 5432 will be used>
34
+ 'port': <optional: if not specified, default port 5432 will be used>,
35
+ 'replica_host': <optional: host address of replica DB>
35
36
}
36
37
37
38
Args:
@@ -126,7 +127,11 @@ def create_secret(service_client, arn, token):
126
127
current_dict ['password' ] = passwd ['RandomPassword' ]
127
128
128
129
# Add DATABASE_URL to secret
129
- current_dict ['DATABASE_URL' ] = dict_to_url (current_dict )
130
+ current_dict ['DATABASE_URL' ] = dict_to_url (current_dict , false )
131
+
132
+ if secret ['replica_host' ]:
133
+ # Add DATABASE_REPLICA_URL to secret
134
+ current_dict ['DATABASE_REPLICA_URL' ] = dict_to_url (current_dict , true )
130
135
131
136
# Put the secret
132
137
service_client .put_secret_value (SecretId = arn , ClientRequestToken = token , SecretString = json .dumps (current_dict ), VersionStages = ['AWSPENDING' ])
@@ -278,7 +283,7 @@ def finish_secret(service_client, arn, token):
278
283
service_client .update_secret_version_stage (SecretId = arn , VersionStage = "AWSCURRENT" , MoveToVersionId = token , RemoveFromVersionId = current_version )
279
284
logger .info ("finishSecret: Successfully set AWSCURRENT stage to version %s for secret %s." % (token , arn ))
280
285
281
- def dict_to_url (secret ):
286
+ def dict_to_url (secret , replica ):
282
287
"""Reformats connection details as a URL string
283
288
284
289
Generate a Heroku-style DATABASE_URL with connection details
@@ -289,9 +294,13 @@ def dict_to_url(secret):
289
294
Returns:
290
295
url: DATABASE_URL-style string
291
296
"""
297
+ if replica :
298
+ host = secret ['host' ]
299
+ else :
300
+ host = secret ['replica_host' ]
292
301
293
302
return "postgres://%s:%s@%s:%s/%s" % (secret ['username' ],
294
- secret ['password' ], secret [ ' host' ] , secret ['port' ],
303
+ secret ['password' ], host , secret ['port' ],
295
304
secret ['dbname' ])
296
305
297
306
def get_connection (secret_dict ):
0 commit comments