You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .header.md
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,3 +78,63 @@ subnets = {
78
78
```
79
79
80
80
The above example will cause only creating 2 new subnets in az `c` of the region being used.
81
+
82
+
## Output manipulation
83
+
84
+
The outputs in this module attempt to align to a methodology of outputting resource attributes in a reasonable collection. The benefit of this is that, most likely, attributes you want access to are already present without having to create new `output {}` for each possible attribute. The [potential] downside is that you will have to extract it yourself using HCL logic. Below are some common examples:
85
+
86
+
### Extracting subnet IDs for private subnets
87
+
88
+
Example Configuration:
89
+
```terraform
90
+
module "vpc" {
91
+
source = "aws-ia/vpc/aws"
92
+
version = ">= 1.0.0"
93
+
94
+
name = "multi-az-vpc"
95
+
cidr_block = "10.0.0.0/20"
96
+
az_count = 3
97
+
98
+
subnets = {
99
+
private = { netmask = 24 }
100
+
}
101
+
}
102
+
```
103
+
104
+
Extracting subnet_ids to a list (using `terraform console` for example output):
105
+
```terraform
106
+
> [ for _, value in module.vpc.private_subnet_attributes_by_az: value.id]
107
+
[
108
+
"subnet-04a86315c4839b519",
109
+
"subnet-02a7249c8652a7136",
110
+
"subnet-09af79b5329b3681f",
111
+
]
112
+
```
113
+
114
+
Alternatively, since these are maps, you can use key in another resource `for_each` loop. The benefit here is that your dependent resource will have keys that match the AZ the subnet is in:
This module organizes outputs by creating output collections of grouped entire resources. The benefit of this is that, most likely, attributes users want access to are already present without having to create new `output {}` for each possible attribute. The [potential] downside is that you will have to extract it yourself using HCL logic. See the [outputs.tf](https://github.com/aws-ia/terraform-aws-vpc/outputs.tf) for examples.
6
+
7
+
Our naming convetion attempts to make the output content clear. `route_table_attributes_by_type_by_az` is a nested map of route table resource attributes grouped by their subnet type then by the az. Example:
8
+
```terraform
9
+
route_table_attributes_by_type_by_az = {
10
+
"private" = {
11
+
"us-east-1a" = {
12
+
"id" = "rtb-0e77040c0598df003"
13
+
"route_table_id" = "rtb-0e77040c0598df003"
14
+
"tags" = tolist([
15
+
{
16
+
"key" = "Name"
17
+
"value" = "private-us-east-1a"
18
+
},
19
+
])
20
+
"vpc_id" = "vpc-033e054f49409592a"
21
+
}
22
+
"us-east-1b" = {
23
+
...
24
+
}
25
+
"public" = { ... }
26
+
```
27
+
28
+
## Adding new subnet types
29
+
30
+
*Note: All subnet types **MUST** accept both `cidrs` and `netmask` arguments.*
31
+
32
+
1. Updates to variables.tf
33
+
34
+
1. Add new to `subnets` key variable validation:
35
+
36
+
```terraform
37
+
validation {
38
+
error_message = "Only valid key values \"public\", \"private\", or \"transit_gateway\"."
*Note: each for_each loop must account for if a user does not want to create the particular subnet type. Follow examples from other subnet types in main.tf*
77
+
78
+
* Create new `aws_subnet`
79
+
* Create new `awscc_ec2_route_table`
80
+
* Create new `awscc_ec2_subnet_route_table_association`
<all attributes of subnet: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet#attributes-reference>
87
+
}
88
+
"us-east-1b" = {...)
89
+
}
90
+
```
91
+
EOF
92
+
}
93
+
94
+
output"route_table_attributes_by_type_by_az" {
95
+
value={
96
+
# TODO: omit keys if value is null
97
+
"private"= awscc_ec2_route_table.private,
98
+
"public"= awscc_ec2_route_table.public
99
+
"transit_gateway"= awscc_ec2_route_table.tgw
100
+
}
101
+
description=<<-EOF
102
+
Map of route tables by type => az => route table attributes. Example usage: module.vpc.route_table_by_subnet_type.private.id
103
+
104
+
Example:
105
+
```
106
+
route_table_attributes_by_type_by_az = {
107
+
"private" = {
108
+
"us-east-1a" = {
109
+
"id" = "rtb-0e77040c0598df003"
110
+
"route_table_id" = "rtb-0e77040c0598df003"
111
+
"tags" = tolist([
112
+
{
113
+
"key" = "Name"
114
+
"value" = "private-us-east-1a"
115
+
},
116
+
])
117
+
"vpc_id" = "vpc-033e054f49409592a"
118
+
}
119
+
"us-east-1b" = { ... }
120
+
"public" = { ... }
121
+
```
122
+
EOF
123
+
}
124
+
125
+
output"nat_gateway_attributes_by_az" {
126
+
value=try(aws_nat_gateway.main, null)
127
+
description=<<-EOF
128
+
Map of nat gateway resource attributes by AZ.
129
+
130
+
Example:
131
+
```
132
+
nat_gateway_attributes_by_az = {
133
+
"us-east-1a" = {
134
+
"allocation_id" = "eipalloc-0e8b20303eea88b13"
135
+
"connectivity_type" = "public"
136
+
"id" = "nat-0fde39f9550f4abb5"
137
+
"network_interface_id" = "eni-0d422727088bf9a86"
138
+
"private_ip" = "10.0.3.40"
139
+
"public_ip" = <>
140
+
"subnet_id" = "subnet-0f11c92e439c8ab4a"
141
+
"tags" = tomap({
142
+
"Name" = "nat-my-public-us-east-1a"
143
+
})
144
+
"tags_all" = tomap({
145
+
"Name" = "nat-my-public-us-east-1a"
146
+
})
147
+
}
148
+
"us-east-1b" = { ... }
149
+
}
150
+
```
151
+
EOF
152
+
}
153
+
154
+
## DEPRECATED OUTPUTS
155
+
156
+
output"subnets" {
157
+
description="DEPRECATED OUTPUT: this output has been renamed to `subnet_cidrs_by_type_by_az`. Please transition to that output and see it for a proper description."
158
+
value=module.calculate_subnets.subnets_by_type
43
159
}
44
160
45
161
output"route_table_by_subnet_type" {
46
-
description="Map of route tables by type => az => route table attributes. Example usage: module.vpc.route_table_by_subnet_type.private.id"
162
+
description="DEPRECATED OUTPUT: this output has been renamed to `route_table_attributes_by_type_by_az`. Please transition to that output and see it for a proper description."
description="Map of nat gateway resource attributes by AZ."
172
+
description="DEPRECATED OUTPUT: this output has been renamed to `nat_gateway_attributes_by_az`. Please transition to that output and see it for a proper description."
Copy file name to clipboardExpand all lines: variables.tf
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -71,23 +71,23 @@ variable "subnets" {
71
71
description=<<-EOF
72
72
Configuration of subnets to build in VPC. 1 Subnet per AZ is created. Subnet types are defined as maps with the available keys: "private", "public", "transit_gateway". Each Subnet type offers its own set of available arguments detailed below.
73
73
74
-
Attributes shared across subnet types:
74
+
**Attributes shared across subnet types:**
75
75
- `cidrs` = (Optional|list(string)) **Cannot set if `netmask` is set.** List of CIDRs to set to subnets. Count of CIDRs defined must match quatity of azs in `az_count`.
76
76
- `netmask` = (Optional|Int) Netmask of the `var.cidr_block` to calculate for each subnet. **Cannot set if `cidrs` is set.**
77
77
- `name_prefix` = (Optional|String) A string prefix to use for the name of your subnet and associated resources. Subnet type key name is used if omitted (aka private, public, transit_gateway). Example `name_prefix = "private"` for `var.subnets.private` is redundant.
78
78
- `tags` = (Optional|map(string)) Tags to set on the subnet and associated resources.
79
79
80
-
`private` subnet type options:
80
+
**private subnet type options:**
81
81
- All shared keys above
82
82
- `route_to_nat` = (Optional|bool) Determines if routes to NAT Gateways should be created. Default = false. Must also set `var.subnets.public.nat_gateway_configuration`.
83
83
- `route_to_transit_gateway` = (Optional|list(string)) Optionally create routes from private subnets to transit gateway subnets.
84
84
85
-
`public` subnet type options:
85
+
**public subnet type options:**
86
86
- All shared keys above
87
87
- `nat_gateway_configuration` = (Optional|string) Determines if NAT Gateways should be created and in how many AZs. Valid values = `"none"`, `"single_az"`, `"all_azs"`. Default = "none". Must also set `var.subnets.private.route_to_nat = true`.
88
88
- `route_to_transit_gateway` = (Optional|list(string)) Optionally create routes from private subnets to transit gateway subnets.
89
89
90
-
`transit_gateway` subnet type options:
90
+
**transit_gateway subnet type options:**
91
91
- All shared keys above
92
92
- `route_to_nat` = (Optional|bool) Determines if routes to NAT Gateways should be created. Default = false. Must also set `var.subnets.public.nat_gateway_configuration`.
93
93
- `transit_gateway_id` = (Required|string) Transit gateway to attach VPC to.
0 commit comments