Variable inside a variable

I would like to use a variable inside a variable.

This is my resource:

resource "aws_route" "vpc_peering_accepter" {
provider = "aws.accepter"
count = length(data.terraform_remote_state.vpc.outputs.${var.region}-vpc-private_routing_tables)
route_table_id = tolist(data.terraform_remote_state.vpc.outputs.${var.region}-vpc-private_routing_tables)[count.index]
destination_cidr_block = var.vpc_cidr
vpc_peering_connection_id = aws_vpc_peering_connection.peer.*.id[0]

Of course this one is not working.
What’s the best practice to do it?


The simplest way is to add a locals block which creates a new variable with the results of that expression, and then provide that new variable to count.

I think that something like

count = length(data.terraform_remote_state.vpc.outputs["${var.region}-vpc-private_routing_tables"])

should work