Skip to content

Commit

Permalink
examples: creating VPCs and subnets across two regions
Browse files Browse the repository at this point in the history
This example demonstrates both creating a network architecture *and* the
use of data resources to minimize the number of variables needed for a
child module by discovering additional data automatically.
  • Loading branch information
apparentlymart committed Jul 17, 2016
1 parent c15a12f commit b97fd97
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/aws-networking/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform.tfstate
terraform.tfstate.backup
.terraform/*
11 changes: 11 additions & 0 deletions examples/aws-networking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# AWS Networking Example

This example creates AWS VPC resources, making a VPC in each of two regions and
then two subnets in each VPC in two different availability zones.

This example also demonstrates the use of modules to create several copies of
the same resource set with different arguments. The child modules in this
directory are:

* `region`: container module for all of the network resources within a region. This is instantiated once per region.
* `subnet`: represents a subnet within a given availability zone. This is instantiated twice per region, using the first two availability zones supported within the target AWS account.
27 changes: 27 additions & 0 deletions examples/aws-networking/numbering/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "region_numbers" {
default = {
us-east-1 = 1
us-west-1 = 2
us-west-2 = 3
eu-west-1 = 4
}
}

variable "az_numbers" {
default = {
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
k = 11
l = 12
m = 13
n = 14
}
}
1 change: 1 addition & 0 deletions examples/aws-networking/region/numbering.tf
11 changes: 11 additions & 0 deletions examples/aws-networking/region/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "vpc_id" {
value = "${aws_vpc.main.id}"
}

output "primary_subnet_id" {
value = "${module.primary_subnet.subnet_id}"
}

output "secondary_subnet_id" {
value = "${module.secondary_subnet.subnet_id}"
}
25 changes: 25 additions & 0 deletions examples/aws-networking/region/security_group.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "aws_security_group" "region" {
name = "region"
description = "Open access within this region"
vpc_id = "${aws_vpc.main.id}"

ingress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["${aws_vpc.main.cidr_block}"]
}
}

resource "aws_security_group" "internal-all" {
name = "internal-all"
description = "Open access within the full internal network"
vpc_id = "${aws_vpc.main.id}"

ingress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["${var.base_cidr_block}"]
}
}
14 changes: 14 additions & 0 deletions examples/aws-networking/region/subnets.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data "aws_availability_zones" "all" {
}

module "primary_subnet" {
source = "../subnet"
vpc_id = "${aws_vpc.main.id}"
availability_zone = "${data.aws_availability_zones.all.names[0]}"
}

module "secondary_subnet" {
source = "../subnet"
vpc_id = "${aws_vpc.main.id}"
availability_zone = "${data.aws_availability_zones.all.names[1]}"
}
9 changes: 9 additions & 0 deletions examples/aws-networking/region/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "region" {
description = "The name of the AWS region to set up a network within"
}

variable "base_cidr_block" {}

provider "aws" {
region = "${var.region}"
}
7 changes: 7 additions & 0 deletions examples/aws-networking/region/vpc.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_vpc" "main" {
cidr_block = "${cidrsubnet(var.base_cidr_block, 4, lookup(var.region_numbers, var.region))}"
}

resource "aws_internet_gateway" "main" {
vpc_id = "${aws_vpc.main.id}"
}
11 changes: 11 additions & 0 deletions examples/aws-networking/regions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module "us-east-1" {
source = "./region"
region = "us-east-1"
base_cidr_block = "${var.base_cidr_block}"
}

module "us-west-2" {
source = "./region"
region = "us-west-2"
base_cidr_block = "${var.base_cidr_block}"
}
1 change: 1 addition & 0 deletions examples/aws-networking/subnet/numbering.tf
3 changes: 3 additions & 0 deletions examples/aws-networking/subnet/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "subnet_id" {
value = "${aws_subnet.main.id}"
}
12 changes: 12 additions & 0 deletions examples/aws-networking/subnet/security_group.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "aws_security_group" "az" {
name = "az-${data.aws_availability_zone.target.name}"
description = "Open access within the AZ ${data.aws_availability_zone.target.name}"
vpc_id = "${var.vpc_id}"

ingress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["${aws_subnet.main.cidr_block}"]
}
}
13 changes: 13 additions & 0 deletions examples/aws-networking/subnet/subnet.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "aws_subnet" "main" {
cidr_block = "${cidrsubnet(data.aws_vpc.target.cidr_block, 4, lookup(var.az_numbers, data.aws_availability_zone.target.name_suffix))}"
vpc_id = "${var.vpc_id}"
}

resource "aws_route_table" "main" {
vpc_id = "${var.vpc_id}"
}

resource "aws_route_table_association" "main" {
subnet_id = "${aws_subnet.main.id}"
route_table_id = "${aws_route_table.main.id}"
}
11 changes: 11 additions & 0 deletions examples/aws-networking/subnet/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "vpc_id" {}

variable "availability_zone" {}

data "aws_availability_zone" "target" {
name = "${var.availability_zone}"
}

data "aws_vpc" "target" {
id = "${var.vpc_id}"
}
3 changes: 3 additions & 0 deletions examples/aws-networking/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "base_cidr_block" {
default = "10.0.0.0/12"
}

0 comments on commit b97fd97

Please sign in to comment.