Skip to content

Commit 78584e5

Browse files
authored
Provide separate route tables for db/elasticache/redshift (#155)
* Provide separate route tables for db/elasticache/redshift * Added example for saperate routes * Updated PR with suggestions * Make redshift to use separate subnet route table also * More cleanup and updates * Fixed one more spelling mistake
1 parent fb3b781 commit 78584e5

File tree

7 files changed

+193
-4
lines changed

7 files changed

+193
-4
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ Terraform version 0.10.3 or newer is required for this module to work.
166166
| azs | A list of availability zones in the region | string | `<list>` | no |
167167
| cidr | The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overridden | string | `0.0.0.0/0` | no |
168168
| create_database_subnet_group | Controls if database subnet group should be created | string | `true` | no |
169+
| create_database_subnet_route_table | Controls if separate route table for database should be created | string | `false` | no |
170+
| create_elasticache_subnet_route_table | Controls if separate route table for elasticache should be created | string | `false` | no |
171+
| create_redshift_subnet_route_table | Controls if separate route table for redshift should be created | string | `false` | no |
169172
| create_vpc | Controls if VPC should be created (it affects almost all resources) | string | `true` | no |
173+
| database_route_table_tags | Additional tags for the database route tables | string | `<map>` | no |
170174
| database_subnet_group_tags | Additional tags for the database subnet group | string | `<map>` | no |
171175
| database_subnet_tags | Additional tags for the database subnets | string | `<map>` | no |
172176
| database_subnets | A list of database subnets | list | `<list>` | no |
@@ -181,6 +185,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
181185
| dhcp_options_netbios_node_type | Specify netbios node_type for DHCP options set | string | `` | no |
182186
| dhcp_options_ntp_servers | Specify a list of NTP servers for DHCP options set | list | `<list>` | no |
183187
| dhcp_options_tags | Additional tags for the DHCP option set | string | `<map>` | no |
188+
| elasticache_route_table_tags | Additional tags for the elasticache route tables | string | `<map>` | no |
184189
| elasticache_subnet_tags | Additional tags for the elasticache subnets | string | `<map>` | no |
185190
| elasticache_subnets | A list of elasticache subnets | list | `<list>` | no |
186191
| enable_dhcp_options | Should be true if you want to specify a DHCP options set with a custom domain name, DNS servers, NTP servers, netbios servers, and/or netbios server type | string | `false` | no |
@@ -210,6 +215,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
210215
| public_route_table_tags | Additional tags for the public route tables | string | `<map>` | no |
211216
| public_subnet_tags | Additional tags for the public subnets | string | `<map>` | no |
212217
| public_subnets | A list of public subnets inside the VPC | string | `<list>` | no |
218+
| redshift_route_table_tags | Additional tags for the redshift route tables | string | `<map>` | no |
213219
| redshift_subnet_group_tags | Additional tags for the redshift subnet group | string | `<map>` | no |
214220
| redshift_subnet_tags | Additional tags for the redshift subnets | string | `<map>` | no |
215221
| redshift_subnets | A list of redshift subnets | list | `<list>` | no |
@@ -224,6 +230,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
224230

225231
| Name | Description |
226232
|------|-------------|
233+
| database_route_table_ids | List of IDs of database route tables |
227234
| database_subnet_group | ID of database subnet group |
228235
| database_subnets | List of IDs of database subnets |
229236
| database_subnets_cidr_blocks | List of cidr_blocks of database subnets |
@@ -239,6 +246,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
239246
| default_vpc_id | Default VPC |
240247
| default_vpc_instance_tenancy | Tenancy of instances spin up within VPC |
241248
| default_vpc_main_route_table_id | The ID of the main route table associated with this VPC |
249+
| elasticache_route_table_ids | List of IDs of elasticache route tables |
242250
| elasticache_subnet_group | ID of elasticache subnet group |
243251
| elasticache_subnet_group_name | Name of elasticache subnet group |
244252
| elasticache_subnets | List of IDs of elasticache subnets |
@@ -247,7 +255,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
247255
| intra_route_table_ids | List of IDs of intra route tables |
248256
| intra_subnets | List of IDs of intra subnets |
249257
| intra_subnets_cidr_blocks | List of cidr_blocks of intra subnets |
250-
| nat_ids | List of allocation ID of Elastic IPs created for AWS NAT Gateway |
258+
| nat_ids | Nat gateway |
251259
| nat_public_ips | List of public Elastic IPs created for AWS NAT Gateway |
252260
| natgw_ids | List of NAT Gateway IDs |
253261
| private_route_table_ids | List of IDs of private route tables |
@@ -256,6 +264,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
256264
| public_route_table_ids | Route tables |
257265
| public_subnets | List of IDs of public subnets |
258266
| public_subnets_cidr_blocks | List of cidr_blocks of public subnets |
267+
| redshift_route_table_ids | List of IDs of redshift route tables |
259268
| redshift_subnet_group | ID of redshift subnet group |
260269
| redshift_subnets | List of IDs of redshift subnets |
261270
| redshift_subnets_cidr_blocks | List of cidr_blocks of redshift subnets |
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# VPC with separate private route tables
2+
3+
Configuration in this directory creates set of VPC resources which may be sufficient for staging or production environment (look into [simple-vpc](../simple-vpc) for more simplified setup).
4+
5+
There are public, private, database, ElastiCache, Redshift subnets, NAT Gateways created in each availability zone. **This example sets up separate private route for database, elasticache and redshift subnets.**.
6+
7+
## Usage
8+
9+
To run this example you need to execute:
10+
11+
```bash
12+
$ terraform init
13+
$ terraform plan
14+
$ terraform apply
15+
```
16+
17+
Note that this example may create resources which can cost money (AWS Elastic IP, for example). Run `terraform destroy` when you don't need these resources.
18+
19+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
20+
21+
## Outputs
22+
23+
| Name | Description |
24+
|------|-------------|
25+
| database_subnets | List of IDs of database subnets |
26+
| elasticache_subnets | List of IDs of elasticache subnets |
27+
| nat_public_ips | NAT gateways |
28+
| private_subnets | Subnets |
29+
| public_subnets | List of IDs of public subnets |
30+
| redshift_subnets | List of IDs of elasticache subnets |
31+
| vpc_id | VPC |
32+
33+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
module "vpc" {
6+
source = "../../"
7+
8+
name = "vpc-separate-private-route-tables"
9+
10+
cidr = "10.10.0.0/16"
11+
12+
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
13+
private_subnets = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"]
14+
public_subnets = ["10.10.11.0/24", "10.10.12.0/24", "10.10.13.0/24"]
15+
database_subnets = ["10.10.21.0/24", "10.10.22.0/24", "10.10.23.0/24"]
16+
elasticache_subnets = ["10.10.31.0/24", "10.10.32.0/24", "10.10.33.0/24"]
17+
redshift_subnets = ["10.10.41.0/24", "10.10.42.0/24", "10.10.43.0/24"]
18+
19+
create_database_subnet_route_table = true
20+
create_elasticache_subnet_route_table = true
21+
create_redshift_subnet_route_table = true
22+
23+
single_nat_gateway = true
24+
enable_nat_gateway = true
25+
26+
tags = {
27+
Owner = "user"
28+
Environment = "staging"
29+
Name = "separate-private-route-tables"
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# VPC
2+
output "vpc_id" {
3+
description = "The ID of the VPC"
4+
value = "${module.vpc.vpc_id}"
5+
}
6+
7+
# Subnets
8+
output "private_subnets" {
9+
description = "List of IDs of private subnets"
10+
value = ["${module.vpc.private_subnets}"]
11+
}
12+
13+
output "public_subnets" {
14+
description = "List of IDs of public subnets"
15+
value = ["${module.vpc.public_subnets}"]
16+
}
17+
18+
output "database_subnets" {
19+
description = "List of IDs of database subnets"
20+
value = ["${module.vpc.database_subnets}"]
21+
}
22+
23+
output "elasticache_subnets" {
24+
description = "List of IDs of elasticache subnets"
25+
value = ["${module.vpc.elasticache_subnets}"]
26+
}
27+
28+
output "redshift_subnets" {
29+
description = "List of IDs of elasticache subnets"
30+
value = ["${module.vpc.redshift_subnets}"]
31+
}
32+
33+
# NAT gateways
34+
output "nat_public_ips" {
35+
description = "List of public Elastic IPs created for AWS NAT Gateway"
36+
value = ["${module.vpc.nat_public_ips}"]
37+
}

main.tf

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,39 @@ resource "aws_route_table" "private" {
9898
}
9999
}
100100

101+
#################
102+
# Database routes
103+
#################
104+
resource "aws_route_table" "database" {
105+
count = "${var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 ? 1 : 0}"
106+
107+
vpc_id = "${aws_vpc.this.id}"
108+
109+
tags = "${merge(var.tags, var.database_route_table_tags, map("Name", "${var.name}-database"))}"
110+
}
111+
112+
#################
113+
# Redshift routes
114+
#################
115+
resource "aws_route_table" "redshift" {
116+
count = "${var.create_vpc && var.create_redshift_subnet_route_table && length(var.redshift_subnets) > 0 ? 1 : 0}"
117+
118+
vpc_id = "${aws_vpc.this.id}"
119+
120+
tags = "${merge(var.tags, var.redshift_route_table_tags, map("Name", "${var.name}-redshift"))}"
121+
}
122+
123+
#################
124+
# Elasticache routes
125+
#################
126+
resource "aws_route_table" "elasticache" {
127+
count = "${var.create_vpc && var.create_elasticache_subnet_route_table && length(var.elasticache_subnets) > 0 ? 1 : 0}"
128+
129+
vpc_id = "${aws_vpc.this.id}"
130+
131+
tags = "${merge(var.tags, var.elasticache_route_table_tags, map("Name", "${var.name}-elasticache"))}"
132+
}
133+
101134
#################
102135
# Intra routes
103136
#################
@@ -350,21 +383,21 @@ resource "aws_route_table_association" "database" {
350383
count = "${var.create_vpc && length(var.database_subnets) > 0 ? length(var.database_subnets) : 0}"
351384

352385
subnet_id = "${element(aws_subnet.database.*.id, count.index)}"
353-
route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}"
386+
route_table_id = "${element(coalescelist(aws_route_table.database.*.id, aws_route_table.private.*.id), (var.single_nat_gateway || var.create_database_subnet_route_table ? 0 : count.index))}"
354387
}
355388

356389
resource "aws_route_table_association" "redshift" {
357390
count = "${var.create_vpc && length(var.redshift_subnets) > 0 ? length(var.redshift_subnets) : 0}"
358391

359392
subnet_id = "${element(aws_subnet.redshift.*.id, count.index)}"
360-
route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}"
393+
route_table_id = "${element(coalescelist(aws_route_table.redshift.*.id, aws_route_table.private.*.id), (var.single_nat_gateway || var.create_redshift_subnet_route_table ? 0 : count.index))}"
361394
}
362395

