maybe a really stupid question here and I'm guessing this is actually nothing like as complicated as I'm finding it. However, after spending hours and hours reading and trying, while I've learnt a lot about Terraform, I'm really no closer to a solution which I can understand or reuse.
I'm trying to do something that would be pretty simple in bash or python but seems to require some trickery in Terraform. I have two variables that I want to "combine" so that I have a valid block of data usable in for_each looping for resource creation. It's quite likely that I have failed to do this properly from the beginning in terms of variable declaration. So please forgive my beginner invompetence.
Just to note also, this is mostly for my own learning so not necessarily a practical example bur rather a use case that illustrates what I want to understand better. In this context, my use case is to provision workspaces in Terraform cloud and to insert the same set of private variables in to each workspace created. aka create workspaces and push in azure service principle authentication variables.
So in variables.tf I have
variable "env_names" {
type = set(string)
default = ["dev", "uat", "prod"]
}
variable "auth_vars" {
default = [
{ key = "subscription_id"
value = "XXXXXXXXXXXXXXXXXXXXXXXX"
},
{
key = "client_id"
value = "XXXXXXXXXXXXXXXXXXXX"
},
{
key = "client_secret"
value = "XXXXXXXXXXXXXXXXXXX"
},
{
key = "tenant_id"
value = "XXXXXXXXXXXXXXXXXXX"
}
]
}
Then I want to use these variables to create workspaces with the set of variables applied to each workspace. For the workspace creation, I have no problems creating them using.
resource "tfe_workspace" "cloud_workspace" {
for_each = var.env_names
name = "MyWorkspace-${each.key}"
organization = "MyOrg"
execution_mode = "remote"
auto_apply = "false"
allow_destroy_plan = "true"
global_remote_state = "false"
}
It's the creation of the variables for the workspaces which is a major headache for me. I have tried all sorts of manipulations using locals, flattening the data, doing a setproduct etc. I think it's here that I really don't know how to approach this problem. I have tried to do things like:
locals {
auth_map = flatten([
for w in var.env_names : [
for v in var.auth_vars : {
workspace = w
key = v.key
value = v.value
}
]
])
}
This does seem to create a structure that makes sense as I have a list of all the tuple values required. aka
{
key = "subscription_id"
value = "XXXXXX"
workspace = "dev"
},
{
key = "client_id"
value = "XXXXXXXXXXXX"
workspace = "dev"
},
ETC.....
I just have no idea how to get this list of tuples in to a useful form so that I can create all the variables for the workspaces. I'd want to use the data to create multiple variable blocks of the type below. I'm not really sure how to get there though.
resource "tfe_variable" "azure-credentials" {
key = ""
value = ""
category = "terraform"
workspace_id = ""
}
I'm aware that I could have simply done a few blocks of code and solved this use case long ago, but it's more for my understanding of how Terraform works and how to deal with these more complex situations. Any advice appreciated as I'm really at the start of learning Terraform properly and am well out of my depth here.
Thank you so much for taking the time