Skip to content

Commit 7415034

Browse files
authored
Allow public and secure connection (#57)
* Allow public and secure connection * terraform-docs: automated update action --------- Co-authored-by: adenot <[email protected]>
1 parent ee1dccb commit 7415034

File tree

6 files changed

+71
-18
lines changed

6 files changed

+71
-18
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ module "network" {
6868
| create\_dbsubgroup\_private | Create Private Subgroup | `bool` | `false` | no |
6969
| create\_dbsubgroup\_public | Create Public Subgroup | `bool` | `false` | no |
7070
| create\_dbsubgroup\_secure | Create Secure Subgroup | `bool` | `true` | no |
71+
| db\_subnet\_group\_secure\_name\_compat | Use previous DB subnet group name (<name>-dbsubnet) for backwards compability (secure only) | `bool` | `false` | no |
7172
| eip\_allocation\_ids | User-specified primary or secondary private IP address to associate with the Elastic IP address | `list(string)` | `[]` | no |
7273
| enable\_firewall\_default\_rule | Enable or disable the default stateful rule. | `bool` | `true` | no |
7374
| firewall\_custom\_rule\_arn | The stateful rule group arn created outside the module | `list(string)` | `[]` | no |
7475
| firewall\_custom\_rules | The stateful rule group rules specifications in Suricata file format, with one rule per line | `list(string)` | `[]` | no |
75-
| firewall\_domain\_list | List the domain names you want to take action on. | `list(any)` | <pre>[<br> ".amazonaws.com",<br> ".github.com"<br>]</pre> | no |
76+
| firewall\_domain\_list | List the domain names you want to take action on. | `list(any)` | <pre>[<br> ".amazonaws.com",<br> ".github.com"<br>]</pre> | no |
7677
| firewall\_netnum\_offset | Start with this subnet for secure ones, plus number of AZs | `number` | `14` | no |
7778
| kms\_key\_arn | The ARN of the KMS Key to use when encrypting log data. | `string` | `""` | no |
7879
| kubernetes\_clusters | List of kubernetes cluster names to creates tags in public and private subnets of this VPC | `list(string)` | `[]` | no |
@@ -93,6 +94,7 @@ module "network" {
9394
| public\_nacl\_outbound\_tcp\_ports | TCP Ports to allow outbound to external services (use [0] to allow all ports) | `list(string)` | <pre>[<br> "0"<br>]</pre> | no |
9495
| public\_nacl\_outbound\_udp\_ports | UDP Ports to allow outbound to external services (use [0] to allow all ports) | `list(string)` | <pre>[<br> "0"<br>]</pre> | no |
9596
| public\_netnum\_offset | Start with this subnet for public ones, plus number of AZs | `number` | `0` | no |
97+
| secure\_nacl\_allow\_public | Allow traffic between public and secure | `bool` | `false` | no |
9698
| secure\_netnum\_offset | Start with this subnet for secure ones, plus number of AZs | `number` | `10` | no |
9799
| tags | Extra tags to attach to resources | `map(string)` | `{}` | no |
98100
| transit\_nacl\_inbound\_tcp\_ports | TCP Ports to allow inbound on transit subnet via NACLs (this list cannot be empty) | `list(string)` | <pre>[<br> "1194"<br>]</pre> | no |
@@ -103,8 +105,15 @@ module "network" {
103105
| vpc\_cidr\_summ | Define cidr used to summarize subnets by tier | `string` | `"/0"` | no |
104106
| vpc\_endpoint\_dynamodb\_gateway | Enable or disable VPC Endpoint for DynamoDB (Gateway) | `bool` | `true` | no |
105107
| vpc\_endpoint\_s3\_gateway | Enable or disable VPC Endpoint for S3 Gateway | `bool` | `true` | no |
106-
| vpc\_endpoint\_s3\_policy | A policy to attach to the endpoint that controls access to the service | `string` |<pre>{ "Statement": <br> [<br> {<br> "Action": <br> "\*\",<br> "Effect\": <br> "Allow\",<br> "Resource\":<br> "\*\",<br> "Principal\":<br> \"*\" <br> } <br> ] <br>}</pre> | no |
107-
| vpc\_endpoints | AWS services to create a VPC endpoint on private subnets for (e.g: ssm, ec2, ecr.dkr) |<pre>list(object(<br>{<br>name = string<br>policy = <br> optional(string)<br>allowed_cidrs =<br> optional(list<br> (string))<br>}<br>))</pre> | `[]` | no |
108+
| vpc\_endpoint\_s3\_policy | A policy to attach to the endpoint that controls access to the service | `string` | `" {
109+
\"Statement\": [
110+
{
111+
\"Action\": \"*\",\"Effect\": \"Allow\",\"Resource\": \"*\",\"Principal\": \"*\"
112+
}
113+
]
114+
}
115+
"` | no |
116+
| vpc\_endpoints | AWS services to create a VPC endpoint on private subnets for (e.g: ssm, ec2, ecr.dkr) | <pre>list(object(<br> {<br> name = string<br> policy = optional(string)<br> allowed_cidrs = optional(list(string))<br> }<br> ))</pre> | `[]` | no |
108117
| vpc\_flow\_logs | Enable or disable VPC Flow Logs | `bool` | `true` | no |
109118
| vpc\_flow\_logs\_retention | Retention in days for VPC Flow Logs CloudWatch Log Group | `number` | `365` | no |
110119

_variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ variable "transit_nacl_inbound_udp_ports" {
144144
description = "UDP Ports to allow inbound on transit subnet via NACLs (this list cannot be empty)"
145145
}
146146

147+
variable "secure_nacl_allow_public" {
148+
type = bool
149+
default = false
150+
description = "Allow traffic between public and secure"
151+
}
152+
147153
variable "vpc_flow_logs" {
148154
type = bool
149155
default = true
@@ -270,6 +276,12 @@ variable "create_dbsubgroup_private" {
270276
description = "Create Private Subgroup"
271277
}
272278

279+
variable "db_subnet_group_secure_name_compat" {
280+
type = bool
281+
default = false
282+
description = "Use previous DB subnet group name (<name>-dbsubnet) for backwards compability (secure only)"
283+
}
284+
273285
locals {
274286
kubernetes_clusters = zipmap(
275287
formatlist("kubernetes.io/cluster/%s", var.kubernetes_clusters),

cf-exports.tf

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ resource "aws_cloudformation_stack" "tf_exports" {
33
template_body = templatefile("${path.module}/cf-exports.yml", {
44
"name" = var.cf_export_name != "" ? var.cf_export_name : var.name
55
"vars" = {
6-
"VpcId" = aws_vpc.default.id,
7-
"CidrBlock" = aws_vpc.default.cidr_block,
8-
"InternetGatewayId" = aws_internet_gateway.default.id,
9-
"PublicSubnetIds" = join(",", aws_subnet.public.*.id),
10-
"PublicSubnetCidrs" = join(",", aws_subnet.public.*.cidr_block),
11-
"PrivateSubnetIds" = join(",", aws_subnet.private.*.id),
12-
"PrivateSubnetCidrs" = join(",", aws_subnet.private.*.cidr_block),
13-
"SecureSubnetIds" = join(",", aws_subnet.secure.*.id),
14-
"SecureSubnetCidrs" = join(",", aws_subnet.secure.*.cidr_block),
15-
"NatGatewayIds" = var.nat ? join(",", aws_nat_gateway.nat_gw.*.id) : "undefined",
16-
"DbSubnetGroupId" = aws_db_subnet_group.secure[0].id,
17-
"DbSubnetPrivateGroupId" = try(aws_db_subnet_group.private[0].id,"")
18-
"DbSubnetPublicGroupId" = try(aws_db_subnet_group.public[0].id,"")
6+
"VpcId" = aws_vpc.default.id,
7+
"CidrBlock" = aws_vpc.default.cidr_block,
8+
"InternetGatewayId" = aws_internet_gateway.default.id,
9+
"PublicSubnetIds" = join(",", aws_subnet.public.*.id),
10+
"PublicSubnetCidrs" = join(",", aws_subnet.public.*.cidr_block),
11+
"PrivateSubnetIds" = join(",", aws_subnet.private.*.id),
12+
"PrivateSubnetCidrs" = join(",", aws_subnet.private.*.cidr_block),
13+
"SecureSubnetIds" = join(",", aws_subnet.secure.*.id),
14+
"SecureSubnetCidrs" = join(",", aws_subnet.secure.*.cidr_block),
15+
"NatGatewayIds" = var.nat ? join(",", aws_nat_gateway.nat_gw.*.id) : "undefined",
16+
"DbSubnetGroupId" = try(aws_db_subnet_group.secure[0].id, "undefined")
17+
"DbSubnetPrivateGroupId" = try(aws_db_subnet_group.private[0].id, "undefined")
18+
"DbSubnetPublicGroupId" = try(aws_db_subnet_group.public[0].id, "undefined")
1919
}
2020
})
21-
}
21+
}

db-subnet.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
resource "aws_db_subnet_group" "secure" {
22
count = var.create_dbsubgroup_secure ? 1 : 0
3-
name = lower("${format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)}-secure")
3+
name = var.db_subnet_group_secure_name_compat ? lower("${var.name}-dbsubnet") : lower("${format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)}-secure")
44
subnet_ids = aws_subnet.secure.*.id
55

66
tags = merge(

nacl-public.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,15 @@ resource "aws_network_acl_rule" "in_public_from_private" {
167167
from_port = 0
168168
to_port = 0
169169
}
170+
171+
resource "aws_network_acl_rule" "in_public_from_secure" {
172+
count = var.secure_nacl_allow_public ? var.vpc_cidr_summ != "/0" ? 1 : length(aws_subnet.secure[*].cidr_block) : 0
173+
network_acl_id = aws_network_acl.public.id
174+
rule_number = count.index + 701
175+
egress = false
176+
protocol = -1
177+
rule_action = "allow"
178+
cidr_block = var.vpc_cidr_summ != "/0" ? local.secure_subnet_summary : aws_subnet.secure[count.index].cidr_block
179+
from_port = 0
180+
to_port = 0
181+
}

nacl-secure.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ resource "aws_network_acl_rule" "out_secure_to_transit" {
7676
cidr_block = var.vpc_cidr_summ != "/0" ? local.transit_subnet_summary : aws_subnet.transit[count.index].cidr_block
7777
}
7878

79+
resource "aws_network_acl_rule" "in_secure_from_public" {
80+
count = var.secure_nacl_allow_public ? var.vpc_cidr_summ != "/0" ? 1 : length(aws_subnet.public[*].cidr_block) : 0
81+
network_acl_id = aws_network_acl.secure.id
82+
rule_number = count.index + 301
83+
egress = false
84+
protocol = -1
85+
rule_action = "allow"
86+
cidr_block = var.vpc_cidr_summ != "/0" ? local.public_subnet_summary : aws_subnet.public[count.index].cidr_block
87+
}
88+
89+
resource "aws_network_acl_rule" "out_secure_to_public" {
90+
count = var.secure_nacl_allow_public ? var.vpc_cidr_summ != "/0" ? 1 : length(aws_subnet.public[*].cidr_block) : 0
91+
network_acl_id = aws_network_acl.secure.id
92+
rule_number = count.index + 301
93+
egress = true
94+
protocol = -1
95+
rule_action = "allow"
96+
cidr_block = var.vpc_cidr_summ != "/0" ? local.public_subnet_summary : aws_subnet.public[count.index].cidr_block
97+
}
98+
7999
#############
80100
# S3 Endpoint
81101
#############

0 commit comments

Comments
 (0)