Skip to content

Commit 138830a

Browse files
committed
chore: resolving the issues post testing
1 parent d4f713f commit 138830a

File tree

5 files changed

+34
-67
lines changed

5 files changed

+34
-67
lines changed

examples/nacls/main.tf

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ locals {
1111
module "vpc" {
1212
source = "../.."
1313

14-
availability_zones = 3
15-
enable_ssm = true
16-
name = "operations"
17-
tags = local.tags
18-
vpc_cidr = "10.100.0.0/21"
14+
availability_zones = 2
15+
enable_ssm = false
16+
enable_route53_resolver_rules = false
17+
name = "operations"
18+
tags = local.tags
19+
vpc_cidr = "10.100.0.0/21"
1920

2021
subnets = {
2122
private = {
2223
netmask = 24
2324
}
24-
public = {
25-
netmask = 24
26-
}
2725
}
2826

2927
nacl_rules = {
@@ -35,7 +33,7 @@ module "vpc" {
3533
to_port = 22
3634
protocol = -1
3735
rule_action = "allow"
38-
rule_number = 100
36+
rule_number = 50
3937
}
4038
]
4139
outbound_rules = [
@@ -45,7 +43,7 @@ module "vpc" {
4543
to_port = 22
4644
protocol = -1
4745
rule_action = "allow"
48-
rule_number = 100
46+
rule_number = 50
4947
}
5048
]
5149
}

main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ module "nacls" {
2222
for_each = var.nacl_rules
2323
source = "./modules/nacls"
2424

25-
vpc_id = module.vpc.vpc_attributes.id
26-
subnet_count = var.availability_zones
27-
subnet_ids = local.all_subnets_by_name[each.key].ids
2825
inbound_rules = var.nacl_rules[each.key].inbound_rules
26+
name = each.key
2927
outbound_rules = var.nacl_rules[each.key].outbound_rules
28+
subnet_count = var.availability_zones
29+
subnet_ids = local.all_subnets_by_name[each.key].ids
3030
tags = var.tags
31+
vpc_id = module.vpc.vpc_attributes.id
3132

3233
depends_on = [module.vpc]
3334
}
@@ -59,7 +60,7 @@ resource "aws_route53_resolver_query_log_config_association" "dns_query_log_asso
5960

6061
## Associate any resolver rules with the vpc if required
6162
resource "aws_route53_resolver_rule_association" "vpc_associations" {
62-
for_each = var.enable_route53_resolver_rules ? toset(local.resolver_rules) : null
63+
for_each = var.enable_route53_resolver_rules ? toset(local.resolver_rules) : []
6364

6465
resolver_rule_id = each.value
6566
vpc_id = module.vpc.vpc_attributes.id

modules/nacls/main.tf

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11

22
## Provision the inbound NACL
3-
resource "aws_network_acl" "inbound" {
3+
resource "aws_network_acl" "nacl" {
44
vpc_id = var.vpc_id
5-
tags = var.tags
5+
tags = merge(var.tags, { Name = "${var.name}" })
66
}
77

88
## Provision the outbound NACL
9-
resource "aws_network_acl" "outbound" {
10-
vpc_id = var.vpc_id
11-
tags = var.tags
12-
}
13-
149
## Provision the inbound NACL rules
1510
resource "aws_network_acl_rule" "inbound" {
1611
for_each = local.inbound
@@ -21,21 +16,13 @@ resource "aws_network_acl_rule" "inbound" {
2116
icmp_code = each.value.rule.icmp_code
2217
icmp_type = each.value.rule.icmp_type
2318
ipv6_cidr_block = each.value.rule.ipv6_cidr_block
24-
network_acl_id = aws_network_acl.inbound.id
19+
network_acl_id = aws_network_acl.nacl.id
2520
protocol = each.value.rule.protocol
2621
rule_action = each.value.rule.rule_action
2722
rule_number = each.value.rule.rule_number
2823
to_port = each.value.rule.to_port
2924
}
3025

31-
## Associate the inbound NACL with the subnets
32-
resource "aws_network_acl_association" "inbound" {
33-
for_each = local.inbound
34-
35-
network_acl_id = aws_network_acl.inbound.id
36-
subnet_id = each.value.id
37-
}
38-
3926
## Provision the outbound NACL rules
4027
resource "aws_network_acl_rule" "outbound" {
4128
for_each = local.outbound
@@ -46,17 +33,17 @@ resource "aws_network_acl_rule" "outbound" {
4633
icmp_code = each.value.rule.icmp_code
4734
icmp_type = each.value.rule.icmp_type
4835
ipv6_cidr_block = each.value.rule.ipv6_cidr_block
49-
network_acl_id = aws_network_acl.outbound.id
36+
network_acl_id = aws_network_acl.nacl.id
5037
protocol = each.value.rule.protocol
5138
rule_action = each.value.rule.rule_action
5239
rule_number = each.value.rule.rule_number
5340
to_port = each.value.rule.to_port
5441
}
5542

56-
## Associate the outbound NACL with the subnets
57-
resource "aws_network_acl_association" "outbound" {
58-
for_each = local.outbound
43+
## Associate the inbound NACL with the subnets
44+
resource "aws_network_acl_association" "nacl" {
45+
for_each = local.inbound
5946

60-
network_acl_id = aws_network_acl.outbound.id
47+
network_acl_id = aws_network_acl.nacl.id
6148
subnet_id = each.value.id
6249
}

modules/nacls/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
variable "name" {
3+
description = "The name of the subnets to create the NACL for"
4+
type = string
5+
}
6+
27
variable "vpc_id" {
38
description = "The VPC ID to create the NACL in"
49
type = string

tests/nacls.tftest.hcl

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ run "check_nacl_rules" {
7575
}
7676

7777
variables {
78+
name = "private"
7879
vpc_id = "vpc-1234567890"
7980
subnet_count = 3
8081
subnet_ids = ["subnet-1234567890", "subnet-1234567891", "subnet-1234567892"]
@@ -106,63 +107,38 @@ run "check_nacl_rules" {
106107
}
107108

108109
assert {
109-
condition = aws_network_acl.inbound != null
110+
condition = aws_network_acl.nacl != null
110111
error_message = "Inbound NACL should be created"
111112
}
112113

113114
assert {
114-
condition = aws_network_acl.inbound.vpc_id == "vpc-1234567890"
115+
condition = aws_network_acl.nacl.vpc_id == "vpc-1234567890"
115116
error_message = "Inbound NACL should be associated with the VPC"
116117
}
117118

118119
assert {
119-
condition = aws_network_acl.outbound != null
120-
error_message = "Outbound NACL should be created"
121-
}
122-
123-
assert {
124-
condition = length(aws_network_acl.inbound.tags) > 0
120+
condition = length(aws_network_acl.nacl.tags) > 0
125121
error_message = "Inbound NACL should have the correct tags"
126122
}
127123

128124
assert {
129-
condition = aws_network_acl.outbound.vpc_id == "vpc-1234567890"
125+
condition = aws_network_acl.nacl.vpc_id == "vpc-1234567890"
130126
error_message = "Outbound NACL should be associated with the VPC"
131127
}
132128

133-
assert {
134-
condition = length(aws_network_acl.outbound.tags) > 0
135-
error_message = "Outbound NACL should have the correct tags"
136-
}
137-
138129
assert {
139130
error_message = "It should associate the inbound NACL with the subnets"
140-
condition = aws_network_acl_association.inbound["0-0"].subnet_id == "subnet-1234567890"
131+
condition = aws_network_acl_association.nacl["0-0"].subnet_id == "subnet-1234567890"
141132
}
142133

143134
assert {
144135
error_message = "It should associate the inbound NACL with the subnets"
145-
condition = aws_network_acl_association.inbound["1-0"].subnet_id == "subnet-1234567891"
136+
condition = aws_network_acl_association.nacl["1-0"].subnet_id == "subnet-1234567891"
146137
}
147138

148139
assert {
149140
error_message = "It should associate the inbound NACL with the subnets"
150-
condition = aws_network_acl_association.inbound["2-0"].subnet_id == "subnet-1234567892"
151-
}
152-
153-
assert {
154-
error_message = "It should associate the outbound NACL with the subnets"
155-
condition = aws_network_acl_association.outbound["0-0"].subnet_id == "subnet-1234567890"
156-
}
157-
158-
assert {
159-
error_message = "It should associate the outbound NACL with the subnets"
160-
condition = aws_network_acl_association.outbound["1-0"].subnet_id == "subnet-1234567891"
161-
}
162-
163-
assert {
164-
error_message = "It should associate the outbound NACL with the subnets"
165-
condition = aws_network_acl_association.outbound["2-0"].subnet_id == "subnet-1234567892"
141+
condition = aws_network_acl_association.nacl["2-0"].subnet_id == "subnet-1234567892"
166142
}
167143

168144
assert {

0 commit comments

Comments
 (0)