Skip to content

Commit 264aec6

Browse files
committed
updated variables to rich types from the strings of terraform 0.11
1 parent 5488458 commit 264aec6

File tree

3 files changed

+67
-52
lines changed

3 files changed

+67
-52
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ This module's **default behaviour** is to create a VPC and then **create one pri
77

88
module vpc-network
99
{
10-
source = "devops4me/vpc-network/aws"
11-
version = "1.0.2"
10+
source = "devops4me/vpc-network/aws"
11+
version ~> "1.0.3"
12+
1213
in_vpc_cidr = "10.245.0.0/16"
1314
in_num_private_subnets = 6
1415
in_num_public_subnets = 3
@@ -28,14 +29,15 @@ You can run the example to see this module create a number of VPCs with varying
2829

2930
| Input Variable | Type | Description | Default |
3031
|:-------------------------- |:-------:|:------------------------------------------------------------- |:--------------:|
31-
| **in_vpc_cidr** | String | The VPC's Cidr defining the range of available IP addresses | 10.42.0.0/16 |
32-
| **in_num_private_subnets** | Integer | Number of private subnets to create across availability zones | 3 |
33-
| **in_num_public_subnets** | Integer | Number of public subnets to create across availability zones. If one or more an internet gateway and route to the internet will be created regardless of the value of the in_create_gateway boolean variable. | 3 |
34-
| **in_create_gateway** | Boolean | If set to true an internet gateway and route will be created even when no public subnets are requested. | false |
35-
| **[in_subnets_max](https://www.devopswiki.co.uk/vpc/network-cidr)** | Integer | 2 to the power of this is the max number of carvable subnets | 4 (16 subnets) |
36-
| **in_ecosystem** | String | the class name of the ecosystem being built here | eco-system |
32+
| **`in_vpc_cidr`** | string | The VPC's Cidr defining the range of available IP addresses | 10.42.0.0/16 |
33+
| **`in_num_private_subnets`** | number | Number of private subnets to create across availability zones | 3 |
34+
| **`in_num_public_subnets`** | number | Number of public subnets to create across availability zones. If one or more an internet gateway and route to the internet will be created regardless of the value of the in_create_gateway boolean variable. | 3 |
35+
| **`in_create_public_gateway`** | bool | if true create an internet gateway and routes so services can access the internet. | true |
36+
| **`in_create_private_gateway`** | bool | if true creates a NAT gateway and private routes for egress access from private subnets. | true |
37+
| **`in_subnets_max`** | Integer | 2 to the power of this is the [max number of carvable subnets](https://www.devopswiki.co.uk/vpc/network-cidr) | 4 (16 subnets) |
38+
3739

38-
### Resource Tag Inputs
40+
### Optional Resource Tag Inputs
3941

4042
Most organisations have a mandatory set of tags that must be placed on AWS resources for cost and billing reports. Typically they denote owners and specify whether environments are prod or non-prod.
4143

@@ -87,11 +89,11 @@ Here are the most popular **output variables** exported from this VPC and subnet
8789

8890
| Exported | Type | Example | Comment |
8991
|:-------- |:---- |:------- |:------- |
90-
**out_vpc_id** | String | vpc-1234567890 | the **VPC id** of the just-created VPC
91-
**out_rtb_id** | String | "rtb-2468013579" | ID of the VPC's default route table
92-
**out_subnet_ids** | List of Strings | [ "subnet-545123498798345", "subnet-83507325124987" ] | list of **all private and public** subnet ids
93-
**out_private_subnet_ids** | List of Strings | [ "subnet-545123498798345", "subnet-83507325124987" ] | list of **private** subnet ids
94-
**out_public_subnet_ids** | List of Strings | [ "subnet-945873408204034", "subnet-8940202943031" ] | list of **public** subnet ids
92+
**`out_vpc_id`** | String | vpc-1234567890 | the **VPC id** of the just-created VPC
93+
**`out_rtb_id`** | String | "rtb-2468013579" | ID of the VPC's default route table
94+
**`out_subnet_ids`** | List of Strings | [ "subnet-545123498798345", "subnet-83507325124987" ] | list of **all private and public** subnet ids
95+
**`out_private_subnet_ids`** | List of Strings | [ "subnet-545123498798345", "subnet-83507325124987" ] | list of **private** subnet ids
96+
**`out_public_subnet_ids`** | List of Strings | [ "subnet-945873408204034", "subnet-8940202943031" ] | list of **public** subnet ids
9597

9698

9799
---

vpc.network-main.tf

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,32 @@
1111
*/
1212
resource aws_vpc this_vpc {
1313

14-
cidr_block = "${ var.in_vpc_cidr }"
15-
enable_dns_support = true
14+
cidr_block = var.in_vpc_cidr
15+
16+
enable_dns_support = true
1617
enable_dns_hostnames = true
1718

1819
tags = {
1920

2021
Name = "vpc-${ var.in_ecosystem }-${ var.in_timestamp }"
21-
Class = "${ var.in_ecosystem }"
22+
Class = var.in_ecosystem
2223
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
2324
Desc = "This vpc for ${ var.in_ecosystem } ${ var.in_description }"
2425
}
2526
}
2627

2728

29+
/*
30+
| --
31+
| -- The region's list of availability zones are used to place
32+
| -- the requested number of public and private subnets in a
33+
| -- round robin fashion.
34+
| --
35+
*/
36+
data aws_availability_zones with {
37+
}
38+
39+
2840
/*
2941
| -- Round robin card dealing distribution of private subnets
3042
| -- across availability zones is done here.
@@ -35,18 +47,18 @@ resource aws_vpc this_vpc {
3547
*/
3648
resource aws_subnet private {
3749

38-
count = "${ var.in_num_private_subnets }"
50+
count = var.in_num_private_subnets
3951

40-
cidr_block = "${ cidrsubnet( var.in_vpc_cidr, var.in_subnets_max, count.index ) }"
41-
availability_zone = "${ element( data.aws_availability_zones.with.names, count.index ) }"
42-
vpc_id = "${ aws_vpc.this_vpc.id }"
52+
cidr_block = cidrsubnet( var.in_vpc_cidr, var.in_subnets_max, count.index )
53+
availability_zone = element( data.aws_availability_zones.with.names, count.index )
54+
vpc_id = aws_vpc.this_vpc.id
4355

4456
map_public_ip_on_launch = false
4557

4658
tags = {
4759

4860
Name = "subnet-${ var.in_ecosystem }-${ var.in_timestamp }-${ format( "%02d", count.index + 1 ) }-az${ element( split( "-", element( data.aws_availability_zones.with.names, count.index ) ), 2 ) }-x"
49-
Class = "${ var.in_ecosystem }"
61+
Class = var.in_ecosystem
5062
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
5163
Desc = "Private subnet no.${ count.index + 1 } within availability zone ${ element( split( "-", element( data.aws_availability_zones.with.names, count.index ) ), 2 ) } ${ var.in_description }"
5264
}
@@ -64,18 +76,18 @@ resource aws_subnet private {
6476
*/
6577
resource aws_subnet public {
6678

67-
count = "${ var.in_num_public_subnets }"
79+
count = var.in_num_public_subnets
6880

69-
cidr_block = "${ cidrsubnet( var.in_vpc_cidr, var.in_subnets_max, var.in_num_private_subnets + count.index ) }"
70-
availability_zone = "${ element( data.aws_availability_zones.with.names, count.index ) }"
71-
vpc_id = "${ aws_vpc.this_vpc.id }"
81+
cidr_block = cidrsubnet( var.in_vpc_cidr, var.in_subnets_max, var.in_num_private_subnets + count.index )
82+
availability_zone = element( data.aws_availability_zones.with.names, count.index )
83+
vpc_id = aws_vpc.this_vpc.id
7284

7385
map_public_ip_on_launch = true
7486

7587
tags = {
7688

7789
Name = "subnet-${ var.in_ecosystem }-${ var.in_timestamp }-${ format( "%02d", var.in_num_private_subnets + count.index + 1 ) }-az${ element( split( "-", element( data.aws_availability_zones.with.names, count.index ) ), 2 ) }-o"
78-
Class = "${ var.in_ecosystem }"
90+
Class = var.in_ecosystem
7991
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
8092
Desc = "Public subnet no.${ var.in_num_private_subnets + count.index + 1 } within availability zone ${ element( split( "-", element( data.aws_availability_zones.with.names, count.index ) ), 2 ) } ${ var.in_description }"
8193
}
@@ -103,7 +115,7 @@ resource aws_internet_gateway this {
103115
tags = {
104116

105117
Name = "net-gateway-${ var.in_ecosystem }-${ var.in_timestamp }"
106-
Class = "${ var.in_ecosystem }"
118+
Class = var.in_ecosystem
107119
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
108120
Desc = "This internet gateway for ${ var.in_ecosystem } ${ var.in_description }"
109121
}
@@ -136,14 +148,14 @@ resource aws_nat_gateway this {
136148

137149
count = var.in_num_private_subnets * ( var.in_create_private_gateway ? 1 : 0 )
138150

139-
allocation_id = "${ element( aws_eip.nat_gw_ip.*.id, count.index ) }"
140-
subnet_id = "${ element( aws_subnet.public.*.id, count.index ) }"
151+
allocation_id = element( aws_eip.nat_gw_ip.*.id, count.index )
152+
subnet_id = element( aws_subnet.public.*.id, count.index )
141153
depends_on = [ "aws_internet_gateway.this" ]
142154

143155
tags = {
144156

145157
Name = "nat-gateway-${ var.in_ecosystem }-${ var.in_timestamp }"
146-
Class = "${ var.in_ecosystem }"
158+
Class = var.in_ecosystem
147159
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
148160
Desc = "This NAT gateway in public subnet ${ element( aws_subnet.public.*.id, count.index ) } for ${ var.in_ecosystem } ${ var.in_description }"
149161
}
@@ -165,9 +177,9 @@ resource aws_route public {
165177

166178
count = var.in_create_public_gateway ? 1 : 0
167179

168-
route_table_id = "${ aws_vpc.this_vpc.default_route_table_id }"
180+
route_table_id = aws_vpc.this_vpc.default_route_table_id
169181
destination_cidr_block = "0.0.0.0/0"
170-
gateway_id = "${ aws_internet_gateway.this[count.index].id }"
182+
gateway_id = aws_internet_gateway.this[count.index].id
171183
}
172184

173185

@@ -192,8 +204,8 @@ resource aws_route private {
192204

193205
count = var.in_num_private_subnets * ( var.in_create_private_gateway ? 1 : 0 )
194206

195-
route_table_id = "${ element( aws_route_table.private.*.id, count.index ) }"
196-
nat_gateway_id = "${ element( aws_nat_gateway.this.*.id, count.index ) }"
207+
route_table_id = element( aws_route_table.private.*.id, count.index )
208+
nat_gateway_id = element( aws_nat_gateway.this.*.id, count.index )
197209

198210
destination_cidr_block = "0.0.0.0/0"
199211
}
@@ -224,7 +236,7 @@ resource aws_eip nat_gw_ip {
224236
tags = {
225237

226238
Name = "elastic-ip-${ var.in_ecosystem }-${ var.in_timestamp }"
227-
Class = "${ var.in_ecosystem }"
239+
Class = var.in_ecosystem
228240
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
229241
Desc = "This elastic IP in public subnet ${ element( aws_subnet.public.*.id, count.index ) } for ${ var.in_ecosystem } ${ var.in_description }"
230242
}
@@ -246,7 +258,7 @@ resource aws_route_table private {
246258
tags = {
247259

248260
Name = "route-table-${ var.in_ecosystem }-${ var.in_timestamp }"
249-
Class = "${ var.in_ecosystem }"
261+
Class = var.in_ecosystem
250262
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
251263
Desc = "This route table associated with private subnet ${ element( aws_subnet.private.*.id, count.index ) } for ${ var.in_ecosystem } ${ var.in_description }"
252264
}
@@ -267,6 +279,6 @@ resource aws_route_table_association private {
267279

268280
count = var.in_num_private_subnets * ( var.in_create_private_gateway ? 1 : 0 )
269281

270-
subnet_id = "${ element( aws_subnet.private.*.id, count.index ) }"
271-
route_table_id = "${ element( aws_route_table.private.*.id, count.index ) }"
282+
subnet_id = element( aws_subnet.private.*.id, count.index )
283+
route_table_id = element( aws_route_table.private.*.id, count.index )
272284
}

vpc.network-variables.tf

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ variable in_vpc_cidr {
77

88
description = "The CIDr block defining the range of IP addresses allocated to this VPC."
99
default = "10.42.0.0/16"
10+
type = string
1011
}
1112

1213

@@ -17,7 +18,8 @@ variable in_vpc_cidr {
1718
variable in_num_private_subnets {
1819

1920
description = "The number of private subnets to create (defaults to 3 if not specified)."
20-
default = "3"
21+
default = 3
22+
type = number
2123
}
2224

2325

@@ -28,7 +30,8 @@ variable in_num_private_subnets {
2830
variable in_num_public_subnets {
2931

3032
description = "The number of public subnets to create (defaults to 3 if not specified)."
31-
default = "3"
33+
default = 3
34+
type = number
3235
}
3336

3437

@@ -39,7 +42,8 @@ variable in_num_public_subnets {
3942
variable in_subnets_max {
4043

4144
description = "Two to the power of in_subnets_max is the maximum number of subnets carvable from VPC described by in_vpc_cidr."
42-
default = "4"
45+
default = 4
46+
type = number
4347
}
4448

4549

@@ -51,6 +55,7 @@ variable in_create_public_gateway {
5155

5256
description = "An internet gateway and route is created unless this variable is supplied as false."
5357
default = true
58+
type = bool
5459
}
5560

5661

@@ -61,6 +66,7 @@ variable in_create_public_gateway {
6166
variable in_create_private_gateway {
6267
description = "If private subnets exist an EIP, a NAT gateway, route and subnet association are created unless this variable is supplied as false."
6368
default = true
69+
type = bool
6470
}
6571

6672

@@ -71,6 +77,7 @@ variable in_create_private_gateway {
7177
variable in_ecosystem {
7278
description = "Creational stamp binding all infrastructure components created on behalf of this ecosystem instance."
7379
default = "vpc-network"
80+
type = string
7481
}
7582

7683

@@ -80,7 +87,8 @@ variable in_ecosystem {
8087

8188
variable in_timestamp {
8289
description = "A timestamp for resource tags in the format ymmdd-hhmm like 80911-1435"
83-
default = "timestamp"
90+
default = "timestamp"
91+
type = string
8492
}
8593

8694

@@ -90,13 +98,6 @@ variable in_timestamp {
9098

9199
variable in_description {
92100
description = "Ubiquitous note detailing who, when, where and why for every infrastructure component."
93-
default = "This VPC network was created for an ecosystem."
101+
default = "This VPC network was created for an ecosystem."
102+
type = string
94103
}
95-
96-
97-
### ############################### ###
98-
### [[data]] aws_availability_zones ###
99-
### ############################### ###
100-
101-
data aws_availability_zones with {}
102-

0 commit comments

Comments
 (0)