Skip to content

Commit 823c567

Browse files
committed
natgateway configuraiton
1 parent c3e5629 commit 823c567

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# .tfstate files
55
*.tfstate
66
*.tfstate.*
7+
*.terraform.lock.hcl
78

89
# Crash log files
910
crash.log

examples/complete/main.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Azurerm Provider configuration
2+
provider "azurerm" {
3+
features {}
4+
}
5+
6+
module "nat-gateway" {
7+
// source = "kumarvna/nat-gateway/azurerm"
8+
// version = "1.0.0"
9+
source = "../../"
10+
11+
# By default, this module will not create a resource group. Location will be same as existing RG.
12+
# proivde a name to use an existing resource group, specify the existing resource group name,
13+
# set the argument to `create_resource_group = true` to create new resrouce group.
14+
# # The Subnet must have the name `AzureFirewallSubnet` and the subnet mask must be at least a /26
15+
resource_group_name = "rg-shared-westeurope-01"
16+
location = "westeurope"
17+
18+
nat_gateway = {
19+
testnatgateway1 = {
20+
availability_zone = ["1"]
21+
public_ip_prefix_length = 30
22+
idle_timeout_in_minutes = 10
23+
subnet_id = [
24+
"/subscriptions/1e3f0eeb-2235-44cd-b3a3-dcded0861d06/resourceGroups/rg-shared-westeurope-01/providers/Microsoft.Network/virtualNetworks/vnet-shared-hub-westeurope-001/subnets/snet-management",
25+
"/subscriptions/1e3f0eeb-2235-44cd-b3a3-dcded0861d06/resourceGroups/rg-shared-westeurope-01/providers/Microsoft.Network/virtualNetworks/vnet-shared-hub-westeurope-001/subnets/snet-testnetwork1"
26+
]
27+
},
28+
testnatgateway-zone2 = {
29+
availability_zone = ["2"]
30+
public_ip_prefix_length = 30
31+
idle_timeout_in_minutes = 10
32+
subnet_id = ["/subscriptions/1e3f0eeb-2235-44cd-b3a3-dcded0861d06/resourceGroups/rg-shared-westeurope-01/providers/Microsoft.Network/virtualNetworks/vnet-shared-hub-westeurope-001/subnets/snet-appgateway"]
33+
}
34+
}
35+
36+
# (Optional) To enable Azure Monitoring for Azure MySQL database
37+
# (Optional) Specify `storage_account_name` to save monitoring logs to storage.
38+
#log_analytics_workspace_name = "loganalytics-we-sharedtest2"
39+
40+
# Adding TAG's to your Azure resources
41+
tags = {
42+
ProjectName = "demo-internal"
43+
Env = "dev"
44+
Owner = "user@example.com"
45+
BusinessUnit = "CORP"
46+
ServiceClass = "Gold"
47+
}
48+
}

main.tf

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

output.tf

Whitespace-only changes.

variables.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
variable "create_resource_group" {
2+
description = "Whether to create resource group and use it for all networking resources"
3+
default = false
4+
}
5+
6+
variable "resource_group_name" {
7+
description = "A container that holds related resources for an Azure solution"
8+
default = ""
9+
}
10+
11+
variable "location" {
12+
description = "The location/region to keep all your network resources. To get the list of all locations with table format from azure cli, run 'az account list-locations -o table'"
13+
default = ""
14+
}
15+
16+
variable "nat_gateway_zones" {
17+
description = "Public ips is a list of ip names that are connected to the firewall. At least one is required."
18+
type = list(string)
19+
default = [1]
20+
}
21+
22+
variable "public_ip_prefix_length" {
23+
description = "Specifies the number of bits of the prefix. The value can be set between 0 (4,294,967,296 addresses) and 31 (2 addresses)."
24+
default = 30
25+
}
26+
27+
variable "nat_gateway_name" {
28+
description = "Specifies the name of the NAT Gateway."
29+
default = ""
30+
}
31+
32+
variable "log_analytics_workspace_name" {
33+
description = "The name of log analytics workspace name"
34+
default = null
35+
}
36+
37+
variable "storage_account_name" {
38+
description = "The name of the hub storage account to store logs"
39+
default = null
40+
}
41+
42+
variable "nat_gateway" {
43+
description = "value"
44+
type = map(object({
45+
public_ip_prefix_length = number
46+
availability_zone = optional(list(string))
47+
idle_timeout_in_minutes = optional(number)
48+
subnet_id = optional(list(string))
49+
}))
50+
}
51+
52+
variable "tags" {
53+
description = "A map of tags to add to all resources"
54+
type = map(string)
55+
default = {}
56+
}

versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
experiments = [module_variable_optional_attrs]
3+
required_providers {
4+
azurerm = {
5+
source = "hashicorp/azurerm"
6+
version = ">= 2.59.0"
7+
}
8+
}
9+
required_version = ">= 0.13"
10+
}

0 commit comments

Comments
 (0)