Skip to content

Commit bcea34a

Browse files
committed
Followups for #161
1 parent e08058d commit bcea34a

File tree

7 files changed

+64
-43
lines changed

7 files changed

+64
-43
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ Terraform version 0.10.3 or newer is required for this module to work.
166166
| assign_generated_ipv6_cidr_block | Requests an Amazon-provided IPv6 CIDR block with a /56 prefix length for the VPC. You cannot specify the range of IP addresses, or the size of the CIDR block | string | `false` | no |
167167
| azs | A list of availability zones in the region | string | `<list>` | no |
168168
| 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 |
169-
| secondary_cidr_blocks | A List of secondary CIDR blocks to add to the vpc. Will append the CIDR blocks before subnet operations are applied | string | `<list>` | no |
170169
| create_database_subnet_group | Controls if database subnet group should be created | string | `true` | no |
171170
| create_database_subnet_route_table | Controls if separate route table for database should be created | string | `false` | no |
172171
| create_elasticache_subnet_route_table | Controls if separate route table for elasticache should be created | string | `false` | no |
@@ -222,6 +221,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
222221
| redshift_subnet_tags | Additional tags for the redshift subnets | string | `<map>` | no |
223222
| redshift_subnets | A list of redshift subnets | list | `<list>` | no |
224223
| reuse_nat_ips | Should be true if you don't want EIPs to be created for your NAT Gateways and will instead pass them in via the 'external_nat_ip_ids' variable | string | `false` | no |
224+
| secondary_cidr_blocks | List of secondary CIDR blocks to associate with the VPC to extend the IP Address pool | string | `<list>` | no |
225225
| single_nat_gateway | Should be true if you want to provision a single shared NAT Gateway across all of your private networks | string | `false` | no |
226226
| tags | A map of tags to add to all resources | string | `<map>` | no |
227227
| vpc_tags | Additional tags for the VPC | string | `<map>` | no |
@@ -281,6 +281,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
281281
| vpc_id | VPC |
282282
| vpc_instance_tenancy | Tenancy of instances spin up within VPC |
283283
| vpc_main_route_table_id | The ID of the main route table associated with this VPC |
284+
| vpc_secondary_cidr_blocks | List of secondary CIDR blocks of the VPC |
284285

