I have same-named secrets across all of my 4 environments. The problem is AWS adds characters at the end of the secret ARN, meaning all of my secrets have different "names", besides their environment, which is not a problem. The specific problem can be described with this:
For example, secret called "dbpass" looks like this on the dev account:
data "aws_secretsmanager_secret" "s1" {
arn = "arn:aws:secretsmanager:us-east-1:xxx:secret:/dev/dbpass-123456"
}
While a secret with the same name looks like this on prod:
data "aws_secretsmanager_secret" "s1" {
arn = "arn:aws:secretsmanager:us-east-1:xxx:secret:/prod/dbpass-654321"
}
These numbers after dbpass
are randomized, I'm guessing for security reasons. But this created the following problem for me: While I can turn the region, account ID and environment into variables whose value depends on the environment I'm deploying to, I can't do the same with the secret arn. These 4 environments are all deployed with a single pipeline, so its very important I have 1 resource acting as a representative for all 4 environments. Now, this can be solved like this:
locals{
dev = "${var.env == "dev" ? "dbpass-123456" : ""}"
prod = "${var.env == "prod" ? "dbpass-654321" : ""}"
dbpass = "${coalesce(local.dev, local.prod)}"
}
And then I could just do:
data "aws_secretsmanager_secret" "s1" {
arn = "arn:aws:secretsmanager:${var.region}:${var.account}:secret:/${var.env}/${local.dbpass}"
}
The problem here however, is that I'd have to make this locals
block for every single secret, go into each AWS account, retrieve them... and there's a lot of secrets. Terraform doesn't let me call a secret by its name/path in the Secrets Manager, only by its ARN. Is there a way to make this easier, or am I stuck with hundreds of locals
blocks?