Terraform module outputs

Hey,

I have a module and one of the resource is using for_each with an outputs.tf:

output "private_subnet" {
 value = values(aws_subnet.private).*.id
}

In order to use the output value I have to reference it as:

module.vpc.private_subnet[0]

I’m wondering if it’s possible to alter it so that I can call it using via a name instead? e.g.

module.vpc[“db”].private_subnet

Thank you

Is

literally what you mean? Or was it accidental to move the square brackets to a different place in the expression?

i.e. did you mean:

module.vpc.private_subnet["db"]

?

You mentioned that

but you haven’t shown the details of the for_each. Is "db" literally one of the keys of the for_each ?

Hi,

Sorry may not have explained it that well. I can only use to reference the value:

module.vpc.private_subnet[0]

I’m asking if it’s possible to change it so I can reference it using a word instead of a number? Thank you

Since you are indexing private_subnet with a numeric value, it must be a series of values like a list or tuple. The index for a series is a number, there is no “word” equivalent.

If you need to find a value in the series via some other attribute, you can use a for expression to iterate through the series to find the value you need. For example, assuming values here is a list of strings, it might look like:

value = one([for v in local.values : v if v == "desired" ])
1 Like