Skip to content

Commit df2b01a

Browse files
committed
upgraded vpc creation to terraform 0.12.5 and improved the example
1 parent 658dc97 commit df2b01a

File tree

5 files changed

+226
-150
lines changed

5 files changed

+226
-150
lines changed

example/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
# Example | Creating a VPC Network
3+
4+
This example creates a VPC, subnets and the networking backbone to allow traffic to be routed in and also routed out to service endpoints on the internet.
5+
6+
## How to Run the Example
7+
8+
```
9+
# get module and go to example directory
10+
git clone github.com/devops4me/terraform-aws-vpc-network
11+
cd terraform-aws-vpc-network/example
12+
13+
# export access information
14+
export TF_VAR_in_role_arn=<<role-arn>>
15+
export AWS_ACCESS_KEY_ID=<<access-key-id>>
16+
export AWS_SECRET_ACCESS_KEY=<<secret-access-key>>
17+
export AWS_DEFAULT_REGION=<<region-key>>
18+
19+
# use terraform to bring up and tear down infastructure
20+
terraform init
21+
terraform apply
22+
terraform destroy
23+
```
24+
25+
## Inputs
26+
27+
| Input Variable | Type | Description | Required? |
28+
|:-------------------------- |:-------:|:------------------------------------------------------------- |:--------------:|
29+
| **in_role_arn** | String | The VPC's Cidr defining the range of available IP addresses | optional |
30+
31+
### What is the role arn?
32+
33+
If you are using an IAM role as the AWS access mechanism then pass it as in_role_arn commonly through an environment variable named **TF_VAR_in_role_arn** in addition to the usual AWS access key, secret key and default region parameters.
34+
35+
| -- Individuals and small businesses who don't have hundreds of AWS
36+
| -- accounts can omit the variable and thanks to dynamic assignment
37+
| -- the assume_role block will cease to exist.
38+
39+
40+
41+
## Related Modules
42+

example/vpc.network-test.tf

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
### #################### ###
3+
### Example VPC Networks ###
4+
### #################### ###
5+
6+
module subnet-count-not-stated {
7+
8+
source = "./.."
9+
in_ecosystem = local.ecosystem_name
10+
in_timestamp = local.timestamp
11+
in_description = local.description
12+
}
13+
14+
15+
module just-two-subnets {
16+
17+
source = "./.."
18+
in_vpc_cidr = "10.240.0.0/21"
19+
in_num_private_subnets = 2
20+
in_num_public_subnets = 2
21+
in_subnets_max = "7"
22+
23+
in_ecosystem = "${ local.ecosystem_name }-01"
24+
in_timestamp = local.timestamp
25+
in_description = local.description
26+
}
27+
28+
29+
module no-private-subnets {
30+
31+
source = "./.."
32+
in_vpc_cidr = "10.241.0.0/16"
33+
in_subnets_max = "4"
34+
in_num_private_subnets = 0
35+
in_num_public_subnets = 2
36+
in_create_public_gateway = false
37+
38+
in_ecosystem = "${ local.ecosystem_name }-02"
39+
in_timestamp = local.timestamp
40+
in_description = local.description
41+
}
42+
43+
44+
module two-subnets-per-zone {
45+
46+
source = "./.."
47+
in_vpc_cidr = "10.242.0.0/16"
48+
in_num_private_subnets = 6
49+
in_num_public_subnets = 6
50+
in_create_public_gateway = false
51+
in_create_private_gateway = false
52+
53+
in_ecosystem = "${ local.ecosystem_name }-03"
54+
in_timestamp = local.timestamp
55+
in_description = local.description
56+
}
57+
58+
59+
### ########################### ###
60+
### Example VPC Network Outputs ###
61+
### ########################### ###
62+
63+
output subnet_ids_1{ value = module.subnet-count-not-stated.out_subnet_ids }
64+
output private_subnet_ids_1{ value = module.subnet-count-not-stated.out_private_subnet_ids }
65+
output public_subnet_ids_1{ value = module.subnet-count-not-stated.out_public_subnet_ids }
66+
67+
output subnet_ids_2{ value = module.just-two-subnets.out_subnet_ids }
68+
output private_subnet_ids_2{ value = module.just-two-subnets.out_private_subnet_ids }
69+
output public_subnet_ids_2{ value = module.just-two-subnets.out_public_subnet_ids }
70+
71+
output subnet_ids_3{ value = module.no-private-subnets.out_subnet_ids }
72+
output private_subnet_ids_3{ value = module.no-private-subnets.out_private_subnet_ids }
73+
output public_subnet_ids_3{ value = module.no-private-subnets.out_public_subnet_ids }
74+
75+
output subnet_ids_4{ value = module.two-subnets-per-zone.out_subnet_ids }
76+
output private_subnet_ids_4{ value = module.two-subnets-per-zone.out_private_subnet_ids }
77+
output public_subnet_ids_4{ value = module.two-subnets-per-zone.out_public_subnet_ids }
78+
79+
80+
/*
81+
| --
82+
| -- If you are using an IAM role as the AWS access mechanism then
83+
| -- pass it as in_role_arn commonly through an environment variable
84+
| -- named TF_VAR_in_role_arn in addition to the usual AWS access
85+
| -- key, secret key and default region parameters.
86+
| --
87+
| -- Individuals and small businesses without hundreds of AWS accounts
88+
| -- can omit the in_role_arn variable. and thanks to dynamic assignment
89+
| --
90+
*/
91+
provider aws {
92+
dynamic assume_role {
93+
for_each = length( var.in_role_arn ) > 0 ? [ var.in_role_arn ] : []
94+
content {
95+
role_arn = assume_role.value
96+
}
97+
}
98+
}
99+
100+
101+
variable in_role_arn {
102+
description = "The Role ARN to use when we assume role to implement the provisioning."
103+
default = ""
104+
}
105+
106+
107+
/*
108+
| --
109+
| -- ### ############# ###
110+
| -- ### Resource Tags ###
111+
| -- ### ############# ###
112+
| --
113+
| -- Terraform will tag every significant resource allowing you to report and collate
114+
| --
115+
| -- [1] - all infrastructure in all environments dedicated to your app (ecosystem_name)
116+
| -- [2] - the infrastructure dedicated to this environment instance (timestamp)
117+
| --
118+
| -- The human readable description reveals the when, where and what of the infrastructure.
119+
| --
120+
*/
121+
locals {
122+
ecosystem_name = "virtual-net"
123+
timestamp = formatdate( "YYMMDDhhmmss", timestamp() )
124+
date_time = formatdate( "EEEE, DD-MMM-YY hh:mm:ss ZZZ", timestamp() )
125+
description = "was created by jenkins on ${ local.date_time }."
126+
}

