diff --git a/examples/example.tf b/examples/example.tf new file mode 100644 index 0000000..ada54a7 --- /dev/null +++ b/examples/example.tf @@ -0,0 +1,65 @@ +variable "env" { + default = "core-us-dev" +} + +variable "first_cidr" { + default = "10.100.0.0/16" +} + +variable "route_tables" { + default = ["rtb-123", "rtb-456"] +} + +variable "second_cidr" { + default = "10.101.0.0/16" +} + +locals { + tags = { + Creator = "Terraform" + Environment = "${var.env}" + Owner = "my-team@my-company.com" + } +} + +module "vpc_peering_requester" { + source = "github.com/jjno91/terraform-aws-vpc-peering-requester?ref=master" + env = "${var.env}" + vpc_id = "my-vpc" + vpc_route_tables = ["${var.route_tables}"] + peer_env = "core-ca-dev" + peer_vpc_id = "their-vpc" + peer_owner_id = "their-aws-account" + peer_region = "their-region" + peer_vpc_cidr_block = "${var.first_cidr}" + tags = "${local.tags}" +} + +# if the VPC you are peering with has more than one CIDR associated +# then you will have to create additional routes and security group rules outside of the module +resource "aws_route" "this" { + count = "${length(var.route_tables)}" + route_table_id = "${element(var.route_tables, count.index)}" + destination_cidr_block = "${var.second_cidr}" + vpc_peering_connection_id = "${module.vpc_peering_requester.vpc_peering_connection_id}" +} + +resource "aws_security_group_rule" "ingress" { + description = "Ingress peer CIDR" + type = "ingress" + from_port = 0 + to_port = 0 + protocol = "-1" + security_group_id = "${module.vpc_peering_requester.security_group_id}" + cidr_blocks = ["${var.second_cidr}"] +} + +resource "aws_security_group_rule" "egress" { + description = "Egress peer CIDR" + type = "egress" + from_port = 0 + to_port = 0 + protocol = "-1" + security_group_id = "${module.vpc_peering_requester.security_group_id}" + cidr_blocks = ["${var.second_cidr}"] +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..1aff266 --- /dev/null +++ b/main.tf @@ -0,0 +1,44 @@ +resource "aws_vpc_peering_connection" "this" { + vpc_id = "${var.vpc_id}" + peer_vpc_id = "${var.peer_vpc_id}" + peer_owner_id = "${var.peer_owner_id}" + peer_region = "${var.peer_region}" + tags = "${merge(map("Name", "${var.env}-peer-${var.peer_env}"), var.tags)}" +} + +resource "aws_route" "this" { + count = "${length(var.vpc_route_tables)}" + route_table_id = "${element(var.vpc_route_tables, count.index)}" + destination_cidr_block = "${var.peer_vpc_cidr_block}" + vpc_peering_connection_id = "${aws_vpc_peering_connection.this.id}" +} + +resource "aws_security_group" "this" { + name_prefix = "${var.env}-peer-${var.peer_env}-" + vpc_id = "${var.vpc_id}" + tags = "${merge(map("Name", "${var.env}-peer-${var.peer_env}"), map("Type", "Peer"), var.tags)}" + + lifecycle { + create_before_destroy = true + } +} + +resource "aws_security_group_rule" "ingress" { + description = "Ingress peer CIDR" + type = "ingress" + from_port = 0 + to_port = 0 + protocol = "-1" + security_group_id = "${aws_security_group.this.id}" + cidr_blocks = ["${var.peer_vpc_cidr_block}"] +} + +resource "aws_security_group_rule" "egress" { + description = "Egress peer CIDR" + type = "egress" + from_port = 0 + to_port = 0 + protocol = "-1" + security_group_id = "${aws_security_group.this.id}" + cidr_blocks = ["${var.peer_vpc_cidr_block}"] +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..fc970d8 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,14 @@ +output "vpc_peering_connection_id" { + description = "https://www.terraform.io/docs/providers/aws/r/vpc_peering.html#id" + value = "${aws_vpc_peering_connection.this.id}" +} + +output "accept_status" { + description = "https://www.terraform.io/docs/providers/aws/r/vpc_peering.html#accept_status" + value = "${aws_vpc_peering_connection.this.accept_status}" +} + +output "security_group_id" { + description = "Security group that grants access to and from the peer's CIDR" + value = "${aws_security_group.this.id}" +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..3da8f2e --- /dev/null +++ b/variables.tf @@ -0,0 +1,44 @@ +variable "env" { + description = "Unique name of your Terraform environment to be used for naming resources" + default = "default" +} + +variable "tags" { + description = "Additional tags to be applied to all resources" + default = {} +} + +variable "vpc_id" { + description = "https://www.terraform.io/docs/providers/aws/r/vpc_peering.html#vpc_id" + default = "" +} + +variable "vpc_route_tables" { + description = "All route tables that you want to receive the peering route" + default = [] +} + +variable "peer_env" { + description = "Environment of the VPC you are peering with" + default = "" +} + +variable "peer_vpc_id" { + description = "https://www.terraform.io/docs/providers/aws/r/vpc_peering.html#peer_vpc_id" + default = "" +} + +variable "peer_owner_id" { + description = "https://www.terraform.io/docs/providers/aws/r/vpc_peering.html#peer_owner_id" + default = "" +} + +variable "peer_region" { + description = "https://www.terraform.io/docs/providers/aws/r/vpc_peering.html#peer_region" + default = "" +} + +variable "peer_vpc_cidr_block" { + description = "CIDR block associated with the peer VPC" + default = "" +}