Skip to content

Azure Resource Deployment Patterns with Terraform

Interactive Diagram

새 탭 ↗

Before listing individual Azure resources, this article treats module inputs, dependencies, and outputs passed to other layers as a shared interface. The network establishes the foundational boundary; managed resources are deployed with explicit network, identity, secret, and connectivity inputs.

  • Security: principle of least privilege (open only required ports), network segmentation (DMZ/Application/Database tiers), access control for managed services via Private Endpoints
  • Scalability: CIDR design that accounts for future growth, Load Balancer SKU/health probe/auto-scaling configuration
  • Availability: regional redundancy, use of Availability Zones, disaster-recovery scenarios

We designed around Azure’s standard 24-bit CIDR. Note that Azure reserves 5 IP addresses per subnet (network address, default gateway, two reserved for DNS mapping, and broadcast).

NameCIDRAvailableRemark
vnet-az01-dev-platform-0110.100.0.0/24/24, 226eaAzure standard 24-bit
NameCIDRAvailableRemark
sbn-az01-dev-platform-appgw10.100.0.0/27/27, 27ea(Optional) supports TCP-proxy-based private endpoint connections
sbn-az01-dev-platform-lb10.100.0.32/28/28, 11ea
sbn-az01-dev-platform-etc10.100.0.48/28/28, 11ea
sbn-az01-dev-platform-db10.100.0.64/27/27, 27eaManaged databases (SQL Database, CosmosDB for MongoDB, Cache for Redis)
sbn-az01-dev-platform-pe10.100.0.96/27/27, 27eaOther managed services (Key Vault)
sbn-az01-dev-platform-app10.100.0.128/25/25, 123eaAzure Kubernetes Service, Public LB (kubernetes), Virtual Machine Scale Sets (node pool)

IaC Structure: Repeated Creation with for_each

Section titled “IaC Structure: Repeated Creation with for_each”

This is a structure where 6 subnets are created repeatedly for a single VNet based on config values, using HCL for_each syntax and map-typed variable definitions.

resource "azurerm_virtual_network" "vnet" {
address_space = [var.address_space]
location = var.resource_group_location
name = var.vnet_name
resource_group_name = var.resource_group_name
tags = var.common_tags
}
resource "azurerm_subnet" "subnet_map" {
for_each = var.subnet_names_map
name = each.value
address_prefixes = [lookup(var.subnet_prefixes_map, each.key, "")]
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
}
locals {
azurerm_subnets = [for s in values(azurerm_subnet.subnet_map) : s]
}

Network Terraform Graph

From a network-integration standpoint, we set up per-service Private DNS Zones, NSG rules, and subnet delegation. From an authentication/authorization standpoint, we applied Managed Identity (system-assigned vs. user-assigned) and RBAC (role definitions, scoping).

  • Node groups are backed by Azure Virtual Machine Scale Sets
  • The Load Balancer/VMSS created alongside the cluster are managed in a separate MC (Managed Cluster) resource group
  • CNI: instead of the default kubenet, we use a Cilium-based CNI Overlay network
  • Ingress: Azure’s application routing add-on (similar to an NGINX Ingress Controller) is deployed in the app-routing-system namespace
  • Secrets: the Azure Key Vault CSI driver add-on is enabled
resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" {
prefix = var.aks_name
}
resource "azurerm_kubernetes_cluster" "aks" {
location = var.resource_group_location
name = var.aks_name
resource_group_name = var.resource_group_name
node_resource_group = var.aks_node_resource_group_name
dns_prefix = random_pet.azurerm_kubernetes_cluster_dns_prefix.id
kubernetes_version = var.aks_control_plane_version
image_cleaner_interval_hours = 72
identity {
type = "SystemAssigned"
}
default_node_pool {
name = var.aks_default_node_name
vm_size = var.aks_default_node_size
vnet_subnet_id = data.azurerm_subnet.subnet_data.id
node_count = var.aks_default_node_count
orchestrator_version = var.aks_data_plane_version
upgrade_settings {
drain_timeout_in_minutes = null
max_surge = "10%"
node_soak_duration_in_minutes = null
}
}
linux_profile {
admin_username = var.aks_node_username
ssh_key {
key_data = azapi_resource_action.ssh_public_key_gen.output.publicKey
}
}
network_profile {
network_plugin = var.aks_network_plugin
network_plugin_mode = var.aks_network_plugin_mode
network_data_plane = var.aks_network_data_plane
pod_cidr = var.aks_pod_cidr
service_cidr = var.aks_service_cidr
dns_service_ip = var.aks_dns_service_ip
}
web_app_routing {
dns_zone_ids = []
}
key_vault_secrets_provider {
secret_rotation_enabled = var.aks_keyvault_csi_enabled
}
oidc_issuer_enabled = var.aks_oidc_issuer_enabled
tags = var.common_tags
}

AKS Terraform Graph

We chose the Standard SKU (Premium is needed for a fully private container registry, but Standard was chosen for cost reasons), and we run separate registries for base images and application images. The attach feature couldn’t be configured directly in IaC due to permission constraints, so we temporarily enabled acr_anonymous_pull_enabled to let AKS pull images, then handled the attach separately.

resource "azurerm_container_registry" "acr" {
name = var.acr_name
location = var.resource_group_location
resource_group_name = var.resource_group_name
sku = var.acr_sku_name
admin_enabled = var.acr_admin_enabled
public_network_access_enabled = var.acr_public_network_access_enabled
anonymous_pull_enabled = var.acr_anonymous_pull_enabled
tags = var.common_tags
}

