Conditionally create resource using for_each

Hey there,

Amateur with Terraform, but trying to figure out what I think should be somewhat simple.

I'm using Terraform Cloud to manage workspaces. I need to update a variable across 200+ workspaces. The TFE module doesn't support wildcard searches, so I'm needing to parse for all workspaces and drill down to just the workspaces I'm looking for in a for_each.

data "tfe_workspace_ids" "all" { names = ["*"] organization = "my-org" } resource "tfe_variable" "my-ws" { for_each = contains(data.tfe_workspace_ids.all.ids, "dev-workspace") key = "access_key" value = "XXXX" category = "terraform" workspace_id = each.value sensitive = true description = "AWS IAM secret access key." } 

My workspace names are consistent, but I need to find all workspaces that start with "dev-workspace" in this case and only create resources for those from the data block above.

Getting this error when attempting to do so:

Error: Error in function call on main.tf line 16, in resource "tfe_variable" "my-ws": 16: for_each = contains(data.tfe_workspace_ids.all.ids, "dev-workspace") |---------------- | data.tfe_workspace_ids.all.ids is map of string with 284 elements Call to function "contains" failed: argument must be list, tuple, or set. 

I've tried changing the data to the formats it's suggesting, but it doesn't accept what I'm feeding it.

A sample of the output from the data block:

aa = { "dev-workspace-2" = "ws-anonymized" "dev-workspace-5" = "ws-anonymized" "prod-workspace-2" = "ws-anonymized" "test-workspace-1" = "ws-anonymized" } 

Would appreciate any help with this. Thanks!

submitted by /u/jmctune
[link] [comments]

Read more here: https://www.reddit.com/r/Terraform/comments/lq2c8o/conditionally_create_resource_using_for_each/

Content Attribution

This content was originally published by /u/jmctune at Terraform, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: