Skip to content

Commit 0b7da15

Browse files
authored
[vpc-peering] Implement cross-account peering (#87)
* Support cross-account peering * add example * fmt * address CR * Update aws/vpc-peering/variables.tf Co-Authored-By: osterman <[email protected]> * Update aws/vpc-peering/variables.tf Co-Authored-By: osterman <[email protected]>
1 parent 2546527 commit 0b7da15

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

aws/backing-services/vpc.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ variable "vpc_nat_gateway_enabled" {
1010
default = "true"
1111
}
1212

13+
data "aws_region" "current" {}
14+
1315
module "vpc" {
1416
source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=tags/0.3.3"
1517
namespace = "${var.namespace}"
@@ -30,3 +32,13 @@ module "subnets" {
3032
cidr_block = "${module.vpc.vpc_cidr_block}"
3133
nat_gateway_enabled = "${var.vpc_nat_gateway_enabled}"
3234
}
35+
36+
output "vpc_id" {
37+
description = "VPC ID of backing services"
38+
value = "${module.vpc.vpc_id}"
39+
}
40+
41+
output "region" {
42+
description = "AWS region of backing services"
43+
value = "${data.aws_region.current.name}"
44+
}

aws/vpc-peering/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Initialize terraform remote state
2+
init:
3+
[ -f .terraform/terraform.tfstate ] || init-terraform
4+
5+
## Clean up the project
6+
clean:
7+
rm -rf .terraform *.tfstate*
8+
9+
## Pass arguments through to terraform which require remote state
10+
apply console destroy graph plan output providers show: init
11+
terraform $@
12+
13+
## Pass arguments through to terraform which do not require remote state
14+
get fmt validate version:
15+
terraform $@

aws/vpc-peering/main.tf

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
terraform {
2+
required_version = ">= 0.11.2"
3+
4+
backend "s3" {}
5+
}
6+
7+
provider "aws" {
8+
assume_role {
9+
role_arn = "${var.aws_assume_role_arn}"
10+
}
11+
}
12+
13+
# Fetch the OrganizationAccountAccessRole ARNs from SSM
14+
module "requester_role_arns" {
15+
enabled = "${var.enabled}"
16+
source = "git::https://github.com/cloudposse/terraform-aws-ssm-parameter-store?ref=tags/0.1.5"
17+
parameter_read = ["/${var.namespace}/${var.requester_account}/organization_account_access_role"]
18+
}
19+
20+
locals {
21+
requester_vpc_tags = "${var.requester_vpc_tags}"
22+
requester_region = "${var.requester_region}"
23+
requester_role_arn = "${join("", module.requester_role_arns.values)}"
24+
}
25+
26+
# Fetch the OrganizationAccountAccessRole ARNs from SSM
27+
module "accepter_role_arns" {
28+
enabled = "${var.enabled}"
29+
source = "git::https://github.com/cloudposse/terraform-aws-ssm-parameter-store?ref=tags/0.1.5"
30+
parameter_read = ["/${var.namespace}/${var.accepter_account}/organization_account_access_role"]
31+
}
32+
33+
locals {
34+
accepter_vpc_tags = "${var.accepter_vpc_tags}"
35+
accepter_region = "${var.accepter_region}"
36+
accepter_role_arn = "${join("", module.accepter_role_arns.values)}"
37+
}
38+
39+
module "vpc_peering" {
40+
source = "git::https://github.com/cloudposse/terraform-aws-vpc-peering-multi-account.git?ref=tags/0.1.0"
41+
42+
enabled = "${var.enabled}"
43+
44+
namespace = "${var.namespace}"
45+
stage = "${var.stage}"
46+
name = "${var.name}"
47+
attributes = ["${var.requester_account}", "${var.accepter_account}"]
48+
49+
auto_accept = true
50+
51+
# Requester
52+
requester_vpc_tags = "${local.requester_vpc_tags}"
53+
requester_region = "${local.requester_region}"
54+
requester_aws_assume_role_arn = "${local.requester_role_arn}"
55+
56+
# Accepter
57+
accepter_vpc_tags = "${local.accepter_vpc_tags}"
58+
accepter_region = "${local.accepter_region}"
59+
accepter_aws_assume_role_arn = "${local.accepter_role_arn}"
60+
}
61+
62+
output "accepter_accept_status" {
63+
description = "Accepter VPC peering connection request status"
64+
value = "${module.vpc_peering.accepter_accept_status}"
65+
}
66+
67+
output "accepter_connection_id" {
68+
description = "Accepter VPC peering connection ID"
69+
value = "${module.vpc_peering.accepter_connection_id}"
70+
}
71+
72+
output "requester_accept_status" {
73+
description = "Requester VPC peering connection request status"
74+
value = "${module.vpc_peering.requester_accept_status}"
75+
}
76+
77+
output "requester_connection_id" {
78+
description = "Requester VPC peering connection ID"
79+
value = "${module.vpc_peering.requester_connection_id}"
80+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
requester_account = "prod"
2+
requester_region = "us-west-2"
3+
4+
requester_vpc_tags = {
5+
Name = "eg-prod-backing-services"
6+
}
7+
8+
accepter_account = "data"
9+
accepter_region = "us-west-2"
10+
11+
accepter_vpc_tags = {
12+
Name = "us-west-2.data.example.co"
13+
}

aws/vpc-peering/variables.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
variable "aws_assume_role_arn" {}
2+
3+
variable "enabled" {
4+
type = "string"
5+
description = "Whether to create the resources. Set to `false` to prevent the module from creating any resources"
6+
default = "true"
7+
}
8+
9+
variable "namespace" {
10+
type = "string"
11+
description = "Namespace (e.g. `eg` or `cp`)"
12+
}
13+
14+
variable "stage" {
15+
type = "string"
16+
description = "Stage (e.g. `prod`, `dev`, `staging`)"
17+
}
18+
19+
variable "name" {
20+
type = "string"
21+
description = "Application or solution name (e.g. `app`)"
22+
default = "vpc-peering"
23+
}
24+
25+
variable "requester_account" {
26+
description = "Account name of the requester (e.g. `prod` or `staging`). Used to look up the role ARN from SSM"
27+
}
28+
29+
variable "requester_region" {
30+
decription = "Region of the requester's VPC"
31+
}
32+
33+
variable "requester_vpc_tags" {
34+
type = "map"
35+
description = "Tags to filter for the requester's VPC"
36+
default = {}
37+
}
38+
39+
variable "accepter_region" {
40+
decription = "Region of the accepter's VPC"
41+
}
42+
43+
variable "accepter_account" {
44+
description = "Account name of the accepter (e.g. `prod` or `staging`). Used to look up the role ARN from SSM"
45+
}
46+
47+
variable "accepter_vpc_tags" {
48+
type = "map"
49+
description = "Tags to filter for the accepter's VPC"
50+
default = {}
51+
}

0 commit comments

Comments
 (0)