|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + azurecaf = { |
| 4 | + source = "aztfmod/azurecaf" |
| 5 | + version = "1.2.26" |
| 6 | + } |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +data "azuread_client_config" "current" {} |
| 11 | + |
| 12 | +# App Configuration naming convention using azurecaf_name module. |
| 13 | +resource "azurecaf_name" "azurerm_app_config" { |
| 14 | + name = var.application_name |
| 15 | + resource_type = "azurerm_app_configuration" |
| 16 | + suffixes = [var.environment] |
| 17 | +} |
| 18 | + |
| 19 | +# Create Azure App Configuration |
| 20 | + |
| 21 | +resource "azurerm_app_configuration" "app_config" { |
| 22 | + name = azurecaf_name.azurerm_app_config.result |
| 23 | + resource_group_name = var.resource_group |
| 24 | + location = var.location |
| 25 | + |
| 26 | + sku = var.environment == "prod" ? "standard" : "free" |
| 27 | + |
| 28 | + purge_protection_enabled = var.environment == "prod" ? true : false |
| 29 | + |
| 30 | + public_network_access = var.environment == "prod" ? "Disabled" : "Enabled" |
| 31 | + |
| 32 | + local_auth_enabled = var.environment == "prod" ? false : true |
| 33 | + |
| 34 | + dynamic "replica" { |
| 35 | + for_each = var.replica_location != null ? [var.replica_location] : [] |
| 36 | + content { |
| 37 | + location = replica.value |
| 38 | + name = "AppConfig${var.environment}${replica.value}" |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +# Create App Configuration Features |
| 44 | + |
| 45 | +resource "azurerm_app_configuration_feature" "feature" { |
| 46 | + for_each = var.features != null ? { for idx, feature in var.features : idx => feature } : {} |
| 47 | + configuration_store_id = azurerm_app_configuration.app_config.id |
| 48 | + description = each.value.description |
| 49 | + name = each.value.name |
| 50 | + |
| 51 | + enabled = each.value.enabled |
| 52 | + locked = each.value.locked |
| 53 | + label = each.value.label |
| 54 | +} |
| 55 | + |
| 56 | +# Create App Configuration Keys |
| 57 | + |
| 58 | +resource "azurerm_app_configuration_key" "key" { |
| 59 | + for_each = var.keys != null ? { for idx, key in var.keys : idx => key } : {} |
| 60 | + configuration_store_id = azurerm_app_configuration.app_config.id |
| 61 | + key = each.value.key |
| 62 | + type = each.value.type |
| 63 | + content_type = each.value.type == "kv" ? each.value.content_type : null |
| 64 | + value = each.value.type == "kv" ? each.value.value : null |
| 65 | + vault_key_reference = each.value.type == "vault" ? each.value.vault_key_reference : null |
| 66 | + label = each.value.label |
| 67 | + locked = each.value.locked |
| 68 | +} |
| 69 | + |
| 70 | +# Create role assignments |
| 71 | + |
| 72 | +resource "azurerm_role_assignment" "app_service_reader_user_role_assignment" { |
| 73 | + principal_id = var.app_service_identity_principal_id |
| 74 | + role_definition_name = "App Configuration Data Reader" |
| 75 | + scope = azurerm_app_configuration.app_config.id |
| 76 | +} |
| 77 | + |
| 78 | +# For demo purposes, allow current user access to the app config |
| 79 | +# Note: when running as a service principal, this is also needed |
| 80 | + |
| 81 | +resource "azurerm_role_assignment" "azconfig_owner_user_role_assignment" { |
| 82 | + scope = azurerm_app_configuration.app_config.id |
| 83 | + role_definition_name = "App Configuration Data Owner" |
| 84 | + principal_id = data.azuread_client_config.current.object_id |
| 85 | +} |
| 86 | + |
| 87 | +# Create Private DNS Zone and Endpoint for App Configuration |
| 88 | + |
| 89 | +resource "azurerm_private_dns_zone" "dns_for_azconfig" { |
| 90 | + count = var.environment == "prod" ? 1 : 0 |
| 91 | + name = "privatelink.azconfig.io" |
| 92 | + resource_group_name = var.resource_group |
| 93 | +} |
| 94 | + |
| 95 | +resource "azurerm_private_dns_zone_virtual_network_link" "virtual_network_link_azconfig" { |
| 96 | + count = var.environment == "prod" ? 1 : 0 |
| 97 | + name = "privatelink.azconfig.io" |
| 98 | + private_dns_zone_name = azurerm_private_dns_zone.dns_for_azconfig[0].name |
| 99 | + virtual_network_id = var.spoke_vnet_id |
| 100 | + resource_group_name = var.resource_group |
| 101 | +} |
| 102 | + |
| 103 | +resource "azurerm_private_endpoint" "azconfig_pe" { |
| 104 | + count = var.environment == "prod" ? 1 : 0 |
| 105 | + name = "private-endpoint-ac" |
| 106 | + location = var.location |
| 107 | + resource_group_name = var.resource_group |
| 108 | + subnet_id = var.private_endpoint_subnet_id |
| 109 | + |
| 110 | + private_dns_zone_group { |
| 111 | + name = "privatednsazconfigzonegroup" |
| 112 | + private_dns_zone_ids = [azurerm_private_dns_zone.dns_for_azconfig[0].id] |
| 113 | + } |
| 114 | + |
| 115 | + private_service_connection { |
| 116 | + name = "peconnection-azconfig" |
| 117 | + private_connection_resource_id = azurerm_app_configuration.app_config.id |
| 118 | + is_manual_connection = false |
| 119 | + subresource_names = ["configurationStores"] |
| 120 | + } |
| 121 | +} |
0 commit comments