I’m new to Terraform, having recently transitioned from CloudFormation. The resources for my service were initially created using CloudFormation but are now managed by Terraform.
I’ve encountered a challenge:
If a new region is introduced in the future, my Terraform code should be able to create and manage all required resources without relying on the old CloudFormation code.
I need to create some resources conditionally using Terraform:
-
If it is a new region(that means there is no secret created yet), then create these additional resources.
-
If it is an existing region(the secret for this region is present), no need to create these resources.
The count meta-argument isn’t working for this scenario. Here’s the list of resources I need to manage conditionally:
resource "aws_secretsmanager_secret" "new_db_password" {
count = local.secret_exists ? 0 : 1
name = "${var.SecretPrefix}-my-service/RDS/password"
description = "password for new my service RDS"
tags = {
"Application" = "${var.SecretPrefix}-my-service"
"resourceowner" = "${var.owner}"
}
tags_all = {
"Application" = "${var.SecretPrefix}-my-service"
"resourceowner" = "${var.owner}"
}
}
resource "random_password" "new_rds_password" {
count = local.secret_exists ? 0 : 1
length = 12
special = false
}
resource "aws_secretsmanager_secret_version" "my_rds_pwd_secret_version" {
count = local.secret_exists ? 0 : 1
secret_id = aws_secretsmanager_secret.new_db_password.id
secret_string = jsonencode({
password = random_password.new_rds_password.result
})
}
Any advice or examples on how to achieve this would be greatly appreciated!
Thanks in advance!
I tried using count meta argument, but it doesn’t work
abk007 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.