This project demonstrates key Terraform concepts for Azure infrastructure management using a structured approach. It provisions a complete environment with networking, storage, and compute resources.
.
βββ README.md
βββ main.tf
βββ variables.tf
βββ outputs.tf
βββ locals.tf
βββ provider.tf
βββ backend.tf
βββ environments
β βββ dev.tfvars
β βββ prod.tfvars
β βββ staging.tfvars
βββ modules
βββ networking
βββ storage
βββ compute
- Terraform v1.0.0 or newer
- Azure CLI installed and configured
- Azure subscription
- Clone this repository
- Authenticate with Azure:
az login
- Initialize Terraform:
terraform init
- Create a workspace (optional):
terraform workspace new dev
- Plan your infrastructure:
terraform plan -var-file=environments/dev.tfvars
- Apply the configuration:
terraform apply -var-file=environments/dev.tfvars
- Destroy when done:
terraform destroy -var-file=environments/dev.tfvars
terraform init
: Initialize working directoryterraform plan
: Preview changesterraform apply
: Apply changesterraform destroy
: Destroy infrastructureterraform import
: Import existing resourcesterraform state list
: List resources in stateterraform state show
: Show resource detailsterraform validate
: Validate configurationterraform fmt
: Format configuration files
This project demonstrates state management using an Azure Storage account backend. For local development, you can use the local state file, but for team environments, the Azure backend provides state locking and collaboration features.
The project supports multiple environments through Terraform workspaces. Use:
terraform workspace new dev|staging|prod
terraform workspace select dev|staging|prod
Environment-specific variables are stored in the environments directory:
dev.tfvars
: Development environmentstaging.tfvars
: Staging environmentprod.tfvars
: Production environment
- Terraform CLI
- Properly configured cloud provider credentials (e.g., AWS CLI, Azure CLI), here we are using Azure CLI.
The backend.tf
file defines the remote backend for storing the Terraform state.
Before running Terraform, you must replace the default backend configuration with the remote backend of your choice (e.g., S3, Azure Blob Storage, GCS, etc.).
Example for Azure Storage Account/Blob Storage:
terraform {
backend "azurerm" {
resource_group_name = "terraform-state-rg"
storage_account_name = "terraformstate12345"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
Each environment (dev
, staging
, prod
) has its own variable definitions located in the environments/
folder.
Replace <env>
with the desired environment name.
terraform init
terraform plan -var-file="environments/<env>.tfvars"
terraform apply -var-file="environments/<env>.tfvars"
terraform destroy -var-file="environments/<env>.tfvars"
terraform init
terraform plan -var-file="environments/dev.tfvars"
terraform apply -var-file="environments/dev.tfvars"
- Make sure your cloud credentials are properly configured before running the scripts.
- If youβre using additional modules, conditionally include them as needed in
main.tf
. - You may also use Terraform workspaces as an alternative to
.tfvars
-based separation.