Skip to content

Commit 8bc8566

Browse files
authored
Merge pull request #1 from NicGorod/feat/networking
Feat: implement networking module
2 parents dcf7ccf + e5b65da commit 8bc8566

7 files changed

Lines changed: 166 additions & 30 deletions

File tree

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
*.tfstate
3+
*.tfstate.*
4+
5+
./terraform.tfstate
6+
.terraform/.terraform.lock*
7+

terraform.tfstate

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

terraform/.terraform.lock.hcl

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# VNet module
2+
3+
This module creates a VNet and subnets in Azure.
4+
It also creates a network security group and associates it with the subnets.
5+
6+
## Module usage
7+
8+
Note: keep in mind this module calculates VNet address space based on the subnets provided.
9+
10+
11+
```hcl
12+
module "vnet" {
13+
source = "./modules/network"
14+
name = "my-vnet"
15+
resource_group_id = azurerm_resource_group.example.id
16+
location = "canadaeast"
17+
subnets = {
18+
"subnet1" = {
19+
address_prefixes = ["10.0.1.0/24"]
20+
service_endpoints = ["Microsoft.Storage", "Microsoft.Sql"]
21+
}
22+
"subnet2" = {
23+
address_prefixes = ["10.0.2.0/24"]
24+
delegate = [
25+
{
26+
name = "delegation"
27+
service = "Microsoft.Web/serverFarms"
28+
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
29+
}
30+
]
31+
}
32+
"subnet3" = {
33+
address_prefixes = ["10.0.3.0/24"]
34+
private_endpoint_network_policies_enabled = false
35+
}
36+
}
37+
tags = {
38+
Environment = "Production"
39+
Project = "MyProject"
40+
}
41+
}
42+
```
43+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
3+
locals {
4+
resource_group_name = split("/", var.resource_group_id)[4]
5+
6+
# Calculate VNet address space from subnet prefixes
7+
# 1. Collects all subnet address prefixes into a flat list
8+
# 2. Calculates the smallest possible CIDR block that can contain all subnets
9+
# 3. Uses this calculated CIDR as the VNet's address space
10+
all_subnet_prefixes = flatten([
11+
for subnet in values(var.subnets) : subnet.address_prefixes
12+
])
13+
# Get the smallest network that contains all subnets
14+
vnet_cidr = [cidrhost(
15+
format("%s/%s",
16+
cidrhost(local.all_subnet_prefixes[0], 0),
17+
tonumber(split("/", local.all_subnet_prefixes[0])[1]) - 1
18+
),
19+
0
20+
)]
21+
22+
}
23+
24+
resource "azurerm_virtual_network" "vnet" {
25+
name = var.vnet_name
26+
location = var.location
27+
resource_group_name = local.resource_group_name
28+
address_space = local.vnet_cidr
29+
tags = var.tags
30+
31+
lifecycle {
32+
create_before_destroy = true
33+
}
34+
}
35+
36+
resource "azurerm_subnet" "subnets" {
37+
for_each = var.subnets
38+
39+
name = each.key
40+
resource_group_name = local.resource_group_name
41+
virtual_network_name = azurerm_virtual_network.vnet.name
42+
address_prefixes = each.value.address_prefixes
43+
service_endpoints = each.value.service_endpoints
44+
private_endpoint_network_policies_enabled = each.value.private_endpoint_network_policies_enabled
45+
private_link_service_network_policies_enabled = each.value.private_link_service_network_policies_enabled
46+
47+
dynamic "delegation" {
48+
for_each = each.value.delegate
49+
content {
50+
name = delegation.value.name
51+
service_delegation {
52+
name = delegation.value.service
53+
actions = delegation.value.actions
54+
}
55+
}
56+
}
57+
}
58+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
output "vnet_id" {
3+
description = "The ID of the Virtual Network"
4+
value = azurerm_virtual_network.vnet.id
5+
}
6+
7+
output "vnet_name" {
8+
description = "The name of the Virtual Network"
9+
value = azurerm_virtual_network.vnet.name
10+
}
11+
12+
output "vnet_address_space" {
13+
description = "The address space of the Virtual Network"
14+
value = azurerm_virtual_network.vnet.address_space
15+
}
16+
17+
output "subnet_ids" {
18+
description = "Map of subnet names and their IDs"
19+
value = {
20+
for subnet_name, subnet in azurerm_subnet.subnets : subnet_name => subnet.id
21+
}
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
variable "vnet_name" {
2+
description = "The name of VNet"
3+
type = string
4+
}
5+
6+
variable "resource_group_id" {
7+
description = "The ID of the resource group"
8+
type = string
9+
}
10+
11+
variable "location" {
12+
description = "The Azure region where the Virtual Network should be created"
13+
type = string
14+
}
15+
16+
variable "tags" {
17+
description = "A mapping of tags to assign to the Virtual Network"
18+
type = map(string)
19+
default = {}
20+
}
21+
22+
variable "subnets" {
23+
description = "Map of subnet configurations"
24+
type = map(object({
25+
address_prefixes = list(string)
26+
service_endpoints = optional(list(string), [])
27+
private_endpoint_network_policies_enabled = optional(bool, true)
28+
private_link_service_network_policies_enabled = optional(bool, true)
29+
delegate = optional(list(object({
30+
name = string
31+
service = string
32+
actions = list(string)
33+
})), [])
34+
}))
35+
}
36+

0 commit comments

Comments
 (0)