Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
initial attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
jjno91 committed Nov 11, 2018
1 parent 3116653 commit 491a8fe
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
65 changes: 65 additions & 0 deletions examples/example.tf
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
}
}

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}"]
}
44 changes: 44 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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}"]
}
14 changes: 14 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
44 changes: 44 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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 = ""
}

0 comments on commit 491a8fe

Please sign in to comment.