Skip to content

Commit 4416e37

Browse files
feat: Add custom subnet names (#816)
Co-authored-by: Anton Babenko <[email protected]>
1 parent 386de73 commit 4416e37

File tree

5 files changed

+78
-22
lines changed

5 files changed

+78
-22
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.75.0
3+
rev: v1.76.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ No modules.
380380
| <a name="input_database_subnet_group_name"></a> [database\_subnet\_group\_name](#input\_database\_subnet\_group\_name) | Name of database subnet group | `string` | `null` | no |
381381
| <a name="input_database_subnet_group_tags"></a> [database\_subnet\_group\_tags](#input\_database\_subnet\_group\_tags) | Additional tags for the database subnet group | `map(string)` | `{}` | no |
382382
| <a name="input_database_subnet_ipv6_prefixes"></a> [database\_subnet\_ipv6\_prefixes](#input\_database\_subnet\_ipv6\_prefixes) | Assigns IPv6 database subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list | `list(string)` | `[]` | no |
383+
| <a name="input_database_subnet_names"></a> [database\_subnet\_names](#input\_database\_subnet\_names) | Explicit values to use in the Name tag on database subnets. If empty, Name tags are generated. | `list(string)` | `[]` | no |
383384
| <a name="input_database_subnet_suffix"></a> [database\_subnet\_suffix](#input\_database\_subnet\_suffix) | Suffix to append to database subnets name | `string` | `"db"` | no |
384385
| <a name="input_database_subnet_tags"></a> [database\_subnet\_tags](#input\_database\_subnet\_tags) | Additional tags for the database subnets | `map(string)` | `{}` | no |
385386
| <a name="input_database_subnets"></a> [database\_subnets](#input\_database\_subnets) | A list of database subnets | `list(string)` | `[]` | no |
@@ -415,6 +416,7 @@ No modules.
415416
| <a name="input_elasticache_subnet_group_name"></a> [elasticache\_subnet\_group\_name](#input\_elasticache\_subnet\_group\_name) | Name of elasticache subnet group | `string` | `null` | no |
416417
| <a name="input_elasticache_subnet_group_tags"></a> [elasticache\_subnet\_group\_tags](#input\_elasticache\_subnet\_group\_tags) | Additional tags for the elasticache subnet group | `map(string)` | `{}` | no |
417418
| <a name="input_elasticache_subnet_ipv6_prefixes"></a> [elasticache\_subnet\_ipv6\_prefixes](#input\_elasticache\_subnet\_ipv6\_prefixes) | Assigns IPv6 elasticache subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list | `list(string)` | `[]` | no |
419+
| <a name="input_elasticache_subnet_names"></a> [elasticache\_subnet\_names](#input\_elasticache\_subnet\_names) | Explicit values to use in the Name tag on elasticache subnets. If empty, Name tags are generated. | `list(string)` | `[]` | no |
418420
| <a name="input_elasticache_subnet_suffix"></a> [elasticache\_subnet\_suffix](#input\_elasticache\_subnet\_suffix) | Suffix to append to elasticache subnets name | `string` | `"elasticache"` | no |
419421
| <a name="input_elasticache_subnet_tags"></a> [elasticache\_subnet\_tags](#input\_elasticache\_subnet\_tags) | Additional tags for the elasticache subnets | `map(string)` | `{}` | no |
420422
| <a name="input_elasticache_subnets"></a> [elasticache\_subnets](#input\_elasticache\_subnets) | A list of elasticache subnets | `list(string)` | `[]` | no |
@@ -451,6 +453,7 @@ No modules.
451453
| <a name="input_intra_route_table_tags"></a> [intra\_route\_table\_tags](#input\_intra\_route\_table\_tags) | Additional tags for the intra route tables | `map(string)` | `{}` | no |
452454
| <a name="input_intra_subnet_assign_ipv6_address_on_creation"></a> [intra\_subnet\_assign\_ipv6\_address\_on\_creation](#input\_intra\_subnet\_assign\_ipv6\_address\_on\_creation) | Assign IPv6 address on intra subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map\_public\_ip\_on\_launch | `bool` | `null` | no |
453455
| <a name="input_intra_subnet_ipv6_prefixes"></a> [intra\_subnet\_ipv6\_prefixes](#input\_intra\_subnet\_ipv6\_prefixes) | Assigns IPv6 intra subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list | `list(string)` | `[]` | no |
456+
| <a name="input_intra_subnet_names"></a> [intra\_subnet\_names](#input\_intra\_subnet\_names) | Explicit values to use in the Name tag on intra subnets. If empty, Name tags are generated. | `list(string)` | `[]` | no |
454457
| <a name="input_intra_subnet_suffix"></a> [intra\_subnet\_suffix](#input\_intra\_subnet\_suffix) | Suffix to append to intra subnets name | `string` | `"intra"` | no |
455458
| <a name="input_intra_subnet_tags"></a> [intra\_subnet\_tags](#input\_intra\_subnet\_tags) | Additional tags for the intra subnets | `map(string)` | `{}` | no |
456459
| <a name="input_intra_subnets"></a> [intra\_subnets](#input\_intra\_subnets) | A list of intra subnets | `list(string)` | `[]` | no |
@@ -477,6 +480,7 @@ No modules.
477480
| <a name="input_outpost_outbound_acl_rules"></a> [outpost\_outbound\_acl\_rules](#input\_outpost\_outbound\_acl\_rules) | Outpost subnets outbound network ACLs | `list(map(string))` | <pre>[<br> {<br> "cidr_block": "0.0.0.0/0",<br> "from_port": 0,<br> "protocol": "-1",<br> "rule_action": "allow",<br> "rule_number": 100,<br> "to_port": 0<br> }<br>]</pre> | no |
478481
| <a name="input_outpost_subnet_assign_ipv6_address_on_creation"></a> [outpost\_subnet\_assign\_ipv6\_address\_on\_creation](#input\_outpost\_subnet\_assign\_ipv6\_address\_on\_creation) | Assign IPv6 address on outpost subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map\_public\_ip\_on\_launch | `bool` | `null` | no |
479482
| <a name="input_outpost_subnet_ipv6_prefixes"></a> [outpost\_subnet\_ipv6\_prefixes](#input\_outpost\_subnet\_ipv6\_prefixes) | Assigns IPv6 outpost subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list | `list(string)` | `[]` | no |
483+
| <a name="input_outpost_subnet_names"></a> [outpost\_subnet\_names](#input\_outpost\_subnet\_names) | Explicit values to use in the Name tag on outpost subnets. If empty, Name tags are generated. | `list(string)` | `[]` | no |
480484
| <a name="input_outpost_subnet_suffix"></a> [outpost\_subnet\_suffix](#input\_outpost\_subnet\_suffix) | Suffix to append to outpost subnets name | `string` | `"outpost"` | no |
481485
| <a name="input_outpost_subnet_tags"></a> [outpost\_subnet\_tags](#input\_outpost\_subnet\_tags) | Additional tags for the outpost subnets | `map(string)` | `{}` | no |
482486
| <a name="input_outpost_subnets"></a> [outpost\_subnets](#input\_outpost\_subnets) | A list of outpost subnets inside the VPC | `list(string)` | `[]` | no |
@@ -487,6 +491,7 @@ No modules.
487491
| <a name="input_private_route_table_tags"></a> [private\_route\_table\_tags](#input\_private\_route\_table\_tags) | Additional tags for the private route tables | `map(string)` | `{}` | no |
488492
| <a name="input_private_subnet_assign_ipv6_address_on_creation"></a> [private\_subnet\_assign\_ipv6\_address\_on\_creation](#input\_private\_subnet\_assign\_ipv6\_address\_on\_creation) | Assign IPv6 address on private subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map\_public\_ip\_on\_launch | `bool` | `null` | no |
489493
| <a name="input_private_subnet_ipv6_prefixes"></a> [private\_subnet\_ipv6\_prefixes](#input\_private\_subnet\_ipv6\_prefixes) | Assigns IPv6 private subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list | `list(string)` | `[]` | no |
494+
| <a name="input_private_subnet_names"></a> [private\_subnet\_names](#input\_private\_subnet\_names) | Explicit values to use in the Name tag on private subnets. If empty, Name tags are generated. | `list(string)` | `[]` | no |
490495
| <a name="input_private_subnet_suffix"></a> [private\_subnet\_suffix](#input\_private\_subnet\_suffix) | Suffix to append to private subnets name | `string` | `"private"` | no |
491496
| <a name="input_private_subnet_tags"></a> [private\_subnet\_tags](#input\_private\_subnet\_tags) | Additional tags for the private subnets | `map(string)` | `{}` | no |
492497
| <a name="input_private_subnets"></a> [private\_subnets](#input\_private\_subnets) | A list of private subnets inside the VPC | `list(string)` | `[]` | no |
@@ -500,6 +505,7 @@ No modules.
500505
| <a name="input_public_route_table_tags"></a> [public\_route\_table\_tags](#input\_public\_route\_table\_tags) | Additional tags for the public route tables | `map(string)` | `{}` | no |
501506
| <a name="input_public_subnet_assign_ipv6_address_on_creation"></a> [public\_subnet\_assign\_ipv6\_address\_on\_creation](#input\_public\_subnet\_assign\_ipv6\_address\_on\_creation) | Assign IPv6 address on public subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map\_public\_ip\_on\_launch | `bool` | `null` | no |
502507
| <a name="input_public_subnet_ipv6_prefixes"></a> [public\_subnet\_ipv6\_prefixes](#input\_public\_subnet\_ipv6\_prefixes) | Assigns IPv6 public subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list | `list(string)` | `[]` | no |
508+
| <a name="input_public_subnet_names"></a> [public\_subnet\_names](#input\_public\_subnet\_names) | Explicit values to use in the Name tag on public subnets. If empty, Name tags are generated. | `list(string)` | `[]` | no |
503509
| <a name="input_public_subnet_suffix"></a> [public\_subnet\_suffix](#input\_public\_subnet\_suffix) | Suffix to append to public subnets name | `string` | `"public"` | no |
504510
| <a name="input_public_subnet_tags"></a> [public\_subnet\_tags](#input\_public\_subnet\_tags) | Additional tags for the public subnets | `map(string)` | `{}` | no |
505511
| <a name="input_public_subnets"></a> [public\_subnets](#input\_public\_subnets) | A list of public subnets inside the VPC | `list(string)` | `[]` | no |
@@ -513,6 +519,7 @@ No modules.
513519
| <a name="input_redshift_subnet_group_name"></a> [redshift\_subnet\_group\_name](#input\_redshift\_subnet\_group\_name) | Name of redshift subnet group | `string` | `null` | no |
514520
| <a name="input_redshift_subnet_group_tags"></a> [redshift\_subnet\_group\_tags](#input\_redshift\_subnet\_group\_tags) | Additional tags for the redshift subnet group | `map(string)` | `{}` | no |
515521
| <a name="input_redshift_subnet_ipv6_prefixes"></a> [redshift\_subnet\_ipv6\_prefixes](#input\_redshift\_subnet\_ipv6\_prefixes) | Assigns IPv6 redshift subnet id based on the Amazon provided /56 prefix base 10 integer (0-256). Must be of equal length to the corresponding IPv4 subnet list | `list(string)` | `[]` | no |
522+
| <a name="input_redshift_subnet_names"></a> [redshift\_subnet\_names](#input\_redshift\_subnet\_names) | Explicit values to use in the Name tag on redshift subnets. If empty, Name tags are generated. | `list(string)` | `[]` | no |
516523
| <a name="input_redshift_subnet_suffix"></a> [redshift\_subnet\_suffix](#input\_redshift\_subnet\_suffix) | Suffix to append to redshift subnets name | `string` | `"redshift"` | no |
517524
| <a name="input_redshift_subnet_tags"></a> [redshift\_subnet\_tags](#input\_redshift\_subnet\_tags) | Additional tags for the redshift subnets | `map(string)` | `{}` | no |
518525
| <a name="input_redshift_subnets"></a> [redshift\_subnets](#input\_redshift\_subnets) | A list of redshift subnets | `list(string)` | `[]` | no |

examples/complete-vpc/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ module "vpc" {
3131
redshift_subnets = ["10.0.41.0/24", "10.0.42.0/24", "10.0.43.0/24"]
3232
intra_subnets = ["10.0.51.0/24", "10.0.52.0/24", "10.0.53.0/24"]
3333

34+
private_subnet_names = ["Private Subnet One", "Private Subnet Two"]
35+
# public_subnet_names omitted to show default name generation for all three subnets
36+
database_subnet_names = ["DB Subnet One"]
37+
elasticache_subnet_names = ["Elasticache Subnet One", "Elasticache Subnet Two"]
38+
redshift_subnet_names = ["Redshift Subnet One", "Redshift Subnet Two", "Redshift Subnet Three"]
39+
intra_subnet_names = []
40+
3441
create_database_subnet_group = false
3542

3643
manage_default_network_acl = true

main.tf

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,9 @@ resource "aws_subnet" "public" {
370370

371371
tags = merge(
372372
{
373-
"Name" = format(
374-
"${var.name}-${var.public_subnet_suffix}-%s",
375-
element(var.azs, count.index),
373+
Name = try(
374+
var.public_subnet_names[count.index],
375+
format("${var.name}-${var.public_subnet_suffix}-%s", element(var.azs, count.index))
376376
)
377377
},
378378
var.tags,
@@ -397,9 +397,9 @@ resource "aws_subnet" "private" {
397397

398398
tags = merge(
399399
{
400-
"Name" = format(
401-
"${var.name}-${var.private_subnet_suffix}-%s",
402-
element(var.azs, count.index),
400+
Name = try(
401+
var.private_subnet_names[count.index],
402+
format("${var.name}-${var.private_subnet_suffix}-%s", element(var.azs, count.index))
403403
)
404404
},
405405
var.tags,
@@ -425,9 +425,9 @@ resource "aws_subnet" "outpost" {
425425

426426
tags = merge(
427427
{
428-
"Name" = format(
429-
"${var.name}-${var.outpost_subnet_suffix}-%s",
430-
var.outpost_az,
428+
Name = try(
429+
var.outpost_subnet_names[count.index],
430+
format("${var.name}-${var.outpost_subnet_suffix}-%s", var.outpost_az)
431431
)
432432
},
433433
var.tags,
@@ -452,9 +452,9 @@ resource "aws_subnet" "database" {
452452

453453
tags = merge(
454454
{
455-
"Name" = format(
456-
"${var.name}-${var.database_subnet_suffix}-%s",
457-
element(var.azs, count.index),
455+
Name = try(
456+
var.database_subnet_names[count.index],
457+
format("${var.name}-${var.database_subnet_suffix}-%s", element(var.azs, count.index), )
458458
)
459459
},
460460
var.tags,
@@ -495,9 +495,9 @@ resource "aws_subnet" "redshift" {
495495

496496
tags = merge(
497497
{
498-
"Name" = format(
499-
"${var.name}-${var.redshift_subnet_suffix}-%s",
500-
element(var.azs, count.index),
498+
Name = try(
499+
var.redshift_subnet_names[count.index],
500+
format("${var.name}-${var.redshift_subnet_suffix}-%s", element(var.azs, count.index))
501501
)
502502
},
503503
var.tags,
@@ -536,9 +536,9 @@ resource "aws_subnet" "elasticache" {
536536

537537
tags = merge(
538538
{
539-
"Name" = format(
540-
"${var.name}-${var.elasticache_subnet_suffix}-%s",
541-
element(var.azs, count.index),
539+
Name = try(
540+
var.elasticache_subnet_names[count.index],
541+
format("${var.name}-${var.elasticache_subnet_suffix}-%s", element(var.azs, count.index))
542542
)
543543
},
544544
var.tags,
@@ -577,9 +577,9 @@ resource "aws_subnet" "intra" {
577577

578578
tags = merge(
579579
{
580-
"Name" = format(
581-
"${var.name}-${var.intra_subnet_suffix}-%s",
582-
element(var.azs, count.index),
580+
Name = try(
581+
var.intra_subnet_names[count.index],
582+
format("${var.name}-${var.intra_subnet_suffix}-%s", element(var.azs, count.index))
583583
)
584584
},
585585
var.tags,

variables.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,48 @@ variable "private_subnet_suffix" {
136136
default = "private"
137137
}
138138

139+
variable "public_subnet_names" {
140+
description = "Explicit values to use in the Name tag on public subnets. If empty, Name tags are generated."
141+
type = list(string)
142+
default = []
143+
}
144+
145+
variable "private_subnet_names" {
146+
description = "Explicit values to use in the Name tag on private subnets. If empty, Name tags are generated."
147+
type = list(string)
148+
default = []
149+
}
150+
151+
variable "outpost_subnet_names" {
152+
description = "Explicit values to use in the Name tag on outpost subnets. If empty, Name tags are generated."
153+
type = list(string)
154+
default = []
155+
}
156+
157+
variable "intra_subnet_names" {
158+
description = "Explicit values to use in the Name tag on intra subnets. If empty, Name tags are generated."
159+
type = list(string)
160+
default = []
161+
}
162+
163+
variable "database_subnet_names" {
164+
description = "Explicit values to use in the Name tag on database subnets. If empty, Name tags are generated."
165+
type = list(string)
166+
default = []
167+
}
168+
169+
variable "redshift_subnet_names" {
170+
description = "Explicit values to use in the Name tag on redshift subnets. If empty, Name tags are generated."
171+
type = list(string)
172+
default = []
173+
}
174+
175+
variable "elasticache_subnet_names" {
176+
description = "Explicit values to use in the Name tag on elasticache subnets. If empty, Name tags are generated."
177+
type = list(string)
178+
default = []
179+
}
180+
139181
variable "outpost_subnet_suffix" {
140182
description = "Suffix to append to outpost subnets name"
141183
type = string

0 commit comments

Comments
 (0)