CodexBloom - Programming Q&A Platform

Terraform - implementing Managing Multiple VPC Peering Connections with Dynamic Blocks

👀 Views: 1108 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-15
terraform aws vpc infrastructure-as-code HCL

I've tried everything I can think of but I'm stuck on something that should probably be simple. I'm working with issues when trying to manage multiple VPC peering connections in AWS using Terraform. My goal is to create a module that dynamically establishes peering connections based on a list of VPC IDs passed as a variable. However, I'm running into an behavior regarding the dependency management between the peering connections. When I apply my configuration, I receive the following behavior: ``` behavior: behavior creating VPC Peering Connection: InvalidVpcPeeringConnectionID.NotFound: The specified VPC Peering Connection ID does not exist ``` I suspect that the question is related to how I'm defining my dynamic blocks for the VPC peering connections. Here's my current code snippet: ```hcl variable "vpc_ids" { type = list(string) } resource "aws_vpc_peering_connection" "peering" { count = length(var.vpc_ids) vpc_id = aws_vpc.my_vpc.id peer_vpc_id = element(var.vpc_ids, count.index) tags = { Name = "Peering-${count.index}" } } resource "aws_vpc" "my_vpc" { cidr_block = "10.0.0.0/16" } ``` I've also tried defining the VPC resource inside the same module, but it didn't resolve the scenario. When I run `terraform apply`, it seems to apply the peering connections in the wrong order, which might be causing the referenced VPC IDs not to be recognized. Is there a best practice for managing dependencies in this scenario or an alternative approach I should consider? I'm currently using Terraform version 1.0.4. For context: I'm using Hcl on Linux. What am I doing wrong?