0

I have an existing VPC in a separate account and am creating independent terraforms for more vpc's in new accounts. I wish to peer specific vpc's within each account but am finding it hard to programatically obtain the vpc_id from the target vpc's. As these vpc's are ephemeral so the vpd_id's will change I want to code this instead of logging into the account to get the vpc_id from the console.

I have tried many variations of data sources and combined with resource "aws_vpc_peering_connection", resource "aws_vpc_peering_connection_accepter" but so far am failing.

Any help would be appreciated.

MattyG
  • 1
  • 1

1 Answers1

2

If you're VPC peering across accounts you need to instantiate multiple AWS providers in Terraform, one for each account. You should then be able to use the aws_vpc datasource with the specific provider instance for the peer account to retrieve the VPC id.

The documentation for aws_vpc_peering_connection_accepter shows how to use multiple provider instances. If the peer VPC already exists, rather than create it with Terraform then you should be able to do something like:

data "aws_vpc" "peer" {
  provider = "peer"
  # Whatever combination of filters are necessary to locate the VPC, cidr_block, etc.
}

You can then refer to ${data.aws_vpc.peer.id} in your aws_vpc_peering_connection resource.

bodgit
  • 4,751
  • 16
  • 27