74 lines
2.1 KiB
Terraform
74 lines
2.1 KiB
Terraform
|
|
# Azure Networking Module
|
||
|
|
# Main resources
|
||
|
|
|
||
|
|
terraform {
|
||
|
|
required_providers {
|
||
|
|
azurerm = {
|
||
|
|
source = "hashicorp/azurerm"
|
||
|
|
version = "~> 3.0"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Virtual Network
|
||
|
|
resource "azurerm_virtual_network" "main" {
|
||
|
|
name = var.vnet_name
|
||
|
|
address_space = var.address_space
|
||
|
|
location = var.location
|
||
|
|
resource_group_name = var.resource_group_name
|
||
|
|
tags = var.tags
|
||
|
|
|
||
|
|
lifecycle {
|
||
|
|
create_before_destroy = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Subnets
|
||
|
|
resource "azurerm_subnet" "subnets" {
|
||
|
|
for_each = var.subnets
|
||
|
|
|
||
|
|
name = each.value.name
|
||
|
|
resource_group_name = var.resource_group_name
|
||
|
|
virtual_network_name = azurerm_virtual_network.main.name
|
||
|
|
address_prefixes = each.value.address_prefixes
|
||
|
|
service_endpoints = each.value.service_endpoints
|
||
|
|
|
||
|
|
lifecycle {
|
||
|
|
create_before_destroy = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Network Security Groups
|
||
|
|
resource "azurerm_network_security_group" "nsgs" {
|
||
|
|
for_each = var.network_security_groups
|
||
|
|
|
||
|
|
name = each.value.name
|
||
|
|
location = var.location
|
||
|
|
resource_group_name = var.resource_group_name
|
||
|
|
tags = var.tags
|
||
|
|
|
||
|
|
dynamic "security_rule" {
|
||
|
|
for_each = each.value.security_rules
|
||
|
|
content {
|
||
|
|
name = security_rule.value.name
|
||
|
|
priority = security_rule.value.priority
|
||
|
|
direction = security_rule.value.direction
|
||
|
|
access = security_rule.value.access
|
||
|
|
protocol = security_rule.value.protocol
|
||
|
|
source_port_range = security_rule.value.source_port_range
|
||
|
|
destination_port_range = security_rule.value.destination_port_range
|
||
|
|
source_address_prefix = security_rule.value.source_address_prefix
|
||
|
|
destination_address_prefix = security_rule.value.destination_address_prefix
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Associate NSGs with subnets
|
||
|
|
resource "azurerm_subnet_network_security_group_association" "nsg_associations" {
|
||
|
|
for_each = var.network_security_groups
|
||
|
|
|
||
|
|
subnet_id = azurerm_subnet.subnets[each.value.subnet_key].id
|
||
|
|
network_security_group_id = azurerm_network_security_group.nsgs[each.key].id
|
||
|
|
}
|
||
|
|
|