diff --git a/README.md b/README.md index 5a40ca7..56937c9 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,24 @@ module "vpc-peering" { auto_accept_peering = true } ``` + +Usage with already created peering connection: +```hc1 +module "vpc-peering" { + source = "./terraform-aws-vpc-peering" + + owner_account_id = "000000000000" + vpc_peer_id = "vpc-00000000" + this_vpc_id = "${module.vpc.vpc_id}" + private_route_table_ids = ["${module.vpc.private_route_table_ids}"] + public_route_table_ids = ["${module.vpc.public_route_table_ids}"] + peer_cidr_block = "10.1.0.1/24" + auto_accept_peering = true + create_peering = 0 + peering_id = "pcx-00000000" + +} +``` Examples -------- Complete example is shown above diff --git a/main.tf b/main.tf index 1a04dd4..8748431 100644 --- a/main.tf +++ b/main.tf @@ -2,6 +2,7 @@ # VPC peering connection # ########################## resource "aws_vpc_peering_connection" "this" { + count = "${var.create_peering ? 1 : 0}" peer_owner_id = "${var.owner_account_id}" peer_vpc_id = "${var.vpc_peer_id}" vpc_id = "${var.this_vpc_id}" @@ -13,21 +14,17 @@ resource "aws_vpc_peering_connection" "this" { ################## resource "aws_route" "private_route_table" { count = "${length(var.private_route_table_ids)}" - route_table_id = "${element(var.private_route_table_ids, count.index)}" destination_cidr_block = "${var.peer_cidr_block}" - vpc_peering_connection_id = "${aws_vpc_peering_connection.this.id}" - depends_on = ["aws_vpc_peering_connection.this"] + vpc_peering_connection_id = "${var.peering_id == "" ? element(concat(aws_vpc_peering_connection.this.*.id, list("")), 0) : var.peering_id}" } ################# # Public routes # ################# resource "aws_route" "public_route_table" { - count = "${length(var.public_route_table_ids) > 0 ? 1: 0}" - + count = "${length(var.public_route_table_ids)}" route_table_id = "${element(var.public_route_table_ids, count.index)}" destination_cidr_block = "${var.peer_cidr_block}" - vpc_peering_connection_id = "${aws_vpc_peering_connection.this.id}" - depends_on = ["aws_vpc_peering_connection.this"] + vpc_peering_connection_id = "${var.peering_id == "" ? element(concat(aws_vpc_peering_connection.this.*.id, list("")), 0) : var.peering_id}" } diff --git a/outputs.tf b/outputs.tf index b3a3585..d6baf9d 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,6 +1,6 @@ output "vpc_peering_id" { description = "Peering connection ID" - value = "${aws_vpc_peering_connection.this.id}" + value = "${var.peering_id == "" ? element(concat(aws_vpc_peering_connection.this.*.id, list("")), 0) : var.peering_id}" } output "private_route_tables" { diff --git a/variables.tf b/variables.tf index e822f45..7579f79 100644 --- a/variables.tf +++ b/variables.tf @@ -34,3 +34,13 @@ variable "auto_accept_peering" { description = "Auto accept peering connection" default = false } + +variable "create_peering" { + description = "Create peering connection, 0 to not create" + default = 1 +} + +variable "peering_id" { + description = "Provide already existing peering connection id" + default = "" +}