Create multiple random passwords in Terraform and AWS Secrets and use them in resources

Viewed 30

I would like to create multiple random passwords in Terraform, create a Secrets in AWS Secrets Manager and use these passwords in my resources block in Terraform. I have tried different ways but I am not able to find a solution that works. Here is what I have tried:

locals {
  list_of_previously_created_resources = [resource_type.X, resource_type.Y]
}

resource "random_password" "generate" {
    for_each            = local.list_of_previously_created_resources
    length              = 16
    special             = true
}

resource "aws_secretsmanager_secret" "secrets-instance" {
    for_each    = local.list_of_previously_created_resources
    name        = each.value
}

resource "aws_secretsmanager_secret_version" "password" {
    for_each        = local.list_of_previously_created_resources
    secret_id       = aws_secretsmanager_secret.secrets-instance.id
    secret_string   = <<EOF
    {
        "username": "AdminPassword",
        "password": "${random_password.generate.result}"
        }
EOF
}

resource "resource_type" "X" {
    AdminPassword = 
}

resource "resource_type" "Y" {
    AdminPassword = 
}
0 Answers
Related