forked from kumarvna/terraform-azurerm-nat-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
87 lines (79 loc) · 3.81 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#---------------------------------
# Local declarations
#---------------------------------
locals {
resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0)
location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0)
}
#---------------------------------------------------------
# Resource Group Creation or selection - Default is "true"
#----------------------------------------------------------
data "azurerm_resource_group" "rgrp" {
count = var.create_resource_group == false ? 1 : 0
name = var.resource_group_name
}
resource "azurerm_resource_group" "rg" {
count = var.create_resource_group ? 1 : 0
name = lower(var.resource_group_name)
location = var.location
tags = merge({ "ResourceName" = format("%s", var.resource_group_name) }, var.tags, )
}
#--------------------------------------------
# Public IP resources for Azure NAT Gateway
#--------------------------------------------
resource "azurerm_public_ip_prefix" "ng-pref" {
for_each = var.nat_gateway
name = lower("${each.key}-pip-prefix")
resource_group_name = local.resource_group_name
location = local.location
prefix_length = lookup(each.value, "public_ip_prefix_length", 30)
availability_zone = element(coalescelist(each.value["availability_zone"], [""]), 0)
tags = merge({ "ResourceName" = lower("${each.key}-pip-prefix") }, var.tags, )
}
resource "azurerm_public_ip" "ng-pip" {
for_each = var.nat_gateway
name = lower("${each.key}-nat-gateway-pip")
location = local.location
resource_group_name = local.resource_group_name
allocation_method = "Static"
sku = "Standard"
availability_zone = element(coalescelist(each.value["availability_zone"], [""]), 0)
tags = merge({ "ResourceName" = lower("${each.key}-nat-gateway-pip") }, var.tags, )
}
#--------------------------------------------
# Azure NAT Gateway configuration
#--------------------------------------------
resource "azurerm_nat_gateway" "main" {
for_each = var.nat_gateway
name = format("%s", each.key)
resource_group_name = local.resource_group_name
location = local.location
idle_timeout_in_minutes = lookup(each.value, "idle_timeout_in_minutes", 4)
sku_name = "Standard"
zones = each.value["availability_zone"]
tags = merge({ "ResourceName" = format("%s", each.key) }, var.tags, )
}
#-----------------------------------------------------
# Association between a Nat Gateway and a Public IP.
#-----------------------------------------------------
resource "azurerm_nat_gateway_public_ip_association" "main" {
for_each = var.nat_gateway
nat_gateway_id = azurerm_nat_gateway.main[each.key].id
public_ip_address_id = azurerm_public_ip.ng-pip[each.key].id
}
#-----------------------------------------------------------
# Association between a Nat Gateway and a Public IP Prefix.
#-----------------------------------------------------------
resource "azurerm_nat_gateway_public_ip_prefix_association" "main" {
for_each = var.nat_gateway
nat_gateway_id = azurerm_nat_gateway.main[each.key].id
public_ip_prefix_id = azurerm_public_ip_prefix.ng-pref[each.key].id
}
#-------------------------------------------------------------------
# Associates a NAT Gateway with a Subnet within a Virtual Network.
#-------------------------------------------------------------------
resource "azurerm_subnet_nat_gateway_association" "main" {
for_each = var.nat_gateway
nat_gateway_id = azurerm_nat_gateway.main[each.key].id
subnet_id = each.value["subnet_id"]
}