Below is a Terraform project to create a VPC on three AWS accounts with VPC peering to link all of them.
Directory structure:
aws-terraform-vpc-peering/
├── main.tf
├── provider.tf
├── variables.tf
├── outputs.tf
└── modules/
├── account_a
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
├── account_b
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
└── account_c
├── main.tf
├── variables.tf
└── outputs.tf
-
main.tf:
module "account_a" {
source = "./modules/account_a"
vpc_cidr_block = "10.0.0.0/16"
}
module "account_b" {
source = "./modules/account_b"
vpc_cidr_block = "10.1.0.0/16"
}
module "account_c" {
source = "./modules/account_c"
vpc_cidr_block = "10.2.0.0/16"
}
-
provider.tf:
provider "aws" {
alias = "account_a"
region = "us-west-2"
assume_role {
role_arn = "arn:aws:iam::AWS_ACCOUNT_A_ID:role/YourRoleName"
}
}
provider "aws" {
alias = "account_b"
region = "us-west-2"
assume_role {
role_arn = "arn:aws:iam::AWS_ACCOUNT_B_ID:role/YourRoleName"
}
}
provider "aws" {
alias = "account_c"
region = "us-west-2"
assume_role {
role_arn = "arn:aws:iam::AWS_ACCOUNT_C_ID:role/YourRoleName"
}
}
-
variables.tf:
variable "vpc_cidr_block" {
description = "The CIDR block for the VPC"
type = string
}
-
outputs.tf:
output "account_a_vpc_id" {
value = module.account_a.vpc_id
}
output "account_b_vpc_id" {
value = module.account_b.vpc_id
}
output "account_c_vpc_id" {
value = module.account_c.vpc_id
}
output "vpc_peering_connection_id_a_b" {
value = module.account_a.vpc_peering_connection_id
}
output "vpc_peering_connection_id_a_c" {
value = module.account_b.vpc_peering_connection_id
}
-
account_a module
resource "aws_vpc" "vpc_a" {
cidr_block = var.vpc_cidr_block
tags = {
Name = "VPC-Account-A"
}
}
resource "aws_vpc_peering_connection" "peering_a_b" {
vpc_id = aws_vpc.vpc_a.id
peer_vpc_id = var.peer_vpc_b_id
}
resource "aws_vpc_peering_connection" "peering_a_c" {
vpc_id = aws_vpc.vpc_a.id
peer_vpc_id = var.peer_vpc_c_id
}
variable "vpc_cidr_block" {
description = "The CIDR block for the VPC"
type = string
}
variable "peer_vpc_b_id" {
description = "The VPC ID of Account-B"
type = string
}
variable "peer_vpc_c_id" {
description = "The VPC ID of Account-C"
type = string
}
output "vpc_id" {
value = aws_vpc.vpc_a.id
}
output "vpc_peering_connection_id" {
value = aws_vpc_peering_connection.peering_a_b.id
}
-
account_b module
resource "aws_vpc" "vpc_b" {
cidr_block = var.vpc_cidr_block
tags = {
Name = "VPC-Account-B"
}
}
resource "aws_vpc_peering_connection_accepter" "accepter_a_b" {
vpc_peering_connection_id = var.vpc_peering_connection_id_a_b
}
variable "vpc_cidr_block" {
description = "The CIDR block for the VPC"
type = string
}
variable "vpc_peering_connection_id_a_b" {
description = "The VPC peering connection ID between Account A and B"
type = string
}
output "vpc_id" {
value = aws_vpc.vpc_b.id
}
-
account_c module
resource "aws_vpc" "vpc_c" {
cidr_block = var.vpc_cidr_block
tags = {
Name = "VPC-Account-C"
}
}
resource "aws_vpc_peering_connection_accepter" "accepter_a_c" {
vpc_peering_connection_id = var.vpc_peering_connection_id_a_c
}
variable "vpc_cidr_block" {
description = "The CIDR block for the VPC"
type = string
}
variable "vpc_peering_connection_id_a_c" {
description = "The VPC peering connection ID between Account A and C"
type = string
}
output "vpc_id" {
value = aws_vpc.vpc_c.id
}
This Terraform project creates three VPCs in separate AWS accounts and sets up VPC peering between them. Replace AWS_ACCOUNT_A_ID, AWS_ACCOUNT_B_ID, and AWS_ACCOUNT_C_ID with the actual AWS account IDs and set up proper roles for accessing the different AWS accounts. Also note that only VPC peering between account A and B and account A and C is being set up. If you want full mesh VPC peering, you can similarly create a new peering connection between account B and C.