-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kamlesh
committed
Aug 21, 2019
0 parents
commit e5c9b8f
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# terraform-aws-vpc-peering |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module "vpc-peering" { | ||
source = "../" | ||
name = "vpc-peering" | ||
environment = "dmz<->dev" | ||
organization = "clouddrove" | ||
requestor_vpc_id = "vpc-4234234324" | ||
acceptor_vpc_id = "vpc-3242343233" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module "lables" { | ||
source = "git::https://github.com/clouddrove/terraform-lables.git?ref=tags/0.11.0" | ||
name = "${var.name}" | ||
application = "${var.application}" | ||
environment = "${var.environment}" | ||
} | ||
|
||
resource "aws_vpc_peering_connection" "default" { | ||
count = "${var.vpc_peering == "true" ? 1 : 0}" | ||
vpc_id = "${var.requestor_vpc_id}" | ||
peer_vpc_id = "${var.acceptor_vpc_id}" | ||
|
||
auto_accept = "${var.auto_accept}" | ||
|
||
accepter { | ||
allow_remote_vpc_dns_resolution = "${var.acceptor_allow_remote_vpc_dns_resolution}" | ||
} | ||
|
||
requester { | ||
allow_remote_vpc_dns_resolution = "${var.requestor_allow_remote_vpc_dns_resolution}" | ||
} | ||
|
||
tags = "${module.lables.tags}" | ||
} | ||
|
||
# Lookup requestor VPC so that we can reference the CIDR | ||
data "aws_vpc" "requestor" { | ||
count = "${var.vpc_peering == "true" ? 1 : 0}" | ||
id = "${var.requestor_vpc_id}" | ||
} | ||
|
||
# Lookup requestor route tables | ||
data "aws_route_table" "requestor" { | ||
count = "${var.vpc_peering == "true" ? length(distinct(sort(data.aws_subnet_ids.requestor.ids))) : 0}" | ||
subnet_id = "${element(distinct(sort(data.aws_subnet_ids.requestor.ids)), count.index)}" | ||
} | ||
|
||
# Lookup requestor subnets | ||
data "aws_subnet_ids" "requestor" { | ||
count = "${var.vpc_peering == "true" ? 1 : 0}" | ||
vpc_id = "${data.aws_vpc.requestor.id}" | ||
} | ||
|
||
# Lookup acceptor VPC so that we can reference the CIDR | ||
data "aws_vpc" "acceptor" { | ||
count = "${var.vpc_peering == "true" ? 1 : 0}" | ||
id = "${var.acceptor_vpc_id}" | ||
} | ||
|
||
# Lookup acceptor subnets | ||
data "aws_subnet_ids" "acceptor" { | ||
count = "${var.vpc_peering == "true" ? 1 : 0}" | ||
vpc_id = "${data.aws_vpc.acceptor.id}" | ||
} | ||
|
||
# Lookup acceptor route tables | ||
data "aws_route_table" "acceptor" { | ||
count = "${var.vpc_peering == "true" ? length(distinct(sort(data.aws_subnet_ids.acceptor.ids))) : 0}" | ||
subnet_id = "${element(distinct(sort(data.aws_subnet_ids.acceptor.ids)), count.index)}" | ||
} | ||
|
||
# Create routes from requestor to acceptor | ||
resource "aws_route" "requestor" { | ||
count = "${var.vpc_peering == "true" ? length(distinct(sort(data.aws_route_table.requestor.*.route_table_id))) * length(data.aws_vpc.acceptor.cidr_block_associations) : 0}" | ||
route_table_id = "${element(distinct(sort(data.aws_route_table.requestor.*.route_table_id)), (ceil(count.index / (length(data.aws_vpc.acceptor.cidr_block_associations)))))}" | ||
destination_cidr_block = "${lookup(data.aws_vpc.acceptor.cidr_block_associations[count.index % (length(data.aws_vpc.acceptor.cidr_block_associations))], "cidr_block")}" | ||
vpc_peering_connection_id = "${aws_vpc_peering_connection.default.id}" | ||
depends_on = ["data.aws_route_table.requestor", "aws_vpc_peering_connection.default"] | ||
} | ||
|
||
# Create routes from acceptor to requestor | ||
resource "aws_route" "acceptor" { | ||
count = "${var.vpc_peering == "true" ? length(distinct(sort(data.aws_route_table.acceptor.*.route_table_id))) * length(data.aws_vpc.requestor.cidr_block_associations) : 0}" | ||
route_table_id = "${element(distinct(sort(data.aws_route_table.acceptor.*.route_table_id)), ceil(count.index / (length(data.aws_vpc.requestor.cidr_block_associations))))}" | ||
destination_cidr_block = "${lookup(data.aws_vpc.requestor.cidr_block_associations[count.index % (length(data.aws_vpc.requestor.cidr_block_associations))], "cidr_block")}" | ||
vpc_peering_connection_id = "${aws_vpc_peering_connection.default.id}" | ||
depends_on = ["data.aws_route_table.acceptor", "aws_vpc_peering_connection.default"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
output "connection_id" { | ||
value = "${join("", aws_vpc_peering_connection.default.*.id)}" | ||
description = "VPC peering connection ID" | ||
} | ||
|
||
output "accept_status" { | ||
value = "${join("", aws_vpc_peering_connection.default.*.accept_status)}" | ||
description = "The status of the VPC peering connection request" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
variable "vpc_peering" { | ||
default = "true" | ||
description = "Set to false to prevent the module from creating or accessing any resources" | ||
} | ||
|
||
variable "requestor_vpc_id" { | ||
type = "string" | ||
description = "Requestor VPC ID" | ||
} | ||
|
||
variable "acceptor_vpc_id" { | ||
type = "string" | ||
description = "Acceptor VPC ID" | ||
} | ||
|
||
variable "auto_accept" { | ||
default = "true" | ||
description = "Automatically accept the peering (both VPCs need to be in the same AWS account)" | ||
} | ||
|
||
variable "acceptor_allow_remote_vpc_dns_resolution" { | ||
default = "true" | ||
description = "Allow acceptor VPC to resolve public DNS hostnames to private IP addresses when queried from instances in the requestor VPC" | ||
} | ||
|
||
variable "requestor_allow_remote_vpc_dns_resolution" { | ||
default = "true" | ||
description = "Allow requestor VPC to resolve public DNS hostnames to private IP addresses when queried from instances in the acceptor VPC" | ||
} | ||
|
||
variable "application" { | ||
type = "string" | ||
description = "Application (e.g. `cp` or `clouddrove`)" | ||
} | ||
|
||
variable "environment" { | ||
type = "string" | ||
description = "Environment (e.g. `prod`, `dev`, `staging`)" | ||
} | ||
|
||
variable "name" { | ||
description = "Name (e.g. `app` or `cluster`)" | ||
type = "string" | ||
} | ||
|
||
variable "delimiter" { | ||
type = "string" | ||
default = "-" | ||
description = "Delimiter to be used between `namespace`, `stage`, `name` and `attributes`" | ||
} | ||
|
||
variable "attributes" { | ||
type = "list" | ||
default = [] | ||
description = "Additional attributes (e.g. `1`)" | ||
} | ||
|
||
variable "tags" { | ||
type = "map" | ||
default = {} | ||
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)" | ||
} |