This Terraform package provides a flexible, configuration-driven approach to deploy Azure resources. You can enable or disable modules and specify the count of resources for each module through a simple JSON configuration file.
- Configuration-Driven: Control which modules to deploy via a JSON configuration file
- Flexible Counts: Specify how many instances of each module to create
- Modular Architecture: Each Azure resource type is a separate, reusable module
- Comprehensive Outputs: All deployed resources are exposed as outputs
-
Azure CLI installed and configured
az login az account set --subscription <your-subscription-id>
-
Terraform installed (version >= 1.0)
- Download from terraform.io
-
Azure Subscription with appropriate permissions to create resources
terraform-azure/
├── main.tf # Main Terraform configuration
├── variables.tf # Variable definitions
├── outputs.tf # Output definitions
├── modules-config.json # Configuration file for module enablement
├── terraform.tfvars.example # Example variables file
├── README.md # This file
└── modules/ # Individual Azure resource modules
├── storage_account/
├── app_service/
├── app_service_plan/
├── virtual_network/
├── key_vault/
├── container_registry/
├── kubernetes_cluster/
├── aks_backup/
├── sql_server/
├── function_app/
└── log_analytics_workspace/
| Module | Description |
|---|---|
storage_account |
Azure Storage Account |
app_service |
Azure App Service (Web App) |
app_service_plan |
Azure App Service Plan |
virtual_network |
Azure Virtual Network with Subnet |
key_vault |
Azure Key Vault |
container_registry |
Azure Container Registry (ACR) |
kubernetes_cluster |
Azure Kubernetes Service (AKS) |
aks_backup |
Azure Backup vault, policy, and instance for AKS |
sql_server |
Azure SQL Server with Database |
function_app |
Azure Function App |
log_analytics_workspace |
Azure Log Analytics Workspace |
Edit modules-config.json to enable/disable modules and set counts:
{
"modules": {
"storage_account": {
"enabled": true,
"count": 2
},
"app_service": {
"enabled": true,
"count": 1
},
"virtual_network": {
"enabled": true,
"count": 1
},
"key_vault": {
"enabled": true,
"count": 1
},
"app_service_plan": {
"enabled": true,
"count": 1
},
"container_registry": {
"enabled": false,
"count": 0
},
"kubernetes_cluster": {
"enabled": false,
"count": 0
},
"sql_server": {
"enabled": false,
"count": 0
},
"function_app": {
"enabled": false,
"count": 0
},
"log_analytics_workspace": {
"enabled": true,
"count": 1
}
}
}Configuration Options:
enabled: Set totrueto deploy the module,falseto skip itcount: Number of instances to create (0 if disabled)
Create a terraform.tfvars file (or copy from terraform.tfvars.example):
subscription_id = "your-azure-subscription-id"
resource_group_name = "my-resource-group"
location = "East US"
environment = "dev"
name_prefix = "myapp"
tags = {
Project = "MyProject"
Owner = "DevOps Team"
CostCenter = "Engineering"
}Required Variables:
subscription_id: Your Azure subscription IDresource_group_name: Name for the resource group (will be created if it doesn't exist)
Optional Variables:
location: Azure region (default: "East US")environment: Environment name (default: "dev")name_prefix: Prefix for resource names (default: "tf")tags: Additional tags to apply to all resourcesmodules_config_file: Path to modules config file (default: "modules-config.json")
cd terraform-azure
terraform initterraform planThis will show you what resources will be created based on your configuration.
terraform applyType yes when prompted to confirm the deployment.
After deployment, view the outputs:
terraform outputEdit modules-config.json:
{
"modules": {
"storage_account": {
"enabled": true,
"count": 3
},
"app_service": { "enabled": false, "count": 0 },
"virtual_network": { "enabled": false, "count": 0 },
"key_vault": { "enabled": false, "count": 0 },
"app_service_plan": { "enabled": false, "count": 0 },
"container_registry": { "enabled": false, "count": 0 },
"kubernetes_cluster": { "enabled": false, "count": 0 },
"sql_server": { "enabled": false, "count": 0 },
"function_app": { "enabled": false, "count": 0 },
"log_analytics_workspace": { "enabled": false, "count": 0 }
}
}{
"modules": {
"storage_account": { "enabled": true, "count": 1 },
"app_service": { "enabled": true, "count": 2 },
"app_service_plan": { "enabled": true, "count": 1 },
"virtual_network": { "enabled": true, "count": 1 },
"key_vault": { "enabled": true, "count": 1 },
"log_analytics_workspace": { "enabled": true, "count": 1 },
"container_registry": { "enabled": false, "count": 0 },
"kubernetes_cluster": { "enabled": false, "count": 0 },
"sql_server": { "enabled": false, "count": 0 },
"function_app": { "enabled": false, "count": 0 }
}
}Some modules have dependencies on others:
- App Service → Requires App Service Plan (will create one if not provided)
- Function App → Requires App Service Plan and Storage Account (will create if not provided)
- aks_backup → Requires an existing kubernetes_cluster module instance to target
The configuration automatically handles these dependencies.
Each module is located in modules/<module_name>/. You can customize:
- Resource Settings: Edit
main.tfin each module directory - Variables: Modify
variables.tfto add new parameters - Outputs: Add outputs in
outputs.tf
- Create a new directory under
modules/ - Add
main.tf,variables.tf, andoutputs.tf - Add the module to
modules-config.json - Add module call in
main.tf(root) - Add outputs in
outputs.tf(root)
To destroy all resources:
terraform destroyWarning: This will delete all resources created by this Terraform configuration.
-
Authentication Error
- Ensure you're logged in:
az login - Verify subscription:
az account show
- Ensure you're logged in:
-
Resource Name Conflicts
- Azure resource names must be globally unique
- Try changing
name_prefixorenvironmentvariables
-
Quota Exceeded
- Check your Azure subscription quotas
- Some resources have regional limits
-
Permission Errors
- Ensure your Azure account has Contributor or Owner role
- Verify subscription permissions
- SQL Server Passwords: Currently hardcoded in the SQL Server module. In production, use Azure Key Vault or Terraform variables with sensitive flags.
- Key Vault Access: The Key Vault module grants access to the current user. Adjust access policies as needed.
- Storage Account Keys: Storage account keys are marked as sensitive in outputs.
- Use Version Control: Commit your
modules-config.jsonandterraform.tfvars(but exclude sensitive data) - Separate Environments: Use different
terraform.tfvarsfiles for dev/staging/prod - Review Plans: Always run
terraform planbeforeterraform apply - Tag Resources: Use meaningful tags for cost tracking and organization
- Backup State: Store Terraform state in Azure Storage or Terraform Cloud
For issues or questions:
- Check the Terraform Azure Provider documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
- Review Azure resource documentation
- Check Terraform logs for detailed error messages
This Terraform configuration is provided as-is for deployment purposes.