Home > Net >  Terraform Set More than one variable in "Count"
Terraform Set More than one variable in "Count"

Time:12-07

I am using this below code:

data "aws_secretsmanager_secret_version" "test1" {
  count = var.test123.load_from_secrets_manager?1:0
   secret_id = "test1"
}

now this works fine when I run terraform init without any errors.

Now, my issue is that I need to add a new variable under the same data section, so like

data "aws_secretsmanager_secret_version" "test1" {
  count = var.test123.load_from_secrets_manager?1:0
  count = var.test456.load_from_secrets_manager?1:0
   secret_id = "test1"
}

when I run terraform init on this I get the ERROR:

The argument "count" was already set. Each argument may be set only once.

I need to figure out a way to add two or more vars in the SAME COUNT. any help would be appreciated.

CodePudding user response:

What you are really trying to accomplish here is to specify two conditionals that combine to determine if the data block should attempt to execute a READ. You can do this by combining the logic:

data "aws_secretsmanager_secret_version" "test1" {
  count = (var.test123.load_from_secrets_manager && var.test456.load_from_secrets_manager) ? 1 : 0
  secret_id = "test1"
}

You also may want to update this to use the for_each meta-argument instead:

data "aws_secretsmanager_secret_version" "test1" {
  for_each = (var.test123.load_from_secrets_manager && var.test456.load_from_secrets_manager) ? toset(["test1"]) : []
  secret_id = "test1"
}
  • Related