ACR Terraform Graph

Deployed in serverless mode (min vCore 0.5, max memory 2Gi, auto-pause after 60+ minutes idle) and connected only via Private Endpoint, with public access blocked. We first provision the SQL Server account, then create the SQL Database as a separate resource. The admin password is generated as a random password and managed in Key Vault.

resource "random_password" "sqlserver_admin_password" {
length = 16
special = true
override_special = "!@#%^&*" # only special characters allowed by SQL Server
}
resource "azurerm_key_vault_secret" "sqlserver_secret" {
name = "${var.sqlserver_name}-password"
value = random_password.sqlserver_admin_password.result
key_vault_id = var.sqlserver_vault_id
}
resource "azurerm_mssql_server" "sqlserver" {
location = var.resource_group_location
resource_group_name = var.resource_group_name
name = var.sqlserver_name
version = var.sqlserver_version
administrator_login = var.sqlserver_administrator_login
administrator_login_password = azurerm_key_vault_secret.sqlserver_secret.value
minimum_tls_version = var.sqlserver_minimum_tls_version
public_network_access_enabled = var.sqlserver_public_access_enabled
identity {
type = "SystemAssigned"
}
tags = var.common_tags
}
resource "azurerm_mssql_database" "sqldb" {
name = var.sqldb_name
server_id = var.sqlserver_id
collation = var.sqldb_collation
max_size_gb = var.sqldb_max_size_gb
enclave_type = var.sqldb_enclave_type
sku_name = var.sqldb_sku_name
# serverless
min_capacity = var.sqldb_min_capacity
auto_pause_delay_in_minutes = var.sqldb_auto_pause_minutes
storage_account_type = "Local"
# prevent the possibility of accidental data loss
lifecycle {
prevent_destroy = true
}
tags = var.common_tags
}

SQL Database Terraform Graph 1 SQL Database Terraform Graph 2

We chose RU-based mode (cost is calculated from a combination of throughput consumed and storage used, and serverless mode is available for this option). vCore-based was still in preview and not yet supported in the Korea Central region, so we excluded it. We use MongoDB 7.0 (LTS), the Standard SKU, and EnableServerless, connecting only via Private Endpoint.

resource "azurerm_cosmosdb_account" "cosmosdb_mongo" {
name = var.cosmosdb_name
location = var.resource_group_location
resource_group_name = var.resource_group_name
kind = var.cosmosdb_kind
mongo_server_version = var.cosmosdb_mongo_version
offer_type = "Standard"
automatic_failover_enabled = false
default_identity_type = "FirstPartyIdentity"
public_network_access_enabled = false
geo_location {
location = var.resource_group_location
failover_priority = 0
zone_redundant = false
}
consistency_policy {
consistency_level = "Session"
}
capabilities {
name = "EnableServerless"
}
capabilities {
name = "EnableAggregationPipeline"
}
capabilities {
name = "mongoEnableDocLevelTTL"
}
capabilities {
name = "EnableMongo"
}
backup {
type = "Continuous"
tier = "Continuous7Days"
}
tags = var.common_tags
}

CosmosDB Terraform Graph

We selected the minimal Basic SKU configuration (max size 250MB, up to 256 client connections), running Redis version 6 (7.2 was still in preview). Connected only via Private Endpoint.

resource "azurerm_redis_cache" "cache_redis" {
name = var.cache_name
location = var.resource_group_location
resource_group_name = var.resource_group_name
sku_name = var.cache_sku_name
family = var.cache_family
capacity = var.cache_capacity
redis_version = var.cache_redis_version
public_network_access_enabled = false
non_ssl_port_enabled = false
access_keys_authentication_enabled = true
redis_configuration {
active_directory_authentication_enabled = true
}
tags = var.common_tags
}

Redis Cache Terraform Graph

We used the minimal Standard SKU configuration. As a rule, we connect only via Private Endpoint, but since terraform apply running on a GitHub-hosted runner needs to write out passwords/secrets as Key Vault secrets, we made a public-access exception. We store DB access keys/passwords, DB tools passwords, and the jumpbox VM password here.

resource "azurerm_key_vault" "keyvault" {
name = var.keyvault_name
location = var.resource_group_location
resource_group_name = var.resource_group_name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = var.keyvault_sku_name
public_network_access_enabled = var.keyvault_public_enabled
soft_delete_retention_days = var.keyvault_soft_delete_retention_days
purge_protection_enabled = false
enable_rbac_authorization = true
enabled_for_disk_encryption = true
enabled_for_deployment = true
enabled_for_template_deployment = true
tags = var.common_tags
}

Key Vault Terraform Graph

The jumpbox VM sits in the ETC subnet, which has no public IP access, and is used for Azure CLI-based resource control as well as writing/validating Terraform plans. It’s created via the azurerm_linux_virtual_machine resource and configured via custom-data-based cloud-init to install basic development tools and set up the Azure CLI/Terraform execution environment.

The user-assigned managed identity is initially created by an administrator who grants the roles, while the federated credential is managed through Terraform. It’s used for GitHub Actions OIDC authentication (e.g. the platform-infra develop/digger workflow) and for AKS OIDC authentication (e.g. the db-tools and application namespaces).

Continue with IaC Quality Gates: Operating Infracost, TFLint, and Terragrunt.