Skip to content

Commit b58d985

Browse files
committed
Adjust admin-login module to handle replica
Saves the replica database url to the same secrets so its value can be exposed as env variable when needed.
1 parent 8ccfd71 commit b58d985

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

rds-postgres/admin-login/main.tf

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ module "secret" {
99
trust_tags = var.trust_tags
1010

1111
initial_value = jsonencode({
12-
dbname = var.database_name
13-
engine = data.aws_db_instance.this.engine
14-
host = data.aws_db_instance.this.address
15-
password = var.initial_password
16-
port = tostring(data.aws_db_instance.this.port)
17-
username = var.username
12+
dbname = var.database_name
13+
engine = data.aws_db_instance.this.engine
14+
host = data.aws_db_instance.this.address
15+
replica_host = data.aws_db_instance.replica.address
16+
password = var.initial_password
17+
port = tostring(data.aws_db_instance.this.port)
18+
username = var.username
1819
})
1920
}
2021

@@ -77,6 +78,10 @@ data "aws_db_instance" "this" {
7778
db_instance_identifier = var.identifier
7879
}
7980

81+
data "aws_db_instance" "replica" {
82+
db_instance_identifier = var.replica_identifier
83+
}
84+
8085
locals {
8186
full_name = join("-", ["rds-postgres", var.identifier])
8287
}

rds-postgres/admin-login/rotation/lambda_function.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def lambda_handler(event, context):
3131
'username': <required: username>,
3232
'password': <required: password>,
3333
'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>
3536
}
3637
3738
Args:
@@ -126,7 +127,11 @@ def create_secret(service_client, arn, token):
126127
current_dict['password'] = passwd['RandomPassword']
127128

128129
# 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)
130135

131136
# Put the secret
132137
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):
278283
service_client.update_secret_version_stage(SecretId=arn, VersionStage="AWSCURRENT", MoveToVersionId=token, RemoveFromVersionId=current_version)
279284
logger.info("finishSecret: Successfully set AWSCURRENT stage to version %s for secret %s." % (token, arn))
280285

281-
def dict_to_url(secret):
286+
def dict_to_url(secret, replica):
282287
"""Reformats connection details as a URL string
283288
284289
Generate a Heroku-style DATABASE_URL with connection details
@@ -289,9 +294,13 @@ def dict_to_url(secret):
289294
Returns:
290295
url: DATABASE_URL-style string
291296
"""
297+
if replica:
298+
host = secret['host']
299+
else:
300+
host = secret['replica_host']
292301

293302
return "postgres://%s:%s@%s:%s/%s" % (secret['username'],
294-
secret['password'], secret['host'], secret['port'],
303+
secret['password'], host, secret['port'],
295304
secret['dbname'])
296305

297306
def get_connection(secret_dict):

rds-postgres/admin-login/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ variable "read_principals" {
3131
default = null
3232
}
3333

34+
variable "replica_identifier" {
35+
description = "Identifier of the database replica"
36+
type = string
37+
}
38+
3439
variable "secret_name" {
3540
description = "Override the name for this secret"
3641
type = string

0 commit comments

Comments
 (0)