Skip to content

Commit 24675d6

Browse files
feat: add possibility to disable resources creation (#87)
* feat: add possibility to disable resources creation * Updated README.md Co-authored-by: actions-bot <[email protected]>
1 parent 93311c1 commit 24675d6

File tree

9 files changed

+47
-30
lines changed

9 files changed

+47
-30
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Available targets:
217217
| cidr\_block | Base CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | n/a | yes |
218218
| context | Default context to use for passing state between label invocations | <pre>object({<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> enabled = bool<br> delimiter = string<br> attributes = list(string)<br> label_order = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": "",<br> "enabled": true,<br> "environment": "",<br> "label_order": [],<br> "name": "",<br> "namespace": "",<br> "regex_replace_chars": "",<br> "stage": "",<br> "tags": {}<br>}</pre> | no |
219219
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
220+
| enabled | Set to false to prevent the module from creating any resources | `bool` | `true` | no |
220221
| environment | The environment name if not using stage | `string` | `""` | no |
221222
| igw\_id | Internet Gateway ID the public route table will point to (e.g. `igw-9c26a123`) | `string` | n/a | yes |
222223
| label\_order | The naming order of the ID output and Name tag | `list(string)` | `[]` | no |

docs/terraform.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| cidr\_block | Base CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | `string` | n/a | yes |
2525
| context | Default context to use for passing state between label invocations | <pre>object({<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> enabled = bool<br> delimiter = string<br> attributes = list(string)<br> label_order = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": "",<br> "enabled": true,<br> "environment": "",<br> "label_order": [],<br> "name": "",<br> "namespace": "",<br> "regex_replace_chars": "",<br> "stage": "",<br> "tags": {}<br>}</pre> | no |
2626
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
27+
| enabled | Set to false to prevent the module from creating any resources | `bool` | `true` | no |
2728
| environment | The environment name if not using stage | `string` | `""` | no |
2829
| igw\_id | Internet Gateway ID the public route table will point to (e.g. `igw-9c26a123`) | `string` | n/a | yes |
2930
| label\_order | The naming order of the ID output and Name tag | `list(string)` | `[]` | no |

label.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module "label" {
1111
regex_replace_chars = var.regex_replace_chars
1212
label_order = var.label_order
1313
context = var.context
14+
enabled = var.enabled
1415
}
1516

1617
variable "additional_tag_map" {

main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# Get object aws_vpc by vpc_id
22
data "aws_vpc" "default" {
3-
id = var.vpc_id
3+
count = var.enabled ? 1 : 0
4+
id = var.vpc_id
45
}
56

67
data "aws_availability_zones" "available" {
8+
count = var.enabled ? 1 : 0
9+
}
10+
11+
locals {
12+
availability_zones_count = var.enabled ? length(var.availability_zones) : 0
13+
enabled = var.enabled ? 1 : 0
714
}

nat-gateway.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module "nat_label" {
55
}
66

77
locals {
8-
nat_gateways_count = var.nat_gateway_enabled ? length(var.availability_zones) : 0
8+
nat_gateways_count = var.enabled && var.nat_gateway_enabled ? local.availability_zones_count : 0
99
}
1010

1111
resource "aws_eip" "default" {

nat-instance.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ module "nat_instance_label" {
55
}
66

77
locals {
8-
nat_instance_count = var.nat_instance_enabled ? length(var.availability_zones) : 0
9-
cidr_block = var.cidr_block != "" ? var.cidr_block : data.aws_vpc.default.cidr_block
8+
nat_instance_count = var.enabled && var.nat_instance_enabled ? local.availability_zones_count : 0
9+
cidr_block = var.cidr_block != "" ? var.cidr_block : join("", data.aws_vpc.default.*.cidr_block)
10+
nat_instance_enabled = var.enabled && var.nat_instance_enabled ? 1 : 0
1011
}
1112

1213
resource "aws_security_group" "nat_instance" {
13-
count = var.nat_instance_enabled ? 1 : 0
14+
count = local.nat_instance_enabled
1415
name = module.nat_instance_label.id
1516
description = "Security Group for NAT Instance"
1617
vpc_id = var.vpc_id
1718
tags = module.nat_instance_label.tags
1819
}
1920

2021
resource "aws_security_group_rule" "nat_instance_egress" {
21-
count = var.nat_instance_enabled ? 1 : 0
22+
count = local.nat_instance_enabled
2223
description = "Allow all egress traffic"
2324
from_port = 0
2425
to_port = 0
@@ -29,7 +30,7 @@ resource "aws_security_group_rule" "nat_instance_egress" {
2930
}
3031

3132
resource "aws_security_group_rule" "nat_instance_ingress" {
32-
count = var.nat_instance_enabled ? 1 : 0
33+
count = local.nat_instance_enabled
3334
description = "Allow ingress traffic from the VPC CIDR block"
3435
from_port = 0
3536
to_port = 0
@@ -41,7 +42,7 @@ resource "aws_security_group_rule" "nat_instance_ingress" {
4142

4243
// aws --region us-west-2 ec2 describe-images --owners amazon --filters Name="name",Values="amzn-ami-vpc-nat*" Name="virtualization-type",Values="hvm"
4344
data "aws_ami" "nat_instance" {
44-
count = var.nat_instance_enabled ? 1 : 0
45+
count = local.nat_instance_enabled
4546
most_recent = true
4647

4748
filter {
@@ -131,4 +132,3 @@ resource "aws_route" "nat_instance" {
131132
destination_cidr_block = "0.0.0.0/0"
132133
depends_on = [aws_route_table.private]
133134
}
134-

private.tf

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ module "private_label" {
1111
}
1212

1313
locals {
14-
private_subnet_count = var.max_subnet_count == 0 ? length(data.aws_availability_zones.available.names) : var.max_subnet_count
14+
private_subnet_count = var.enabled && var.max_subnet_count == 0 ? length(flatten(data.aws_availability_zones.available.*.names)) : var.max_subnet_count
15+
private_network_acl_enabled = var.enabled && signum(length(var.private_network_acl_id)) == 0 ? 1 : 0
1516
}
1617

1718
resource "aws_subnet" "private" {
18-
count = length(var.availability_zones)
19-
vpc_id = data.aws_vpc.default.id
19+
count = local.availability_zones_count
20+
vpc_id = join("", data.aws_vpc.default.*.id)
2021
availability_zone = element(var.availability_zones, count.index)
2122

2223
cidr_block = cidrsubnet(
23-
signum(length(var.cidr_block)) == 1 ? var.cidr_block : data.aws_vpc.default.cidr_block,
24+
signum(length(var.cidr_block)) == 1 ? var.cidr_block : join("", data.aws_vpc.default.*.cidr_block),
2425
ceil(log(local.private_subnet_count * 2, 2)),
2526
count.index
2627
)
@@ -48,8 +49,8 @@ resource "aws_subnet" "private" {
4849
}
4950

5051
resource "aws_route_table" "private" {
51-
count = length(var.availability_zones)
52-
vpc_id = data.aws_vpc.default.id
52+
count = local.availability_zones_count
53+
vpc_id = join("", data.aws_vpc.default.*.id)
5354

5455
tags = merge(
5556
module.private_label.tags,
@@ -69,14 +70,13 @@ resource "aws_route_table" "private" {
6970
}
7071

7172
resource "aws_route_table_association" "private" {
72-
count = length(var.availability_zones)
73-
73+
count = local.availability_zones_count
7474
subnet_id = element(aws_subnet.private.*.id, count.index)
7575
route_table_id = element(aws_route_table.private.*.id, count.index)
7676
}
7777

7878
resource "aws_network_acl" "private" {
79-
count = signum(length(var.private_network_acl_id)) == 0 ? 1 : 0
79+
count = local.private_network_acl_enabled
8080
vpc_id = var.vpc_id
8181
subnet_ids = aws_subnet.private.*.id
8282

@@ -100,4 +100,3 @@ resource "aws_network_acl" "private" {
100100

101101
tags = module.private_label.tags
102102
}
103-

public.tf

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ module "public_label" {
1111
}
1212

1313
locals {
14-
public_subnet_count = var.max_subnet_count == 0 ? length(data.aws_availability_zones.available.names) : var.max_subnet_count
14+
public_subnet_count = var.enabled && var.max_subnet_count == 0 ? length(flatten(data.aws_availability_zones.available.*.names)) : var.max_subnet_count
15+
public_route_expr_enabled = var.enabled && signum(length(var.vpc_default_route_table_id)) == 1
16+
public_network_acl_enabled = var.enabled && signum(length(var.public_network_acl_id)) == 0 ? 1 : 0
17+
vpc_default_route_table_id = var.enabled ? signum(length(var.vpc_default_route_table_id)) : 0
1518
}
1619

1720
resource "aws_subnet" "public" {
18-
count = length(var.availability_zones)
19-
vpc_id = data.aws_vpc.default.id
21+
count = local.availability_zones_count
22+
vpc_id = join("", data.aws_vpc.default.*.id)
2023
availability_zone = element(var.availability_zones, count.index)
2124

2225
cidr_block = cidrsubnet(
23-
signum(length(var.cidr_block)) == 1 ? var.cidr_block : data.aws_vpc.default.cidr_block,
26+
signum(length(var.cidr_block)) == 1 ? var.cidr_block : join("", data.aws_vpc.default.*.cidr_block),
2427
ceil(log(local.public_subnet_count * 2, 2)),
2528
local.public_subnet_count + count.index
2629
)
@@ -49,33 +52,33 @@ resource "aws_subnet" "public" {
4952
}
5053

5154
resource "aws_route_table" "public" {
52-
count = signum(length(var.vpc_default_route_table_id)) == 1 ? 0 : 1
53-
vpc_id = data.aws_vpc.default.id
55+
count = local.public_route_expr_enabled ? 0 : local.enabled
56+
vpc_id = join("", data.aws_vpc.default.*.id)
5457

5558
tags = module.public_label.tags
5659
}
5760

5861
resource "aws_route" "public" {
59-
count = signum(length(var.vpc_default_route_table_id)) == 1 ? 0 : 1
62+
count = local.public_route_expr_enabled ? 0 : local.enabled
6063
route_table_id = join("", aws_route_table.public.*.id)
6164
destination_cidr_block = "0.0.0.0/0"
6265
gateway_id = var.igw_id
6366
}
6467

6568
resource "aws_route_table_association" "public" {
66-
count = signum(length(var.vpc_default_route_table_id)) == 1 ? 0 : length(var.availability_zones)
69+
count = local.public_route_expr_enabled ? 0 : local.availability_zones_count
6770
subnet_id = element(aws_subnet.public.*.id, count.index)
6871
route_table_id = aws_route_table.public[0].id
6972
}
7073

7174
resource "aws_route_table_association" "public_default" {
72-
count = signum(length(var.vpc_default_route_table_id)) == 1 ? length(var.availability_zones) : 0
75+
count = local.public_route_expr_enabled ? local.availability_zones_count : 0
7376
subnet_id = element(aws_subnet.public.*.id, count.index)
7477
route_table_id = var.vpc_default_route_table_id
7578
}
7679

7780
resource "aws_network_acl" "public" {
78-
count = signum(length(var.public_network_acl_id)) == 0 ? 1 : 0
81+
count = local.public_network_acl_enabled
7982
vpc_id = var.vpc_id
8083
subnet_ids = aws_subnet.public.*.id
8184

@@ -99,4 +102,3 @@ resource "aws_network_acl" "public" {
99102

100103
tags = module.public_label.tags
101104
}
102-

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
variable "enabled" {
2+
type = bool
3+
default = true
4+
description = "Set to false to prevent the module from creating any resources"
5+
}
6+
17
variable "subnet_type_tag_key" {
28
type = string
39
default = "cpco.io/subnet/type"

0 commit comments

Comments
 (0)