CodexBloom - Programming Q&A Platform

Terraform: Difficulty Configuring Multiple VPC Peering Connections with Unique Routes

๐Ÿ‘€ Views: 25 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-12
terraform aws vpc-peering hcl

I'm getting frustrated with Hey everyone, I'm running into an issue that's driving me crazy... I'm following best practices but I'm relatively new to this, so bear with me. After trying multiple solutions online, I still can't figure this out. I'm trying to set up multiple VPC peering connections between different AWS accounts using Terraform, but I'm running into issues with routing configurations. I have two VPCs in Account A and two VPCs in Account B, and I want to establish peering between each pair. I'm using Terraform version 1.2.0 and the AWS provider version 3.74.0. Hereโ€™s what Iโ€™ve set up so far: ```hcl resource "aws_vpc_peering_connection" "peer_a_to_b" { vpc_id = aws_vpc.vpc_a.id peer_vpc_id = aws_vpc.vpc_b.id auto_accept = true } resource "aws_route" "route_a_to_b" { route_table_id = aws_route_table.route_table_a.id destination_cidr_block = aws_vpc.vpc_b.cidr_block vpc_peering_connection_id = aws_vpc_peering_connection.peer_a_to_b.id } resource "aws_vpc_peering_connection" "peer_a_to_b2" { vpc_id = aws_vpc.vpc_a.id peer_vpc_id = aws_vpc.vpc_b2.id auto_accept = true } resource "aws_route" "route_a_to_b2" { route_table_id = aws_route_table.route_table_a.id destination_cidr_block = aws_vpc.vpc_b2.cidr_block vpc_peering_connection_id = aws_vpc_peering_connection.peer_a_to_b2.id } ``` When I run `terraform apply`, I get the following behavior: ``` behavior: behavior creating VPC Peering Connection: InvalidVpcPeeringConnectionState: The VPC peering connection is not in the "active" state. Current state: "pending-acceptance" ``` I've checked the configurations, and the VPCs have non-overlapping CIDR blocks. I also tried adding explicit dependencies using `depends_on`, but that didnโ€™t resolve the scenario. My routes seem to be configured correctly, but Iโ€™m uncertain if the peering connection is being established properly before the routes are applied. Do I need to manage the acceptance of the peering connection manually? If so, how can I automate this process within Terraform? Any guidance on best practices for handling multiple peering connections would be greatly appreciated! My development environment is Windows. What am I doing wrong? I'm on Ubuntu 22.04 using the latest version of Hcl. Has anyone dealt with something similar? I'm developing on Ubuntu 20.04 with Hcl. I appreciate any insights! I'm working with Hcl in a Docker container on Ubuntu 22.04. I'd really appreciate any guidance on this.