285286
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
286287

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
# Simple VPC with secondary CIDR blocks
2-
Configuration in this directory creates set of VPC resources across multiple CIDR blocks.
3-
There is a public and private subnet created per availability zone in addition to single NAT Gateway shared between all 3 availability zones.
4-
## Usage
5-
To run this example you need to execute:
6-
```bash
2+
3+
Configuration in this directory creates set of VPC resources across multiple CIDR blocks.
4+
5+
There is a public and private subnet created per availability zone in addition to single NAT Gateway shared between all 3 availability zones.
6+
7+
## Usage
8+
9+
To run this example you need to execute:
10+
11+
```bash
712
$ terraform init
813
$ terraform plan
914
$ terraform apply
1015
```
11-
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.
12-
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
13-
## Outputs
14-
| Name | Description |
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 |
1524
|------|-------------|
1625
| nat_public_ips | NAT gateways |
1726
| private_subnets | Subnets |
1827
| public_subnets | List of IDs of public subnets |
1928
| vpc_cidr_block | CIDR blocks |
20-
| vpc_secondary_cidr_blocks | Secondary CIDR blocks |
2129
| vpc_id | VPC |
30+
| vpc_secondary_cidr_blocks | List of secondary CIDR blocks of the VPC |
31+
2232
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/secondary-cidr-blocks/main.tf

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
provider "aws" {
22
region = "eu-west-1"
33
}
4+
45
module "vpc" {
56
source = "../../"
7+
68
name = "secondary-cidr-blocks-example"
7-
cidr = "10.0.0.0/16"
9+
10+
cidr = "10.0.0.0/16"
811
secondary_cidr_blocks = ["10.1.0.0/16", "10.2.0.0/16"]
12+
913
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
1014
private_subnets = ["10.0.1.0/24", "10.1.2.0/24", "10.2.3.0/24"]
1115
public_subnets = ["10.0.101.0/24", "10.1.102.0/24", "10.2.103.0/24"]
16+
1217
assign_generated_ipv6_cidr_block = true
13-
enable_nat_gateway = true
14-
single_nat_gateway = true
18+
enable_nat_gateway = true
19+
single_nat_gateway = true
20+
1521
public_subnet_tags = {
1622
Name = "overridden-name-public"
1723
}
24+
1825
tags = {
1926
Owner = "user"
2027
Environment = "dev"
2128
}
29+
2230
vpc_tags = {
2331
Name = "vpc-name"
2432
}

examples/secondary-cidr-blocks/outputs.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ output "vpc_id" {
33
description = "The ID of the VPC"
44
value = "${module.vpc.vpc_id}"
55
}
6+
67
# CIDR blocks
78
output "vpc_cidr_block" {
89
description = "The CIDR block of the VPC"
910
value = ["${module.vpc.vpc_cidr_block}"]
1011
}
12+
1113
output "vpc_secondary_cidr_blocks" {
12-
description = "Secondary CIDR blocks of the VPC"
14+
description = "List of secondary CIDR blocks of the VPC"
1315
value = ["${module.vpc.vpc_secondary_cidr_blocks}"]
1416
}
1517

@@ -18,10 +20,12 @@ output "private_subnets" {
1820
description = "List of IDs of private subnets"
1921
value = ["${module.vpc.private_subnets}"]
2022
}
23+
2124
output "public_subnets" {
2225
description = "List of IDs of public subnets"
2326
value = ["${module.vpc.public_subnets}"]
2427
}
28+
2529
# NAT gateways
2630
output "nat_public_ips" {
2731
description = "List of public Elastic IPs created for AWS NAT Gateway"

main.tf

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ terraform {
55
locals {
66
max_subnet_length = "${max(length(var.private_subnets), length(var.elasticache_subnets), length(var.database_subnets), length(var.redshift_subnets))}"
77
nat_gateway_count = "${var.single_nat_gateway ? 1 : (var.one_nat_gateway_per_az ? length(var.azs) : local.max_subnet_length)}"
8-
vpc_id = "${length(var.secondary_cidr_blocks) > 0 ? element(concat(aws_vpc_ipv4_cidr_block_association.this.*.vpc_id, list("")), 0) : aws_vpc.this.id}"
98
}
109

1110
######
@@ -24,7 +23,7 @@ resource "aws_vpc" "this" {
2423
}
2524

2625
resource "aws_vpc_ipv4_cidr_block_association" "this" {
27-
count = "${length(var.secondary_cidr_blocks)}"
26+
count = "${var.create_vpc && length(var.secondary_cidr_blocks) > 0 ? length(var.secondary_cidr_blocks) : 0}"
2827

2928
vpc_id = "${aws_vpc.this.id}"
3029

@@ -52,7 +51,7 @@ resource "aws_vpc_dhcp_options" "this" {
5251
resource "aws_vpc_dhcp_options_association" "this" {
5352
count = "${var.create_vpc && var.enable_dhcp_options ? 1 : 0}"
5453

55-
vpc_id = "${local.vpc_id}"
54+
vpc_id = "${aws_vpc.this.id}"
5655
dhcp_options_id = "${aws_vpc_dhcp_options.this.id}"
5756
}
5857

@@ -62,7 +61,7 @@ resource "aws_vpc_dhcp_options_association" "this" {
6261
resource "aws_internet_gateway" "this" {
6362
count = "${var.create_vpc && length(var.public_subnets) > 0 ? 1 : 0}"
6463

65-
vpc_id = "${local.vpc_id}"
64+
vpc_id = "${aws_vpc.this.id}"
6665

6766
tags = "${merge(map("Name", format("%s", var.name)), var.igw_tags, var.tags)}"
6867
}
@@ -73,7 +72,7 @@ resource "aws_internet_gateway" "this" {
7372
resource "aws_route_table" "public" {
7473
count = "${var.create_vpc && length(var.public_subnets) > 0 ? 1 : 0}"
7574

76-
vpc_id = "${local.vpc_id}"
75+
vpc_id = "${aws_vpc.this.id}"
7776

7877
tags = "${merge(map("Name", format("%s-public", var.name)), var.public_route_table_tags, var.tags)}"
7978
}
@@ -97,7 +96,7 @@ resource "aws_route" "public_internet_gateway" {
9796
resource "aws_route_table" "private" {
9897
count = "${var.create_vpc && local.max_subnet_length > 0 ? local.nat_gateway_count : 0}"
9998

100-
vpc_id = "${local.vpc_id}"
99+
vpc_id = "${aws_vpc.this.id}"
101100

102101
tags = "${merge(map("Name", (var.single_nat_gateway ? "${var.name}-private" : format("%s-private-%s", var.name, element(var.azs, count.index)))), var.private_route_table_tags, var.tags)}"
103102

@@ -114,7 +113,7 @@ resource "aws_route_table" "private" {
114113
resource "aws_route_table" "database" {
115114
count = "${var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 ? 1 : 0}"
116115

117-
vpc_id = "${local.vpc_id}"
116+
vpc_id = "${aws_vpc.this.id}"
118117

119118
tags = "${merge(var.tags, var.database_route_table_tags, map("Name", "${var.name}-database"))}"
120119
}
@@ -125,7 +124,7 @@ resource "aws_route_table" "database" {
125124
resource "aws_route_table" "redshift" {
126125
count = "${var.create_vpc && var.create_redshift_subnet_route_table && length(var.redshift_subnets) > 0 ? 1 : 0}"
127126

128-
vpc_id = "${local.vpc_id}"
127+
vpc_id = "${aws_vpc.this.id}"
129128

130129
tags = "${merge(var.tags, var.redshift_route_table_tags, map("Name", "${var.name}-redshift"))}"
131130
}
@@ -136,7 +135,7 @@ resource "aws_route_table" "redshift" {
136135
resource "aws_route_table" "elasticache" {
137136
count = "${var.create_vpc && var.create_elasticache_subnet_route_table && length(var.elasticache_subnets) > 0 ? 1 : 0}"
138137

139-
vpc_id = "${local.vpc_id}"
138+
vpc_id = "${aws_vpc.this.id}"
140139

141140
tags = "${merge(var.tags, var.elasticache_route_table_tags, map("Name", "${var.name}-elasticache"))}"
142141
}
@@ -147,7 +146,7 @@ resource "aws_route_table" "elasticache" {
147146
resource "aws_route_table" "intra" {
148147
count = "${var.create_vpc && length(var.intra_subnets) > 0 ? 1 : 0}"
149148

150-
vpc_id = "${local.vpc_id}"
149+
vpc_id = "${aws_vpc.this.id}"
151150

152151
tags = "${merge(map("Name", "${var.name}-intra"), var.intra_route_table_tags, var.tags)}"
153152
}
@@ -158,7 +157,7 @@ resource "aws_route_table" "intra" {
158157
resource "aws_subnet" "public" {
159158
count = "${var.create_vpc && length(var.public_subnets) > 0 && (!var.one_nat_gateway_per_az || length(var.public_subnets) >= length(var.azs)) ? length(var.public_subnets) : 0}"
160159

161-
vpc_id = "${local.vpc_id}"
160+
vpc_id = "${aws_vpc.this.id}"
162161
cidr_block = "${var.public_subnets[count.index]}"
163162
availability_zone = "${element(var.azs, count.index)}"
164163
map_public_ip_on_launch = "${var.map_public_ip_on_launch}"
@@ -172,7 +171,7 @@ resource "aws_subnet" "public" {
172171
resource "aws_subnet" "private" {
173172
count = "${var.create_vpc && length(var.private_subnets) > 0 ? length(var.private_subnets) : 0}"
174173

175-
vpc_id = "${local.vpc_id}"
174+
vpc_id = "${aws_vpc.this.id}"
176175
cidr_block = "${var.private_subnets[count.index]}"
177176
availability_zone = "${element(var.azs, count.index)}"
178177

@@ -185,7 +184,7 @@ resource "aws_subnet" "private" {
185184
resource "aws_subnet" "database" {
186185
count = "${var.create_vpc && length(var.database_subnets) > 0 ? length(var.database_subnets) : 0}"
187186

188-
vpc_id = "${local.vpc_id}"
187+
vpc_id = "${aws_vpc.this.id}"
189188
cidr_block = "${var.database_subnets[count.index]}"
190189
availability_zone = "${element(var.azs, count.index)}"
191190

@@ -208,7 +207,7 @@ resource "aws_db_subnet_group" "database" {
208207
resource "aws_subnet" "redshift" {
209208
count = "${var.create_vpc && length(var.redshift_subnets) > 0 ? length(var.redshift_subnets) : 0}"
210209

211-
vpc_id = "${local.vpc_id}"
210+
vpc_id = "${aws_vpc.this.id}"
212211
cidr_block = "${var.redshift_subnets[count.index]}"
213212
availability_zone = "${element(var.azs, count.index)}"
214213

@@ -231,7 +230,7 @@ resource "aws_redshift_subnet_group" "redshift" {
231230
resource "aws_subnet" "elasticache" {
232231
count = "${var.create_vpc && length(var.elasticache_subnets) > 0 ? length(var.elasticache_subnets) : 0}"
233232

234-
vpc_id = "${local.vpc_id}"
233+
vpc_id = "${aws_vpc.this.id}"
235234
cidr_block = "${var.elasticache_subnets[count.index]}"
236235
availability_zone = "${element(var.azs, count.index)}"
237236

@@ -252,7 +251,7 @@ resource "aws_elasticache_subnet_group" "elasticache" {
252251
resource "aws_subnet" "intra" {
253252
count = "${var.create_vpc && length(var.intra_subnets) > 0 ? length(var.intra_subnets) : 0}"
254253

255-
vpc_id = "${local.vpc_id}"
254+
vpc_id = "${aws_vpc.this.id}"
256255
cidr_block = "${var.intra_subnets[count.index]}"
257256
availability_zone = "${element(var.azs, count.index)}"
258257

@@ -317,7 +316,7 @@ data "aws_vpc_endpoint_service" "s3" {
317316
resource "aws_vpc_endpoint" "s3" {
318317
count = "${var.create_vpc && var.enable_s3_endpoint ? 1 : 0}"
319318

320-
vpc_id = "${local.vpc_id}"
319+
vpc_id = "${aws_vpc.this.id}"
321320
service_name = "${data.aws_vpc_endpoint_service.s3.service_name}"
322321
}
323322

@@ -354,7 +353,7 @@ data "aws_vpc_endpoint_service" "dynamodb" {
354353
resource "aws_vpc_endpoint" "dynamodb" {
355354
count = "${var.create_vpc && var.enable_dynamodb_endpoint ? 1 : 0}"
356355

357-
vpc_id = "${local.vpc_id}"
356+
vpc_id = "${aws_vpc.this.id}"
358357
service_name = "${data.aws_vpc_endpoint_service.dynamodb.service_name}"
359358
}
360359

@@ -430,15 +429,15 @@ resource "aws_route_table_association" "public" {
430429
resource "aws_vpn_gateway" "this" {
431430
count = "${var.create_vpc && var.enable_vpn_gateway ? 1 : 0}"
432431

433-
vpc_id = "${local.vpc_id}"
432+
vpc_id = "${aws_vpc.this.id}"
434433

435434
tags = "${merge(map("Name", format("%s", var.name)), var.vpn_gateway_tags, var.tags)}"
436435
}
437436

438437
resource "aws_vpn_gateway_attachment" "this" {
439438
count = "${var.vpn_gateway_id != "" ? 1 : 0}"
440439

441-
vpc_id = "${local.vpc_id}"
440+
vpc_id = "${aws_vpc.this.id}"
442441
vpn_gateway_id = "${var.vpn_gateway_id}"
443442
}
444443

outputs.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ output "vpc_main_route_table_id" {
5959
// value = "${element(concat(aws_vpc.this.*.ipv6_cidr_block, list("")), 0)}"
6060
//}
6161

62+
output "vpc_secondary_cidr_blocks" {
63+
description = "List of secondary CIDR blocks of the VPC"
64+
value = ["${aws_vpc_ipv4_cidr_block_association.this.*.cidr_block}"]
65+
}
66+
6267
# Subnets
6368
output "private_subnets" {
6469
description = "List of IDs of private subnets"
@@ -231,11 +236,6 @@ output "default_vpc_cidr_block" {
231236
value = "${element(concat(aws_default_vpc.this.*.cidr_block, list("")), 0)}"
232237
}
233238

234-
output "vpc_secondary_cidr_blocks" {
235-
description = "Secondary CIDR blocks of the VPC"
236-
value = ["${aws_vpc_ipv4_cidr_block_association.this.*.cidr_block}"]
237-
}
238-
239239
output "default_vpc_default_security_group_id" {
240240
description = "The ID of the security group created by default on VPC creation"
241241
value = "${element(concat(aws_default_vpc.this.*.default_security_group_id, list("")), 0)}"

variables.tf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ variable "assign_generated_ipv6_cidr_block" {
1919
}
2020

2121
variable "secondary_cidr_blocks" {
22-
type = "list"
23-
description = "Secondary CIDR blocks to associate with the VPC to extend the IP Address pool."
24-
default = []
22+
description = "List of secondary CIDR blocks to associate with the VPC to extend the IP Address pool"
23+
default = []
2524
}
2625

2726
variable "instance_tenancy" {

0 commit comments

Comments
 (0)