I have a terraform module as follows
module "vpc" {
source = "./modules"
my_aws_account_id = "<aws_account_id>"
my_aws_vpc_id = "<aws_vpc_id>"
my_vpc_peering_connections = {
stage = "<aws_stage_vpc_id>"
preprod = "<aws_preprod_vpc_id>"
}
my_vpc_route_table = {
default = {
name = "test-default"
routes = [{
cidr_block = "10.10.16.0/20"
vpc_peering_connection_id = module.vpc.op_vpc_peering_connections["stage"]
}, {
cidr_block = "10.10.32.0/20"
vpc_peering_connection_id = module.vpc.op_vpc_peering_connections["preprod"]
}]
}
}
}
and my variable looks like this
variable "my_aws_account_id" {
type = string
}
variable "my_aws_vpc_id" {
type = string
}
variable "my_vpc_peering_connections" {
type = map(string)
}
variable "my_vpc_route_table" {
type = object({
default = object({
name = string
routes = list(object({
cidr_block = string
vpc_peering_connection_id = string
}))
})
})
}
resource
resource "aws_vpc_peering_connection" "peering_connections" {
for_each = var.my_vpc_peering_connections
peer_owner_id = var.my_aws_account_id
peer_vpc_id = each.value
vpc_id = var.my_aws_vpc_id
tags = {
Name = each.key
}
timeouts {}
}
resource "aws_route_table" "default_route_table" {
vpc_id = var.my_aws_vpc_id
tags = {
Name = var.my_vpc_route_table.default.name
}
dynamic "route" {
for_each = var.my_vpc_route_table.default.routes
content {
cidr_block = lookup(route.value, "cidr_block")
vpc_peering_connection_id = lookup(route.value, "vpc_peering_connection_id")
instance_id = ""
gateway_id = ""
carrier_gateway_id = ""
destination_prefix_list_id = ""
egress_only_gateway_id = ""
ipv6_cidr_block = ""
local_gateway_id = ""
nat_gateway_id = ""
network_interface_id = ""
transit_gateway_id = ""
vpc_endpoint_id = ""
}
}
depends_on = [
aws_vpc_peering_connection.peering_connections
]
}
output
output op_vpc_peering_connections {
value = tomap({
for name, details in aws_vpc_peering_connection.peering_connections : name => details.id
})
}
later when I run
> terraform import module.vpc.aws_vpc_peering_connection.peering_connections["stage"] <peering_connection_id>
> terraform import module.vpc.aws_vpc_peering_connection.peering_connections["preprod"] <peering_connection_id>
I get the following error
╷
│ Error: Invalid index
│
│ on /Users/sarad/workspace/personal/terraform-for-each/main.tf line 33, in module "vpc":
│ 33: vpc_peering_connection_id = module.vpc.op_vpc_peering_connections["preprod"]
│ ├────────────────
│ │ module.vpc.op_vpc_peering_connections is map of string with 1 element
│
│ The given key does not identify an element in this collection value.
╵
the error goes comes if I have more than 1 element in op_vpc_peering_connections
and I am trying to reference them in module by name like module.vpc.op_vpc_peering_connections["stage"]
or module.vpc.op_vpc_peering_connections["preprod"]
also I am unable to debug the output with terraform output
where I get the following warning
Warning: No outputs found
│
│ The state file either has no outputs defined, or all the defined outputs are empty. Please define an output in your configuration with the `output` keyword and run `terraform
│ refresh` for it to become available. If you are using interpolation, please verify the interpolated value is not empty. You can use the `terraform console` command to assist.
╵