Hello,
Im trying to added multiple routes to multiple VPCs, this is part of VPC_PEERING between the new VPC and the other VPC that host things like CI/CD, database, scanner etc
my code
peering (works)
resource "aws_vpc_peering_connection" "peers_vpc" {
depends_on = [module.k8s_vpc]
count = length(local.peer_to)
vpc_id = module.k8s_vpc.vpc_id
peer_vpc_id = element(local.peer_to, count.index)
peer_owner_id = data.aws_caller_identity.current.account_id
auto_accept = true
accepter {
allow_remote_vpc_dns_resolution = true
}
requester {
allow_remote_vpc_dns_resolution = true
}
tags = {
Side = "Requester"
Name = "${local.vpc_name} <--> ${element(local.peer_to_label, count.index)}"
terraform = "true"
environment = local.environment
}
}
next works too: from VPC’s main route to peer’s main route
resource "aws_route" "peers_vpc" {
depends_on = [module.k8s_vpc,aws_vpc_peering_connection.peers_vpc]
count = length(local.peer_to)
route_table_id = module.k8s_vpc.vpc_main_route_table_id
destination_cidr_block = element(local.peer_to_cidr, count.index)
vpc_peering_connection_id = aws_vpc_peering_connection.peers_vpc[count.index].id
}
here is where im at lost any help really appreciated
from VPC’s subnets to peer’s subnet, private subnets
resource "aws_route" "private_subnets_to_peer_vpc" {
depends_on = [module.k8s_vpc,aws_vpc_peering_connection.peers_vpc]
for_each = [ for route in module.k8s_vpc.private_route_table_ids: {
count = length(local.peer_to)
route_table_id = route
destination_cidr_block = element(local.peer_to_cidr, count.index)
vpc_peering_connection_id = aws_vpc_peering_connection.peers_vpc[count.index].id
}]
}
the error:
Error: Missing required argument
on vpc_peering.tf line 85, in resource "aws_route" "private_subnets_to_peer_vpc":
85: resource "aws_route" "private_subnets_to_peer_vpc" {
The argument "route_table_id" is required, but no definition was found.
Error: Reference to "count" in non-counted context
on vpc_peering.tf line 90, in resource "aws_route" "private_subnets_to_peer_vpc":
90: destination_cidr_block = element(local.peer_to_cidr, count.index)
The "count" object can only be used in "module", "resource", and "data"
blocks, and only when the "count" argument is set.
Error: Reference to "count" in non-counted context
on vpc_peering.tf line 91, in resource "aws_route" "private_subnets_to_peer_vpc":
91: vpc_peering_connection_id = aws_vpc_peering_connection.peers_vpc[count.index].id
The "count" object can only be used in "module", "resource", and "data"
blocks, and only when the "count" argument is set.
thanks
ls