Skip to content

Instantly share code, notes, and snippets.

@bruceharrison1984
Last active March 2, 2021 15:42
Show Gist options
  • Save bruceharrison1984/219a92a7f87e95dab0b89772c9e1a486 to your computer and use it in GitHub Desktop.
Save bruceharrison1984/219a92a7f87e95dab0b89772c9e1a486 to your computer and use it in GitHub Desktop.
Bulk save AWS Secrets with Terraform
variable "base_name" {
description = "The prefix on created resources"
}
variable "secret_map" {
description = "A Key/Value map of secrets that will be added to AWS Secrets"
type = map(string)
}
variable "default_tags" {
description = "Tags to be applied to resources"
}
variable "secret_retention_days" {
default = 0
description = "Number of days before secret is actually deleted. Increasing this above 0 will result in Terraform errors if you redeploy to the same workspace."
}
resource "aws_secretsmanager_secret" "map_secret" {
for_each = var.secret_map
name = "/${terraform.workspace}/${each.key}"
recovery_window_in_days = var.secret_retention_days
tags = merge(var.default_tags, {
Name = "${var.base_name}-${each.key}"
})
}
resource "aws_secretsmanager_secret_version" "map_secret" {
for_each = aws_secretsmanager_secret.map_secret
secret_id = aws_secretsmanager_secret.map_secret[each.key].id
secret_string = var.secret_map[each.key]
}
output "secret_arns" {
value = zipmap(keys(aws_secretsmanager_secret_version.map_secret), values(aws_secretsmanager_secret_version.map_secret)[*].arn)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment