Skip to content

πŸš€Creating Azure Resources Using Terraform HCL πŸ› οΈπŸŒ©οΈ This repository shows how to provision resources in Azure Cloud using Terraform HCL. Along the way, it also demonstrates various key Terraform concepts 🧠 β€” helping you understand how to structure, manage, and scale your infrastructure-as-code like a pro! πŸ’ͺ🌍

Notifications You must be signed in to change notification settings

Shatabdi2621/Terraform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Azure Terraform Demo Project

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.

Project Structure

.
β”œβ”€β”€ 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

Prerequisites

  • Terraform v1.0.0 or newer
  • Azure CLI installed and configured
  • Azure subscription

Getting Started

  • 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 Commands Demonstrated

  • terraform init: Initialize working directory
  • terraform plan: Preview changes
  • terraform apply: Apply changes
  • terraform destroy: Destroy infrastructure
  • terraform import: Import existing resources
  • terraform state list: List resources in state
  • terraform state show: Show resource details
  • terraform validate: Validate configuration
  • terraform fmt: Format configuration files

State Management

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.

Workspaces

The project supports multiple environments through Terraform workspaces. Use:

terraform workspace new dev|staging|prod
terraform workspace select dev|staging|prod

Variable Files

Environment-specific variables are stored in the environments directory:

  • dev.tfvars: Development environment
  • staging.tfvars: Staging environment
  • prod.tfvars: Production environment

Prerequisites

  • Terraform CLI
  • Properly configured cloud provider credentials (e.g., AWS CLI, Azure CLI), here we are using Azure CLI.

Backend Configuration

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"
  }
}

Running Terraform in Stages

Each environment (dev, staging, prod) has its own variable definitions located in the environments/ folder. Replace <env> with the desired environment name.

Initialize Terraform

terraform init

Plan Terraform

terraform plan -var-file="environments/<env>.tfvars"

Apply Terraform

terraform apply -var-file="environments/<env>.tfvars"

Destroy Terraform

terraform destroy -var-file="environments/<env>.tfvars"

Example

terraform init
terraform plan -var-file="environments/dev.tfvars"
terraform apply -var-file="environments/dev.tfvars"

Notes

  • 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.

About

πŸš€Creating Azure Resources Using Terraform HCL πŸ› οΈπŸŒ©οΈ This repository shows how to provision resources in Azure Cloud using Terraform HCL. Along the way, it also demonstrates various key Terraform concepts 🧠 β€” helping you understand how to structure, manage, and scale your infrastructure-as-code like a pro! πŸ’ͺ🌍

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages