Create output from vars and outputs from module

Hi,
i’m trying create template file which i want to render in my output.

I have variable like this:

CLIENTS_LIST = [{
name = “client1”
supported_identity_providers = [“COGNITO”]
allowed_oauth_flows = [“code”]
allowed_oauth_scopes = [“email”, “openid”]
callback_urls = [“https://localhost:3000/login”]
prevent_user_existence_errors = “ENABLED”
generate_secret = true
},
{ name = “client2”
supported_identity_providers = [“COGNITO”]
allowed_oauth_flows = [“code”]
allowed_oauth_scopes = [“email”, “openid”]
callback_urls = [“https://my-app:3000/login”]
prevent_user_existence_errors = “ENABLED”
generate_secret = true
}]
}

and im using this in aws_cognito_user_pool_client resource in this way

count = length(var.CLIENTS_LIST)
name = lookup(var.CLIENTS_LIST[count.index], “name”, null)
callback_urls = lookup(var.ADDITIONAL_CLIENTS[count.index], “callback_urls”, null)
etc.

As you know this resource exporting id and secret to output.

and now i want create template file like this

name: “client1”
id: <id2.from.module.output>
secret: <secret2.from.module.output>

name: “client2”
id: <id2.from.module.output>
secret: <secret2.from.module.output>

i got solved id and secret but i have problem to take name from my variable.

Any ideas ?

It’s difficult to help here without a more concrete example of Terraform configuration that shows the problem. Can you post a reduced but working Terraform configuration that does part of what you want, so that we can suggest how to solve the rest?

(Please use the <> code block formatting, not the " quote to make it easier to read. Thanks!)

ok Maybe in other way.

i have 2 outups like this:

output "extra_client_id" {
  description = "The id of the user pool client"
  value       = aws_cognito_user_pool_client.extra.*.id
}
output "extra_client_secret" {
  description = "The client secret of user pool client"
  value       = aws_cognito_user_pool_client.extra.*.client_secret
}

those outputs have 3 values list each.

how i can create template file with contents like this

id: ${1_element_from_extra_client_id_output }
secret: ${1_element_from_extra_client_secret_output }


id: ${2_element_from_extra_client_id_output }
secret: ${2_element_from_extra_client_secret_output }


id: ${3_element_from_extra_client_id_output }
secret: ${3_element_from_extra_client_secret_output }

I see. What I’d recommend here is combining the two outputs using zipmap. Here’s an example using locals and an output:

locals {
  client_ids = tolist(["abc1", "def2", "efg3"])
  secrets    = tolist(["1234", "5678", "9012"])
  id_secrets = zipmap(local.client_ids, local.secrets)
}

output "id-secret" {
  value = templatefile("${path.module}/id-secret.tpl", {
    ids = local.client_ids,
    secrets = local.id_secrets,
  })
}

With this template:

%{ for id in ids ~}
id: ${id}
secret: ${secrets[id]}

%{ endfor }

The result is:

$ terraform output -raw id-secret
id: abc1
secret: 1234

id: def2
secret: 5678

id: efg3
secret: 9012

Is this what you’re looking for?

1 Like