Skip to content

Commit 3acd89e

Browse files
sbaidachniCopilot
andauthored
CAF Module and bug fixes (#250)
* rg name * switch to array * storage account test * deployment script storage * trying 0.7.1 * empty sufix * fix rg empty condition * fix space * change trim to length * another test * resource share user * fix conditions for rg * apply better naming * search servie caf * fix ai naming and add empty string feature * fix name for open ai * fix locals issue * it's a holiday today * validate empty string * virtual network and nsg * deployment script net * private endpoint subnets * fix typo * shorter names * gateways and ips * fix key names * Update infra/main.tf Co-authored-by: Copilot <[email protected]> * Update infra/variables.tf Co-authored-by: Copilot <[email protected]> * terraform formatting * more formatting * moving naming into a separate file --------- Co-authored-by: Copilot <[email protected]>
1 parent 7454e7f commit 3acd89e

File tree

9 files changed

+171
-56
lines changed

9 files changed

+171
-56
lines changed

infra/main.ai.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ module "azure_open_ai" {
33
# checkov:skip=CKV_AZURE_236: The Power Platform AI Search connector only supports service principal, API key, or interactive auth.
44
# checkov:skip=CKV_TF_1: Using published module version for maintainability. See decision-log/001-avm-usage-and-version.md for details.
55
source = "Azure/avm-res-cognitiveservices-account/azurerm"
6-
version = "0.8.0"
6+
version = "0.7.1"
77
kind = "OpenAI"
88
location = var.location
9-
name = "aoai${random_string.name.id}"
9+
name = azurecaf_name.main_names.results["azurerm_cognitive_account"]
1010
resource_group_name = local.resource_group_name
1111
enable_telemetry = true
1212
sku_name = "S0"
1313
local_auth_enabled = true
1414
cognitive_deployments = var.cognitive_deployments
1515
public_network_access_enabled = false
1616
outbound_network_access_restricted = true
17-
fqdns = ["aoai${random_string.name.id}.openai.azure.com"]
17+
fqdns = ["${azurecaf_name.main_names.results["azurerm_cognitive_account"]}.openai.azure.com"]
1818

1919
network_acls = {
2020
default_action = "Deny"
@@ -31,7 +31,7 @@ module "azure_open_ai" {
3131

3232
private_endpoints = {
3333
pe_endpoint = {
34-
name = "pe_endpoint_${random_string.name.id}"
34+
name = "pe-${azurecaf_name.main_names.results["azurerm_cognitive_account"]}"
3535
private_service_connection_name = "pe_endpoint_connection"
3636
subnet_resource_id = local.pe_primary_subnet_id
3737
}

infra/main.naming.tf

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Unique naming for Azure resources using Azure CAF naming conventions
2+
3+
locals {
4+
# Organization suffixes and prefixes are optional, and we need to form an array of non-empty values only
5+
org_prefix = compact([var.org_naming.org_prefix])
6+
org_suffix = compact([var.org_naming.org_environment, var.org_naming.org_suffix])
7+
}
8+
9+
# Generate unique names for primary resources
10+
resource "azurecaf_name" "main_names" {
11+
name = var.org_naming.workload_name
12+
resource_types = [
13+
"azurerm_resource_group",
14+
"azurerm_storage_account",
15+
"azurerm_search_service",
16+
"azurerm_cognitive_account",
17+
"azurerm_virtual_network",
18+
"azurerm_network_security_group",
19+
"azurerm_virtual_network_gateway",
20+
"azurerm_public_ip"
21+
]
22+
prefixes = local.org_prefix
23+
suffixes = local.org_suffix
24+
random_length = 4
25+
# use_slug = false
26+
clean_input = true
27+
}
28+
29+
# Generate unique names for failover resources
30+
resource "azurecaf_name" "failover_names" {
31+
name = var.org_naming.workload_name
32+
resource_types = [
33+
"azurerm_virtual_network",
34+
"azurerm_network_security_group",
35+
"azurerm_virtual_network_gateway",
36+
"azurerm_public_ip"
37+
]
38+
prefixes = local.org_prefix
39+
suffixes = concat(local.org_suffix, ["failover"])
40+
random_length = 4
41+
# use_slug = false
42+
clean_input = true
43+
}
44+
45+
# Generate unique names for primary private endpoint subnet
46+
resource "azurecaf_name" "main_pe_subnet_names" {
47+
name = var.org_naming.workload_name
48+
resource_types = [
49+
"azurerm_subnet"
50+
]
51+
prefixes = concat(["pe"], local.org_prefix)
52+
suffixes = concat(local.org_suffix, ["primary"])
53+
random_length = 4
54+
# use_slug = false
55+
clean_input = true
56+
}
57+
58+
# Generate unique names for failover private endpoint subnet
59+
resource "azurecaf_name" "failover_pe_subnet_names" {
60+
name = var.org_naming.workload_name
61+
resource_types = [
62+
"azurerm_subnet"
63+
]
64+
prefixes = concat(["pe"], local.org_prefix)
65+
suffixes = concat(local.org_suffix, ["failover"])
66+
random_length = 4
67+
# use_slug = false
68+
clean_input = true
69+
}
70+
71+
# Generate unique names for Azure Deployment Script related resources
72+
resource "azurecaf_name" "deployment_script_names" {
73+
name = var.org_naming.workload_name
74+
resource_types = [
75+
"azurerm_storage_account",
76+
"azurerm_network_security_group",
77+
"azurerm_subnet",
78+
"azurerm_user_assigned_identity"
79+
]
80+
prefixes = local.org_prefix
81+
suffixes = concat(local.org_suffix, ["script"])
82+
random_length = 4
83+
# use_slug = false
84+
clean_input = true
85+
}

infra/main.network.tf

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ data "azurerm_resources" "vnets" {
5757
resource "azurerm_virtual_network" "primary_virtual_network" {
5858
count = local.create_network_infrastructure ? 0 : 1
5959

60-
name = "power-platform-primary-vnet-${random_string.name.id}"
60+
name = azurecaf_name.main_names.results["azurerm_virtual_network"]
6161
resource_group_name = local.resource_group_name
6262
location = var.primary_location
6363
address_space = var.primary_vnet_address_spaces
@@ -67,7 +67,7 @@ resource "azurerm_virtual_network" "primary_virtual_network" {
6767
resource "azurerm_virtual_network" "failover_virtual_network" {
6868
count = local.create_network_infrastructure ? 0 : 1
6969

70-
name = "power-platform-failover-vnet-${random_string.name.id}"
70+
name = azurecaf_name.failover_names.results["azurerm_virtual_network"]
7171
resource_group_name = local.resource_group_name
7272
location = var.failover_location
7373
address_space = var.failover_vnet_address_spaces
@@ -98,7 +98,7 @@ resource "azurerm_subnet_nat_gateway_association" "primary_subnet_nat" {
9898
count = local.create_network_infrastructure ? 0 : 1
9999

100100
subnet_id = azurerm_subnet.primary_subnet[0].id
101-
nat_gateway_id = azurerm_nat_gateway.nat_gateways["primary"].id
101+
nat_gateway_id = azurerm_nat_gateway.primary_nat_gateway[0].id
102102
}
103103

104104
# Create failover subnets as first-class resources
@@ -125,15 +125,15 @@ resource "azurerm_subnet_nat_gateway_association" "failover_subnet_nat" {
125125
count = local.create_network_infrastructure ? 0 : 1
126126

127127
subnet_id = azurerm_subnet.failover_subnet[0].id
128-
nat_gateway_id = azurerm_nat_gateway.nat_gateways["failover"].id
128+
nat_gateway_id = azurerm_nat_gateway.failover_nat_gateway[0].id
129129
}
130130

131131
# Create dedicated private endpoint subnets without delegations
132132
resource "azurerm_subnet" "pe_primary_subnet" {
133133
count = local.create_network_infrastructure ? 0 : 1
134134

135135
# checkov:skip=CKV2_AZURE_31:"Ensure VNET subnet is configured with a Network Security Group (NSG)
136-
name = "pe-primary-subnet"
136+
name = azurecaf_name.main_pe_subnet_names.results["azurerm_subnet"]
137137
resource_group_name = local.resource_group_name
138138
virtual_network_name = azurerm_virtual_network.primary_virtual_network[0].name
139139
address_prefixes = var.primary_pe_subnet_address_spaces
@@ -147,7 +147,7 @@ resource "azurerm_subnet" "pe_failover_subnet" {
147147
# checkov:skip=CKV2_AZURE_31:"Ensure VNET subnet is configured with a Network Security Group (NSG)
148148
count = local.create_network_infrastructure ? 0 : 1
149149

150-
name = "pe-failover-subnet"
150+
name = azurecaf_name.failover_pe_subnet_names.results["azurerm_subnet"]
151151
resource_group_name = local.resource_group_name
152152
virtual_network_name = azurerm_virtual_network.failover_virtual_network[0].name
153153
address_prefixes = var.failover_pe_subnet_address_spaces
@@ -183,7 +183,7 @@ resource "azurerm_subnet_nat_gateway_association" "github_runner_primary_subnet_
183183
count = var.deploy_github_runner && local.create_network_infrastructure == false ? 1 : 0
184184

185185
subnet_id = azurerm_subnet.github_runner_primary_subnet[0].id
186-
nat_gateway_id = azurerm_nat_gateway.nat_gateways["primary"].id
186+
nat_gateway_id = azurerm_nat_gateway.primary_nat_gateway[0].id
187187
}
188188

189189
resource "azurerm_subnet" "github_runner_failover_subnet" {
@@ -210,55 +210,71 @@ resource "azurerm_subnet_nat_gateway_association" "github_runner_failover_subnet
210210
count = var.deploy_github_runner && local.create_network_infrastructure == false ? 1 : 0
211211

212212
subnet_id = azurerm_subnet.github_runner_failover_subnet[0].id
213-
nat_gateway_id = azurerm_nat_gateway.nat_gateways["failover"].id
213+
nat_gateway_id = azurerm_nat_gateway.failover_nat_gateway[0].id
214214
}
215215

216216
# Create public IP addresses for NAT gateways
217-
resource "azurerm_public_ip" "nat_gateway_ips" {
218-
for_each = local.create_network_infrastructure ? {} : {
219-
primary = var.primary_location
220-
failover = var.failover_location
221-
}
217+
resource "azurerm_public_ip" "primary_nat_gateway_ip" {
218+
count = local.create_network_infrastructure ? 0 : 1
219+
name = azurecaf_name.main_names.results["azurerm_public_ip"]
220+
location = var.primary_location
221+
resource_group_name = local.resource_group_name
222+
allocation_method = "Static"
223+
sku = "Standard"
224+
tags = var.tags
225+
}
222226

223-
name = "${each.key}-nat-gateway-ip"
224-
location = each.value
227+
resource "azurerm_public_ip" "failover_nat_gateway_ip" {
228+
count = local.create_network_infrastructure ? 0 : 1
229+
name = azurecaf_name.failover_names.results["azurerm_public_ip"]
230+
location = var.failover_location
225231
resource_group_name = local.resource_group_name
226232
allocation_method = "Static"
227233
sku = "Standard"
228234
tags = var.tags
229235
}
230236

231-
resource "azurerm_nat_gateway" "nat_gateways" {
232-
for_each = local.create_network_infrastructure ? {} : {
233-
primary = var.primary_location
234-
failover = var.failover_location
235-
}
237+
resource "azurerm_nat_gateway" "primary_nat_gateway" {
238+
count = local.create_network_infrastructure ? 0 : 1
239+
location = var.primary_location
240+
name = azurecaf_name.main_names.results["azurerm_virtual_network_gateway"]
241+
resource_group_name = local.resource_group_name
242+
sku_name = "Standard"
243+
tags = var.tags
236244

237-
location = each.value
238-
name = "${each.key}-nat-gateway"
245+
# Associate the public IP address with the NAT gateway
246+
depends_on = [azurerm_public_ip.primary_nat_gateway_ip]
247+
}
248+
249+
resource "azurerm_nat_gateway" "failover_nat_gateway" {
250+
count = local.create_network_infrastructure ? 0 : 1
251+
location = var.failover_location
252+
name = azurecaf_name.failover_names.results["azurerm_virtual_network_gateway"]
239253
resource_group_name = local.resource_group_name
240254
sku_name = "Standard"
241255
tags = var.tags
242256

243257
# Associate the public IP address with the NAT gateway
244-
depends_on = [azurerm_public_ip.nat_gateway_ips]
258+
depends_on = [azurerm_public_ip.failover_nat_gateway_ip]
245259
}
246260

247261
# Associate public IP addresses with NAT gateways
248-
resource "azurerm_nat_gateway_public_ip_association" "nat_gateway_ip_associations" {
249-
for_each = local.create_network_infrastructure ? {} : {
250-
primary = var.primary_location
251-
failover = var.failover_location
252-
}
262+
resource "azurerm_nat_gateway_public_ip_association" "primary_nat_gateway_ip_association" {
263+
count = local.create_network_infrastructure ? 0 : 1
264+
nat_gateway_id = azurerm_nat_gateway.primary_nat_gateway[0].id
265+
public_ip_address_id = azurerm_public_ip.primary_nat_gateway_ip[0].id
266+
}
253267

254-
nat_gateway_id = azurerm_nat_gateway.nat_gateways[each.key].id
255-
public_ip_address_id = azurerm_public_ip.nat_gateway_ips[each.key].id
268+
resource "azurerm_nat_gateway_public_ip_association" "failover_nat_gateway_ip_association" {
269+
count = local.create_network_infrastructure ? 0 : 1
270+
nat_gateway_id = azurerm_nat_gateway.failover_nat_gateway[0].id
271+
public_ip_address_id = azurerm_public_ip.failover_nat_gateway_ip[0].id
256272
}
257273

258274
resource "azurerm_subnet" "deployment_script_container_subnet" {
259275
count = local.create_network_infrastructure ? 0 : 1
260276

261-
name = "deploymentscript-subnet"
277+
name = azurecaf_name.deployment_script_names.results["azurerm_subnet"]
262278
resource_group_name = local.resource_group_name
263279
virtual_network_name = azurerm_virtual_network.primary_virtual_network[0].name
264280
address_prefixes = var.deployment_script_subnet_address_spaces
@@ -278,7 +294,7 @@ resource "azurerm_subnet_nat_gateway_association" "deployment_script_nat" {
278294
count = local.create_network_infrastructure ? 0 : 1
279295

280296
subnet_id = azurerm_subnet.deployment_script_container_subnet[0].id
281-
nat_gateway_id = azurerm_nat_gateway.nat_gateways["primary"].id
297+
nat_gateway_id = azurerm_nat_gateway.primary_nat_gateway[0].id
282298
}
283299

284300
# ============================================================================
@@ -289,7 +305,7 @@ resource "azurerm_subnet_nat_gateway_association" "deployment_script_nat" {
289305
resource "azurerm_network_security_group" "power_platform_primary_nsg" {
290306
count = local.create_network_infrastructure ? 0 : 1
291307

292-
name = "power-platform-primary-nsg-${random_string.name.id}"
308+
name = azurecaf_name.main_names.results["azurerm_network_security_group"]
293309
location = var.primary_location
294310
resource_group_name = local.resource_group_name
295311
tags = var.tags
@@ -351,7 +367,7 @@ resource "azurerm_network_security_group" "power_platform_primary_nsg" {
351367
resource "azurerm_network_security_group" "power_platform_failover_nsg" {
352368
count = local.create_network_infrastructure ? 0 : 1
353369

354-
name = "power-platform-failover-nsg-${random_string.name.id}"
370+
name = azurecaf_name.failover_names.results["azurerm_network_security_group"]
355371
location = var.failover_location
356372
resource_group_name = local.resource_group_name
357373
tags = var.tags
@@ -413,7 +429,7 @@ resource "azurerm_network_security_group" "power_platform_failover_nsg" {
413429
resource "azurerm_network_security_group" "private_endpoint_primary_nsg" {
414430
count = local.create_network_infrastructure ? 0 : 1
415431

416-
name = "private-endpoint-primary-nsg-${random_string.name.id}"
432+
name = azurecaf_name.main_pe_subnet_names.results["azurerm_subnet"]
417433
location = var.primary_location
418434
resource_group_name = local.resource_group_name
419435
tags = var.tags
@@ -449,7 +465,7 @@ resource "azurerm_network_security_group" "private_endpoint_primary_nsg" {
449465
resource "azurerm_network_security_group" "private_endpoint_failover_nsg" {
450466
count = local.create_network_infrastructure ? 0 : 1
451467

452-
name = "private-endpoint-failover-nsg-${random_string.name.id}"
468+
name = azurecaf_name.failover_pe_subnet_names.results["azurerm_subnet"]
453469
location = var.failover_location
454470
resource_group_name = local.resource_group_name
455471
tags = var.tags
@@ -558,7 +574,7 @@ resource "azurerm_network_security_group" "github_runner_nsg" {
558574
resource "azurerm_network_security_group" "deployment_script_nsg" {
559575
count = local.create_network_infrastructure ? 0 : 1
560576

561-
name = "deployment-script-nsg-${random_string.name.id}"
577+
name = azurecaf_name.deployment_script_names.results["azurerm_network_security_group"]
562578
location = var.primary_location
563579
resource_group_name = local.resource_group_name
564580
tags = var.tags

infra/main.search.tf

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
locals {
2-
search_name = replace("ais${random_string.name.id}", "/[^a-z0-9-]/", "")
3-
}
4-
51
resource "azurerm_search_service" "ai_search" {
62
# checkov:skip=CKV_AZURE_209: Deploying with minimal infrastructure for evaluation. Update partition_count and replica_count for production scenarios.
73
# checkov:skip=CKV_AZURE_208: Deploying with minimal infrastructure for evaluation. Update partition_count and replica_count for production scenarios.
8-
name = local.search_name
4+
name = azurecaf_name.main_names.results["azurerm_search_service"]
95
location = var.primary_location
106
resource_group_name = local.resource_group_name
117
sku = var.ai_search_config.sku
@@ -29,14 +25,14 @@ resource "azurerm_search_service" "ai_search" {
2925
# Primary region private endpoint
3026
resource "azurerm_private_endpoint" "primary_endpoint" {
3127
location = azurerm_search_service.ai_search.location
32-
name = "private-endpoint-primary-${local.search_name}"
28+
name = "pe-primary-${azurecaf_name.main_names.results["azurerm_search_service"]}"
3329
resource_group_name = local.resource_group_name
3430
subnet_id = local.pe_primary_subnet_id
3531
tags = var.tags
3632

3733
private_service_connection {
3834
is_manual_connection = false
39-
name = "private-connection-primary-${local.search_name}"
35+
name = "pc-primary-${azurecaf_name.main_names.results["azurerm_search_service"]}"
4036
private_connection_resource_id = azurerm_search_service.ai_search.id
4137
subresource_names = ["searchService"]
4238
}
@@ -47,14 +43,14 @@ resource "azurerm_private_endpoint" "primary_endpoint" {
4743
# Failover region private endpoint
4844
resource "azurerm_private_endpoint" "failover_endpoint" {
4945
location = local.failover_virtual_network_location
50-
name = "private-endpoint-failover-${local.search_name}"
46+
name = "pe-failover-${azurecaf_name.main_names.results["azurerm_search_service"]}"
5147
resource_group_name = local.resource_group_name
5248
subnet_id = local.pe_failover_subnet_id
5349
tags = var.tags
5450

5551
private_service_connection {
5652
is_manual_connection = false
57-
name = "private-connection-failover-${local.search_name}"
53+
name = "pc-failover-${azurecaf_name.main_names.results["azurerm_search_service"]}"
5854
private_connection_resource_id = azurerm_search_service.ai_search.id
5955
subresource_names = ["searchService"]
6056
}

infra/main.search_configuration.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ resource "azurerm_storage_account" "deployment_container" {
1919
# checkov:skip=CKV2_AZURE_38: Enabling soft delete for deployment container protection
2020
# checkov:skip=CKV2_AZURE_47: Blob anonymous access required for deployment scripts
2121
# checkov:skip=CKV2_AZURE_1: Customer managed encryption not needed for temporary deployment container
22-
name = "deploycontainer${random_string.name.id}"
22+
name = azurecaf_name.deployment_script_names.results["azurerm_storage_account"]
2323
resource_group_name = local.resource_group_name
2424
location = local.resource_group_location
2525
account_tier = "Standard"

infra/main.security.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
data "azurerm_subscription" "current" {}
77

88
resource "azurerm_user_assigned_identity" "script_identity" {
9-
name = "deployment-script-identity"
9+
name = azurecaf_name.deployment_script_names.results["azurerm_user_assigned_identity"]
1010
resource_group_name = local.resource_group_name
1111
location = local.resource_group_location
1212
tags = var.tags

0 commit comments

Comments
 (0)