Terraform allows you to manage infrastructure across multiple cloud providers and accounts. However, creating a VPC peering connection between two accounts involves a few steps and requires configuration on both sides. Here's how you can do it in a single Terraform module, but remember you need to have the required permissions in both AWS accounts.
Below is an example of a Terraform module that sets up a VPC peering connection between two VPCs in different AWS accounts. The Terraform code is split into two parts: one for the requester's side and one for the accepter's side.
variable "requester_vpc_id" {}
variable "requester_aws_region" {}
variable "requester_aws_profile" {}
variable "accepter_vpc_id" {}
variable "accepter_aws_region" {}
variable "accepter_aws_profile" {}
# Requester's side of the VPC Peering Connection
provider "aws" {
region = var.requester_aws_region
profile = var.requester_aws_profile
alias = "requester"
}
resource "aws_vpc_peering_connection" "peer" {
provider = aws.requester
vpc_id = var.requester_vpc_id
peer_vpc_id = var.accepter_vpc_id
peer_region = var.accepter_aws_region
peer_owner_id = data.aws_caller_identity.accepter.account_id
auto_accept = false
tags = {
Name = "vpc-peering-connection"
}
}
# Accepter's side of the VPC Peering Connection
provider "aws" {
region = var.accepter_aws_region
profile = var.accepter_aws_profile
alias = "accepter"
}
# This data source fetches the account ID of the accepter
data "aws_caller_identity" "accepter" {
provider = aws.accepter
}
resource "aws_vpc_peering_connection_accepter" "peer_accept" {
provider = aws.accepter
vpc_peering_connection_id = aws_vpc_peering_connection.peer.id
auto_accept = true
tags = {
Name = "vpc-peering-connection-accept"
}
}
output "requester_vpc_peering_connection_id" {
value = aws_vpc_peering_connection.peer.id
}
output "accepter_vpc_peering_connection_id" {
value = aws_vpc_peering_connection_accepter.peer_accept.id
}
This Terraform module defines two providers, one for the requester account and one for the accepter account. It then creates a VPC peering connection on the requester's side and accepts the peering connection on the accepter's side.
When you run terraform apply, you will need to provide the required variables either through a terraform.tfvars file, environment variables, or by entering them when prompted.
Here's an example terraform.tfvars file:
requester_vpc_id = "vpc-XXXXXXXXXXXXXXXXX"
requester_aws_region = "us-west-2"
requester_aws_profile = "requester-profile"
accepter_vpc_id = "vpc-YYYYYYYYYYYYYYYYY"
accepter_aws_region = "us-west-2"
accepter_aws_profile = "accepter-profile"
Make sure to replace XXXXXXXXXXXXXXXXX and YYYYYYYYYYYYYYYYY with the actual VPC IDs, and requester-profile and accepter-profile with the profile names set up in your AWS credentials file.
Keep in mind that for this to work, you will need to have configured AWS CLI profiles with the necessary permissions to manage VPCs and peering connections on both accounts.
Lastly, always review Terraform's execution plan before applying it, to ensure that it's going to create the resources you expect.