363396
resource "aws_route_table_association" "elasticache" {
364397
count = "${var.create_vpc && length(var.elasticache_subnets) > 0 ? length(var.elasticache_subnets) : 0}"
365398

366399
subnet_id = "${element(aws_subnet.elasticache.*.id, count.index)}"
367-
route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}"
400+
route_table_id = "${element(coalescelist(aws_route_table.elasticache.*.id, aws_route_table.private.*.id), (var.single_nat_gateway || var.create_elasticache_subnet_route_table ? 0 : count.index))}"
368401
}
369402

370403
resource "aws_route_table_association" "intra" {

outputs.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,27 @@ output "private_route_table_ids" {
151151
value = ["${aws_route_table.private.*.id}"]
152152
}
153153

154+
output "database_route_table_ids" {
155+
description = "List of IDs of database route tables"
156+
value = ["${coalescelist(aws_route_table.database.*.id, aws_route_table.private.*.id)}"]
157+
}
158+
159+
output "redshift_route_table_ids" {
160+
description = "List of IDs of redshift route tables"
161+
value = ["${coalescelist(aws_route_table.redshift.*.id, aws_route_table.private.*.id)}"]
162+
}
163+
164+
output "elasticache_route_table_ids" {
165+
description = "List of IDs of elasticache route tables"
166+
value = ["${coalescelist(aws_route_table.elasticache.*.id, aws_route_table.private.*.id)}"]
167+
}
168+
154169
output "intra_route_table_ids" {
155170
description = "List of IDs of intra route tables"
156171
value = ["${aws_route_table.intra.*.id}"]
157172
}
158173

174+
# Nat gateway
159175
output "nat_ids" {
160176
description = "List of allocation ID of Elastic IPs created for AWS NAT Gateway"
161177
value = ["${aws_eip.nat.*.id}"]

variables.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ variable "elasticache_subnets" {
4646
default = []
4747
}
4848

49+
variable "create_database_subnet_route_table" {
50+
description = "Controls if separate route table for database should be created"
51+
default = false
52+
}
53+
54+
variable "create_redshift_subnet_route_table" {
55+
description = "Controls if separate route table for redshift should be created"
56+
default = false
57+
}
58+
59+
variable "create_elasticache_subnet_route_table" {
60+
description = "Controls if separate route table for elasticache should be created"
61+
default = false
62+
}
63+
4964
variable "intra_subnets" {
5065
type = "list"
5166
description = "A list of intra subnets"
@@ -168,6 +183,21 @@ variable "private_route_table_tags" {
168183
default = {}
169184
}
170185

186+
variable "database_route_table_tags" {
187+
description = "Additional tags for the database route tables"
188+
default = {}
189+
}
190+
191+
variable "redshift_route_table_tags" {
192+
description = "Additional tags for the redshift route tables"
193+
default = {}
194+
}
195+
196+
variable "elasticache_route_table_tags" {
197+
description = "Additional tags for the elasticache route tables"
198+
default = {}
199+
}
200+
171201
variable "intra_route_table_tags" {
172202
description = "Additional tags for the intra route tables"
173203
default = {}

0 commit comments

Comments
 (0)