integration.test.dir/vpc.network-test.tf

Lines changed: 0 additions & 92 deletions
This file was deleted.

vpc.network-main.tf

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ resource aws_vpc this_vpc {
1717

1818
tags = {
1919

20-
Name = "vpc-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
21-
Class = "${ var.in_ecosystem_name }"
22-
Instance = "${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
23-
Desc = "This vpc for ${ var.in_ecosystem_name } ${ var.in_tag_description }"
20+
Name = "vpc-${ var.in_ecosystem }-${ var.in_timestamp }"
21+
Class = "${ var.in_ecosystem }"
22+
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
23+
Desc = "This vpc for ${ var.in_ecosystem } ${ var.in_description }"
2424
}
2525
}
2626

@@ -45,10 +45,10 @@ resource aws_subnet private {
4545

4646
tags = {
4747

48-
Name = "subnet-${ var.in_ecosystem_name }-${ var.in_tag_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_name }"
50-
Instance = "${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
51-
Desc = "Private subnet no.${ count.index + 1 } within availability zone ${ element( split( "-", element( data.aws_availability_zones.with.names, count.index ) ), 2 ) } ${ var.in_tag_description }"
48+
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 }"
50+
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
51+
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 }"
5252
}
5353

5454
}
@@ -74,10 +74,10 @@ resource aws_subnet public {
7474

7575
tags = {
7676

77-
Name = "subnet-${ var.in_ecosystem_name }-${ var.in_tag_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_name }"
79-
Instance = "${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
80-
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_tag_description }"
77+
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 }"
79+
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
80+
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 }"
8181
}
8282

8383
}
@@ -102,10 +102,10 @@ resource aws_internet_gateway this {
102102

103103
tags = {
104104

105-
Name = "net-gateway-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
106-
Class = "${ var.in_ecosystem_name }"
107-
Instance = "${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
108-
Desc = "This internet gateway for ${ var.in_ecosystem_name } ${ var.in_tag_description }"
105+
Name = "net-gateway-${ var.in_ecosystem }-${ var.in_timestamp }"
106+
Class = "${ var.in_ecosystem }"
107+
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
108+
Desc = "This internet gateway for ${ var.in_ecosystem } ${ var.in_description }"
109109
}
110110
}
111111

@@ -142,10 +142,10 @@ resource aws_nat_gateway this {
142142

143143
tags = {
144144

145-
Name = "nat-gateway-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
146-
Class = "${ var.in_ecosystem_name }"
147-
Instance = "${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
148-
Desc = "This NAT gateway in public subnet ${ element( aws_subnet.public.*.id, count.index ) } for ${ var.in_ecosystem_name } ${ var.in_tag_description }"
145+
Name = "nat-gateway-${ var.in_ecosystem }-${ var.in_timestamp }"
146+
Class = "${ var.in_ecosystem }"
147+
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
148+
Desc = "This NAT gateway in public subnet ${ element( aws_subnet.public.*.id, count.index ) } for ${ var.in_ecosystem } ${ var.in_description }"
149149
}
150150
}
151151

@@ -223,10 +223,10 @@ resource aws_eip nat_gw_ip {
223223

224224
tags = {
225225

226-
Name = "elastic-ip-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
227-
Class = "${ var.in_ecosystem_name }"
228-
Instance = "${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
229-
Desc = "This elastic IP in public subnet ${ element( aws_subnet.public.*.id, count.index ) } for ${ var.in_ecosystem_name } ${ var.in_tag_description }"
226+
Name = "elastic-ip-${ var.in_ecosystem }-${ var.in_timestamp }"
227+
Class = "${ var.in_ecosystem }"
228+
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
229+
Desc = "This elastic IP in public subnet ${ element( aws_subnet.public.*.id, count.index ) } for ${ var.in_ecosystem } ${ var.in_description }"
230230
}
231231
}
232232

@@ -245,10 +245,10 @@ resource aws_route_table private {
245245

246246
tags = {
247247

248-
Name = "route-table-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
249-
Class = "${ var.in_ecosystem_name }"
250-
Instance = "${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
251-
Desc = "This route table associated with private subnet ${ element( aws_subnet.private.*.id, count.index ) } for ${ var.in_ecosystem_name } ${ var.in_tag_description }"
248+
Name = "route-table-${ var.in_ecosystem }-${ var.in_timestamp }"
249+
Class = "${ var.in_ecosystem }"
250+
Instance = "${ var.in_ecosystem }-${ var.in_timestamp }"
251+
Desc = "This route table associated with private subnet ${ element( aws_subnet.private.*.id, count.index ) } for ${ var.in_ecosystem } ${ var.in_description }"
252252
}
253253
}
254254

0 commit comments

Comments
 (0)