Skip to content

Commit 1c9109d

Browse files
committed
Added example of AWS multi account vpc peering
1 parent d41708f commit 1c9109d

File tree

6 files changed

+109
-33
lines changed

6 files changed

+109
-33
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
locals {
2+
accepter_name = "tenent-peering"
3+
accepter_region = "us-east-1"
4+
accepter_vpc_id = "vpc-07a2c1d0328341493"
5+
requester_name = "management-peering"
6+
requester_region = "ap-northeast-1"
7+
requester_vpc_id = "vpc-0ce36808b9b133608"
8+
additional_tags = {
9+
Owner = "tenent"
10+
Tenancy = "dedicated"
11+
}
12+
}
13+
14+
module "vpc_peering" {
15+
source = "../../modules/vpc_peering"
16+
accepter_name = local.accepter_name
17+
vpc_peering_accepter_vpc_id = local.accepter_vpc_id
18+
vpc_peering_accepter_vpc_region = local.accepter_region
19+
requester_name = local.requester_name
20+
vpc_peering_requester_vpc_id = local.requester_vpc_id
21+
vpc_peering_requester_vpc_region = local.requester_region
22+
vpc_peering_multi_account_enabled = true
23+
vpc_peering_requester_aws_profile = "peer"
24+
vpc_peering_accepter_aws_profile = "accepter"
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "vpc_peering_connection_id" {
2+
description = "Peering connection ID"
3+
value = module.vpc_peering.vpc_peering_connection_id
4+
}
5+
6+
output "vpc_peering_accept_status" {
7+
description = "Accept status for the connection"
8+
value = module.vpc_peering.vpc_peering_accept_status
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
provider "aws" {
2+
alias = "peer"
3+
region = "ap-northeast-1"
4+
aws_account_id = ""
5+
default_tags {
6+
tags = local.additional_tags
7+
}
8+
}
9+
10+
provider "aws" {
11+
alias = "accepter"
12+
region = "ap-northeast-1"
13+
aws_account_id = ""
14+
default_tags {
15+
tags = local.additional_tags
16+
}
17+
}

modules/vpc_peering/main.tf

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
locals {
2-
requester_route_tables_ids = data.aws_route_tables.requester.ids
3-
accepter_route_tables_ids = data.aws_route_tables.accepter.ids
2+
vpc_peering_requester_route_tables_ids = data.aws_route_tables.requester.ids
3+
vpc_peering_accepter_route_tables_ids = data.aws_route_tables.accepter.ids
44
}
55

66
provider "aws" {
7-
alias = "peer"
8-
region = var.requester_vpc_region
7+
alias = "peer"
8+
region = var.vpc_peering_requester_vpc_region
9+
profile = var.vpc_peering_multi_account_enabled ? var.vpc_peering_requester_aws_profile : "default"
910
}
1011

1112
provider "aws" {
12-
alias = "accepter"
13-
region = var.accepter_vpc_region
13+
alias = "accepter"
14+
region = var.vpc_peering_accepter_vpc_region
15+
profile = var.vpc_peering_multi_account_enabled ? var.vpc_peering_accepter_aws_profile : "default"
1416
}
1517

1618
data "aws_vpc" "accepter" {
17-
id = var.accepter_vpc_id
19+
id = var.vpc_peering_accepter_vpc_id
1820
provider = aws.accepter
1921
}
2022

2123
data "aws_route_tables" "accepter" {
22-
vpc_id = var.accepter_vpc_id
24+
vpc_id = var.vpc_peering_accepter_vpc_id
2325
provider = aws.accepter
2426
}
2527

2628
data "aws_vpc" "requester" {
27-
id = var.requester_vpc_id
29+
id = var.vpc_peering_requester_vpc_id
2830
provider = aws.peer
2931
}
3032

3133
data "aws_route_tables" "requester" {
32-
vpc_id = var.requester_vpc_id
34+
vpc_id = var.vpc_peering_requester_vpc_id
3335
provider = aws.peer
3436
}
3537

38+
data "aws_caller_identity" "accepter" {
39+
provider = aws.accepter
40+
}
41+
3642
resource "aws_vpc_peering_connection" "this" {
37-
count = var.peering_enabled ? 1 : 0
38-
vpc_id = var.requester_vpc_id
39-
peer_vpc_id = var.accepter_vpc_id
40-
peer_region = var.accepter_vpc_region
41-
auto_accept = false
42-
provider = aws.peer
43+
count = var.vpc_peering_enabled ? 1 : 0
44+
vpc_id = var.vpc_peering_requester_vpc_id
45+
peer_vpc_id = var.vpc_peering_accepter_vpc_id
46+
peer_region = var.vpc_peering_multi_account_enabled ? var.vpc_peering_accepter_vpc_region : null
47+
auto_accept = false
48+
peer_owner_id = var.vpc_peering_multi_account_enabled ? data.aws_caller_identity.accepter.id : null
49+
provider = aws.peer
4350
tags = {
4451
Name = format("%s-%s-%s", var.requester_name, "to", var.accepter_name)
4552
}
4653
}
4754

4855
resource "aws_vpc_peering_connection_accepter" "this" {
49-
count = var.peering_enabled ? 1 : 0
56+
count = var.vpc_peering_enabled ? 1 : 0
5057
depends_on = [aws_vpc_peering_connection.this]
5158
provider = aws.accepter
5259
vpc_peering_connection_id = aws_vpc_peering_connection.this[0].id
@@ -57,7 +64,7 @@ resource "aws_vpc_peering_connection_accepter" "this" {
5764
}
5865

5966
resource "aws_vpc_peering_connection_options" "this" {
60-
count = var.peering_enabled ? 1 : 0
67+
count = var.vpc_peering_enabled ? 1 : 0
6168
depends_on = [aws_vpc_peering_connection_accepter.this]
6269
vpc_peering_connection_id = aws_vpc_peering_connection.this[0].id
6370
accepter {
@@ -70,17 +77,17 @@ resource "aws_vpc_peering_connection_options" "this" {
7077
#### route tables ####
7178

7279
resource "aws_route" "requester" {
73-
count = var.peering_enabled ? length(local.requester_route_tables_ids) : 0
74-
route_table_id = local.requester_route_tables_ids[count.index]
80+
count = var.vpc_peering_enabled ? length(local.vpc_peering_requester_route_tables_ids) : 0
81+
route_table_id = local.vpc_peering_requester_route_tables_ids[count.index]
7582
destination_cidr_block = data.aws_vpc.accepter.cidr_block
76-
vpc_peering_connection_id = var.peering_enabled ? aws_vpc_peering_connection.this[0].id : null
83+
vpc_peering_connection_id = var.vpc_peering_enabled ? aws_vpc_peering_connection.this[0].id : null
7784
provider = aws.peer
7885
}
7986

8087
resource "aws_route" "accepter" {
81-
count = var.peering_enabled ? length(local.accepter_route_tables_ids) : 0
82-
route_table_id = local.accepter_route_tables_ids[count.index]
88+
count = var.vpc_peering_enabled ? length(local.vpc_peering_accepter_route_tables_ids) : 0
89+
route_table_id = local.vpc_peering_accepter_route_tables_ids[count.index]
8390
destination_cidr_block = data.aws_vpc.requester.cidr_block
84-
vpc_peering_connection_id = var.peering_enabled ? aws_vpc_peering_connection.this[0].id : null
91+
vpc_peering_connection_id = var.vpc_peering_enabled ? aws_vpc_peering_connection.this[0].id : null
8592
provider = aws.accepter
86-
}
93+
}

modules/vpc_peering/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
output "vpc_peering_connection_id" {
22
description = "Peering connection ID"
3-
value = var.peering_enabled ? aws_vpc_peering_connection.this[0].id : null
3+
value = var.vpc_peering_enabled ? aws_vpc_peering_connection.this[0].id : null
44
}
55

66
output "vpc_peering_accept_status" {
77
description = "Status for the connection"
8-
value = var.peering_enabled ? aws_vpc_peering_connection_accepter.this[0].accept_status : null
9-
}
8+
value = var.vpc_peering_enabled ? aws_vpc_peering_connection_accepter.this[0].accept_status : null
9+
}

modules/vpc_peering/variables.tf

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
variable "accepter_vpc_id" {
1+
variable "vpc_peering_accepter_vpc_id" {
22
type = string
33
description = "Specify the unique identifier of the VPC that will act as the Acceptor in the VPC peering connection."
44
default = ""
55
}
66

7-
variable "accepter_vpc_region" {
7+
variable "vpc_peering_accepter_vpc_region" {
88
type = string
99
description = "Provide the AWS region where the Acceptor VPC is located. This helps in identifying the correct region for establishing the VPC peering connection."
1010
default = ""
1111
}
1212

13-
variable "requester_vpc_id" {
13+
variable "vpc_peering_requester_vpc_id" {
1414
type = string
1515
description = "Specify the unique identifier of the VPC that will act as the Reqester in the VPC peering connection."
1616
default = ""
1717
}
1818

19-
variable "requester_vpc_region" {
19+
variable "vpc_peering_requester_vpc_region" {
2020
type = string
2121
description = "Specify the AWS region where the Requester VPC resides. It ensures the correct region is used for setting up the VPC peering."
2222
default = ""
@@ -34,8 +34,26 @@ variable "accepter_name" {
3434
default = ""
3535
}
3636

37-
variable "peering_enabled" {
37+
variable "vpc_peering_enabled" {
3838
type = bool
3939
description = "Set this variable to true if you want to create the VPC peering connection. Set it to false if you want to skip the creation process."
4040
default = true
4141
}
42+
43+
variable "vpc_peering_multi_account_enabled" {
44+
type = bool
45+
description = "Set this variable to true if you want to create the VPC peering connection between reagions. Set it to false if you want to skip the creation process."
46+
default = true
47+
}
48+
49+
variable "vpc_peering_requester_aws_profile" {
50+
type = string
51+
description = "Provide the AWS profile where the requester VPC is located."
52+
default = ""
53+
}
54+
55+
variable "vpc_peering_accepter_aws_profile" {
56+
type = string
57+
description = "Provide the AWS profile where the accepter VPC is located."
58+
default = ""
59+
}

0 commit comments

Comments
 (0)