|
| 1 | +#--------------------------------- |
| 2 | +# Local declarations |
| 3 | +#--------------------------------- |
| 4 | +locals { |
| 5 | + resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0) |
| 6 | + location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0) |
| 7 | + nat_gateway_zones = { for zone in var.nat_gateway_zones : zone => true } |
| 8 | +} |
| 9 | + |
| 10 | +#--------------------------------------------------------- |
| 11 | +# Resource Group Creation or selection - Default is "true" |
| 12 | +#---------------------------------------------------------- |
| 13 | +data "azurerm_resource_group" "rgrp" { |
| 14 | + count = var.create_resource_group == false ? 1 : 0 |
| 15 | + name = var.resource_group_name |
| 16 | +} |
| 17 | + |
| 18 | +resource "azurerm_resource_group" "rg" { |
| 19 | + count = var.create_resource_group ? 1 : 0 |
| 20 | + name = lower(var.resource_group_name) |
| 21 | + location = var.location |
| 22 | + tags = merge({ "ResourceName" = format("%s", var.resource_group_name) }, var.tags, ) |
| 23 | +} |
| 24 | + |
| 25 | +data "azurerm_log_analytics_workspace" "logws" { |
| 26 | + count = var.log_analytics_workspace_name != null ? 1 : 0 |
| 27 | + name = var.log_analytics_workspace_name |
| 28 | + resource_group_name = local.resource_group_name |
| 29 | +} |
| 30 | + |
| 31 | +data "azurerm_storage_account" "storeacc" { |
| 32 | + count = var.storage_account_name != null ? 1 : 0 |
| 33 | + name = var.storage_account_name |
| 34 | + resource_group_name = local.resource_group_name |
| 35 | +} |
| 36 | + |
| 37 | +#-------------------------------------------- |
| 38 | +# Public IP resources for Azure NAT Gateway |
| 39 | +#-------------------------------------------- |
| 40 | +resource "azurerm_public_ip_prefix" "ng-pref" { |
| 41 | + for_each = var.nat_gateway |
| 42 | + name = lower("${each.key}-pip-prefix") |
| 43 | + resource_group_name = local.resource_group_name |
| 44 | + location = local.location |
| 45 | + prefix_length = lookup(each.value, "public_ip_prefix_length", 30) |
| 46 | + availability_zone = element(coalescelist(each.value["availability_zone"], [""]), 0) |
| 47 | + tags = merge({ "ResourceName" = lower("${each.key}-pip-prefix") }, var.tags, ) |
| 48 | +} |
| 49 | + |
| 50 | +resource "azurerm_public_ip" "ng-pip" { |
| 51 | + for_each = var.nat_gateway |
| 52 | + name = lower("${each.key}-nat-gateway-pip") |
| 53 | + location = local.location |
| 54 | + resource_group_name = local.resource_group_name |
| 55 | + allocation_method = "Static" |
| 56 | + sku = "Standard" |
| 57 | + availability_zone = element(coalescelist(each.value["availability_zone"], [""]), 0) |
| 58 | + tags = merge({ "ResourceName" = lower("${each.key}-nat-gateway-pip") }, var.tags, ) |
| 59 | +} |
| 60 | + |
| 61 | +#-------------------------------------------- |
| 62 | +# Azure NAT Gateway configuration |
| 63 | +#-------------------------------------------- |
| 64 | +resource "azurerm_nat_gateway" "main" { |
| 65 | + for_each = var.nat_gateway |
| 66 | + name = format("%s", each.key) |
| 67 | + resource_group_name = local.resource_group_name |
| 68 | + location = local.location |
| 69 | + idle_timeout_in_minutes = lookup(each.value, "idle_timeout_in_minutes", 4) |
| 70 | + sku_name = "Standard" |
| 71 | + zones = each.value["availability_zone"] |
| 72 | + tags = merge({ "ResourceName" = format("%s", each.key) }, var.tags, ) |
| 73 | +} |
| 74 | + |
| 75 | +#----------------------------------------------------- |
| 76 | +# Association between a Nat Gateway and a Public IP. |
| 77 | +#----------------------------------------------------- |
| 78 | +resource "azurerm_nat_gateway_public_ip_association" "main" { |
| 79 | + for_each = var.nat_gateway |
| 80 | + nat_gateway_id = azurerm_nat_gateway.main[each.key].id |
| 81 | + public_ip_address_id = azurerm_public_ip.ng-pip[each.key].id |
| 82 | +} |
| 83 | + |
| 84 | +#----------------------------------------------------------- |
| 85 | +# Association between a Nat Gateway and a Public IP Prefix. |
| 86 | +#----------------------------------------------------------- |
| 87 | +resource "azurerm_nat_gateway_public_ip_prefix_association" "main" { |
| 88 | + for_each = var.nat_gateway |
| 89 | + nat_gateway_id = azurerm_nat_gateway.main[each.key].id |
| 90 | + public_ip_prefix_id = azurerm_public_ip_prefix.ng-pref[each.key].id |
| 91 | +} |
| 92 | + |
| 93 | +#----------------------------------------------------------- |
| 94 | +# Association between a Nat Gateway and a Public IP Prefix. |
| 95 | +#----------------------------------------------------------- |
| 96 | +resource "azurerm_subnet_nat_gateway_association" "main" { |
| 97 | + for_each = var.nat_gateway |
| 98 | + nat_gateway_id = azurerm_nat_gateway.main[each.key].id |
| 99 | + subnet_id = each.value |
| 100 | +} |
0 commit comments