Skip to content

Commit 6f250ce

Browse files
Add NAT Gateway for agents (#13)
* Add NAT Gateway for agents * Fix linting * Fix typos * Add missing count
1 parent 8a21970 commit 6f250ce

7 files changed

Lines changed: 49 additions & 6 deletions

File tree

alz/azuredevops/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ module "azure" {
4949
virtual_network_subnet_address_prefix_container_instances = var.virtual_network_subnet_address_prefix_container_instances
5050
virtual_network_subnet_address_prefix_storage = var.virtual_network_subnet_address_prefix_storage
5151
storage_account_replication_type = var.storage_account_replication_type
52+
public_ip_name = local.resource_names.public_ip
53+
nat_gateway_name = local.resource_names.nat_gateway
5254
}
5355

5456
module "azure_devops" {

alz/azuredevops/terraform.tfvars

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ resource_names = {
3333
version_control_system_agent_pool_apply = "{{service_name}}-{{environment_name}}-apply"
3434
version_control_system_group = "{{service_name}}-{{environment_name}}-approvers"
3535
virtual_network = "vnet-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}"
36+
public_ip = "pip-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}"
37+
nat_gateway = "nat-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}"
3638
subnet_container_instances = "subnet-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}-aci"
3739
subnet_storage = "subnet-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}-sto"
3840
private_endpoint = "pe-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}"

alz/github/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ module "azure" {
5050
virtual_network_subnet_address_prefix_container_instances = var.virtual_network_subnet_address_prefix_container_instances
5151
virtual_network_subnet_address_prefix_storage = var.virtual_network_subnet_address_prefix_storage
5252
storage_account_replication_type = var.storage_account_replication_type
53+
public_ip_name = local.resource_names.public_ip
54+
nat_gateway_name = local.resource_names.nat_gateway
5355
}
5456

5557
module "github" {

alz/github/terraform.tfvars

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ resource_names = {
2323
version_control_system_team = "{{service_name}}-{{environment_name}}-approvers"
2424
version_control_system_runner_group = "{{service_name}}-{{environment_name}}"
2525
virtual_network = "vnet-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}"
26+
public_ip = "pip-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}"
27+
nat_gateway = "nat-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}"
2628
subnet_container_instances = "subnet-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}-aci"
2729
subnet_storage = "subnet-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}-sto"
2830
private_endpoint = "pe-{{service_name}}-{{environment_name}}-{{azure_location}}-{{postfix_number}}"

modules/azure/container_instances.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,4 @@ resource "azurerm_container_group" "alz" {
4242
(var.agent_token_environment_variable) = var.agent_token
4343
}
4444
}
45-
46-
lifecycle {
47-
ignore_changes = [
48-
container[0].secure_environment_variables
49-
]
50-
}
5145
}

modules/azure/networking.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ resource "azurerm_virtual_network" "alz" {
66
address_space = [var.virtual_network_address_space]
77
}
88

9+
resource "azurerm_public_ip" "alz" {
10+
count = local.use_private_networking ? 1 : 0
11+
name = var.public_ip_name
12+
location = var.azure_location
13+
resource_group_name = azurerm_resource_group.network[0].name
14+
allocation_method = "Static"
15+
sku = "Standard"
16+
}
17+
18+
resource "azurerm_nat_gateway" "alz" {
19+
count = local.use_private_networking ? 1 : 0
20+
name = var.nat_gateway_name
21+
location = var.azure_location
22+
resource_group_name = azurerm_resource_group.network[0].name
23+
sku_name = "Standard"
24+
}
25+
26+
resource "azurerm_nat_gateway_public_ip_association" "alz" {
27+
count = local.use_private_networking ? 1 : 0
28+
nat_gateway_id = azurerm_nat_gateway.alz[0].id
29+
public_ip_address_id = azurerm_public_ip.alz[0].id
30+
}
31+
932
resource "azurerm_subnet" "container_instances" {
1033
count = local.use_private_networking ? 1 : 0
1134
name = var.virtual_network_subnet_name_container_instances
@@ -22,6 +45,12 @@ resource "azurerm_subnet" "container_instances" {
2245
}
2346
}
2447

48+
resource "azurerm_subnet_nat_gateway_association" "container_instances" {
49+
count = local.use_private_networking ? 1 : 0
50+
subnet_id = azurerm_subnet.container_instances[0].id
51+
nat_gateway_id = azurerm_nat_gateway.alz[0].id
52+
}
53+
2554
resource "azurerm_subnet" "storage" {
2655
count = local.use_private_networking ? 1 : 0
2756
name = var.virtual_network_subnet_name_storage

modules/azure/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ variable "virtual_network_address_space" {
202202
default = "10.0.0.0/24"
203203
}
204204

205+
variable "public_ip_name" {
206+
type = string
207+
description = "The name of the public ip"
208+
default = ""
209+
}
210+
211+
variable "nat_gateway_name" {
212+
type = string
213+
description = "The name of the nat gateway"
214+
default = ""
215+
}
216+
205217
variable "virtual_network_subnet_name_container_instances" {
206218
type = string
207219
description = "Name of the virtual network subnet"

0 commit comments

Comments
 (0)