Skip to content

Commit 0c7fcc5

Browse files
committed
feat: adding the new shared subnet module
Added a new way of sharing subnets, curving out zones within a shared network
1 parent 7538271 commit 0c7fcc5

File tree

17 files changed

+500
-231
lines changed

17 files changed

+500
-231
lines changed

README.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module "vpc" {
3535
transit_gateway_id = data.aws_ec2_transit_gateway.this.id
3636
vpc_cidr = var.vpc_cidr
3737
38-
transit_gateway_rotues = {
38+
transit_gateway_routes = {
3939
private = aws_ec2_managed_prefix_list.internal.id
4040
}
4141
}
@@ -230,63 +230,73 @@ Remember to:
230230
1. Ensure CIDR blocks don't overlap
231231
2. Consider your IP address space requirements
232232
3. Follow your organization's IP addressing scheme
233-
4. Update route tables and network ACLs accordingly
234233

235234
The module include a convenient way to share subnets using AWS Resource Access Manager (RAM). Here is an example configuration:
236235

237236
```hcl
238-
## Alternatively you specify the subnets directly
237+
## Provision a network is no subnets inside
239238
module "vpc" {
240239
source = "../.."
241240
242241
availability_zones = 3
243242
name = "development"
244243
tags = local.tags
245244
vpc_cidr = "10.90.0.0/16"
245+
}
246+
247+
## Curve out subnets for sharing
248+
module "subnets" {
249+
source = "../../modules/shared"
250+
251+
name = "product-a"
252+
share = { accounts = ["123456789012"] }
253+
tags = local.tags
254+
vpc_id = module.vpc.vpc_id
255+
256+
## Additional subnet to add to the isolation zone
257+
permitted_subnets = [
258+
"10.90.20.0/24",
259+
]
246260
247261
subnets = {
248-
prod = {
249-
netmask = 24
262+
web = {
263+
cidrs = ["10.90.0.0/24", "10.90.1.0/24"]
250264
}
251-
"dev" = {
252-
netmask = 24
265+
app = {
266+
cidrs = ["10.90.10.0/24", "10.90.11.0/24"]
253267
}
254268
}
255269
}
270+
```
256271

257-
## Note, due to the arns being dynamic this will be need to perform with a target,
258-
## i.e vpc must exist before the share can be applied.
259-
module "share_dev" {
260-
source = "../../modules/shared"
272+
Note, this module will automatically create a network access control list (NACL) for the shared subnets, it will any
261273

262-
name = "dev"
263-
share = { accounts = ["123456789012"] }
264-
subnet_arns = module.vpc.all_subnets_by_name["dev"].arns
265-
tags = local.tags
274+
1. Permit all outbound and inbound traffic from the subnets
275+
2. Permit all outbound and inbound traffic from the `var.permitted_subnets` variable cidr_blocks.
276+
3. Deny all other traffic to the VPC CIDR block.
277+
4. Permit all outbound and inbound traffic not destined to the VPC CIDR block.
266278

267-
depends_on = [module.vpc]
268-
}
269-
```
279+
By performing the above to ensure the subnets are isolated from the rest of the VPC, while still allowing access to external resources.
270280

271-
## Network Access Control Lists (NACLS)
281+
## Network Access Control Lists (NACLs)
272282

273283
Network Access Control Lists (NACLs) are an optional layer of security for your VPC that acts as a firewall for controlling traffic in and out of one or more subnets. Unlike security groups, NACLs are stateless, meaning that responses to allowed inbound traffic are subject to the rules for outbound traffic. NACLs allow you to explicitly allow or deny traffic based on IP address, port, and protocol. Here's an example of how to configure NACLs in this module:
274284

275285
```hcl
276286
module "vpc" {
277287
source = "../.."
278-
288+
279289
name = "production"
280290
vpc_cidr = "10.0.0.0/16"
281291
availability_zones = 3
282292
tags = local.tags
283-
293+
284294
subnets = {
285295
private = {
286296
netmask = 24
287297
}
288298
}
289-
299+
290300
nacl_rules = {
291301
private = {
292302
inbound_rules = [

examples/nacls/main.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ module "vpc" {
2020

2121
subnets = {
2222
private = {
23-
netmask = 24
23+
netmask = 24
24+
isolation_key = "private"
25+
}
26+
devops_apps = {
27+
netmask = 24
28+
isolation_key = "devops"
2429
}
2530
}
2631

examples/shared/README.md

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/shared/outputs.tf

Lines changed: 0 additions & 125 deletions
This file was deleted.

examples/sharing/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Providers
3+
4+
No providers.
5+
6+
## Inputs
7+
8+
No inputs.
9+
10+
## Outputs
11+
12+
| Name | Description |
13+
|------|-------------|
14+
| <a name="output_network_inbound_acls"></a> [network\_inbound\_acls](#output\_network\_inbound\_acls) | The inbound network ACLs provisioned |
15+
| <a name="output_network_outbound_acls"></a> [network\_outbound\_acls](#output\_network\_outbound\_acls) | The outbound network ACLs provisioned |
16+
<!-- END_TF_DOCS -->
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ locals {
55
"GitRepo" = "https://github.com/appvia/terraform-aws-network"
66
"Terraform" = "true"
77
}
8-
98
}
109

1110
## Alteratively you specifiy the subnets directly
@@ -16,26 +15,29 @@ module "vpc" {
1615
name = "development"
1716
tags = local.tags
1817
vpc_cidr = "10.90.0.0/16"
19-
20-
subnets = {
21-
prod = {
22-
netmask = 24
23-
}
24-
"dev" = {
25-
netmask = 24
26-
}
27-
}
2818
}
2919

3020
## Note, due to the arns being dynamic this will be need to perfomed with a target,
3121
## i.e vpc must exist before the share can be applied.
32-
module "share_dev" {
22+
module "subnets" {
3323
source = "../../modules/shared"
3424

35-
name = "dev"
36-
share = { accounts = ["123456789012"] }
37-
subnet_arns = module.vpc.all_subnets_by_name["dev"].arns
38-
tags = local.tags
25+
name = "dev"
26+
share = { accounts = ["123456789012"] }
27+
tags = local.tags
28+
vpc_id = module.vpc.vpc_id
3929

40-
depends_on = [module.vpc]
30+
permitted_subnets = [
31+
"10.90.20.0/24",
32+
]
33+
34+
subnets = {
35+
web = {
36+
cidrs = ["10.90.0.0/24", "10.90.1.0/24"]
37+
}
38+
app = {
39+
cidrs = ["10.90.10.0/24", "10.90.11.0/24"]
40+
}
41+
}
4142
}
43+

examples/sharing/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
output "network_inbound_acls" {
3+
description = "The inbound network ACLs provisioned"
4+
value = module.subnets.inbound_network_acls
5+
}
6+
7+
output "network_outbound_acls" {
8+
description = "The outbound network ACLs provisioned"
9+
value = module.subnets.outbound_network_acls
10+
}

locals.tf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ locals {
4242
}
4343
} : null
4444

45-
## A collection of all the tags for all the resources
45+
## A collection of all the tags for all the resources
4646
tags = merge(var.tags, {})
4747
# A map of all the subnets by name i.e. private/us-east-1a, public/us-east-1a, etc.
4848
all_subnets = merge(module.vpc.private_subnet_attributes_by_az, module.vpc.public_subnet_attributes_by_az)
4949
## A list of all the names of the subnets
5050
all_subnets_by_name = { for name in keys(try(var.subnets, {})) : name => {
51-
arns = [for k, v in local.all_subnets : format("arn:aws:ec2:%s:%s:subnet/%s", local.region, local.account_id, v.id) if startswith(k, "${name}/")]
52-
ids = [for k, v in local.all_subnets : v.id if startswith(k, "${name}/")]
51+
arns = [for k, v in local.all_subnets : format("arn:aws:ec2:%s:%s:subnet/%s", local.region, local.account_id, v.id) if startswith(k, "${name}/")]
52+
cidr_blocks = [for k, v in local.all_subnets : v.cidr_block if startswith(k, "${name}/")]
53+
ids = [for k, v in local.all_subnets : v.id if startswith(k, "${name}/")]
5354
} }
55+
5456
# A list of all the private subnets cidr blocks
5557
private_subnet_cidrs = [for k, x in module.vpc.private_subnet_attributes_by_az : x.cidr_block if startswith(k, "private/")]
5658
# A map of private subnet id to cidr block
@@ -78,6 +80,7 @@ locals {
7880
# A map of the route table ids for the transit gateway by az
7981
transit_route_table_by_az = local.enable_transit_gateway ? { for k, v in module.vpc.rt_attributes_by_type_by_az.transit_gateway : k => v.id } : {}
8082

83+
## A list of all the subnets
8184
subnets = merge(
8285
local.private_subnet,
8386
local.public_subnet,

0 commit comments

Comments
 (0)