🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now β†’
Menu

Categories

Terraform Complete Guide: Infrastructure as Code Commands & Best Practices (2026)

Terraform Complete Guide: Infrastructure as Code Commands & Best Practices (2026)

Terraform is the most widely used Infrastructure as Code (IaC) tool in the world. It allows you to define, provision, and manage cloud infrastructure using declarative configuration files written in HCL (HashiCorp Configuration Language). Whether you use AWS, Azure, GCP, or any combination, Terraform provides a consistent workflow for managing it all.

Terraform Infrastructure as Code

Free Terraform Cheat Sheet

All essential commands, HCL syntax, resource examples, and Terraform vs alternatives comparison.

Download Free PDF

Table of Contents

What is Terraform?

Terraform, created by HashiCorp, treats infrastructure as code. Instead of clicking through AWS Console or Azure Portal, you write configuration files that describe your desired infrastructure state. Terraform then calculates what changes are needed and applies them.

Key advantages:

  • Multi-cloud β€” Single tool for AWS, Azure, GCP, Kubernetes, and 3000+ providers
  • Declarative β€” Describe what you want, Terraform figures out how
  • Plan before apply β€” Preview all changes before they happen
  • State tracking β€” Knows current infrastructure state, only changes what is needed
  • Version controlled β€” Infrastructure definitions in Git = history, review, rollback
  • Modular β€” Reusable modules for common patterns

Core Concepts

ConceptDescriptionExample
ProviderPlugin that connects to a cloud/serviceaws, azurerm, google, kubernetes
ResourceInfrastructure component to manageaws_instance, aws_s3_bucket
Data SourceRead-only query for existing resourcesdata.aws_ami, data.aws_vpc
VariableInput parameter for configurationvar.region, var.instance_type
OutputExported value after applyoutput.instance_ip
ModuleReusable collection of resourcesmodule.vpc, module.eks
StateCurrent infrastructure state (JSON)terraform.tfstate
BackendWhere state is storedS3, Azure Blob, Terraform Cloud

The Terraform Workflow

  1. Write β€” Define infrastructure in .tf files
  2. Init β€” terraform init downloads providers and initializes backend
  3. Plan β€” terraform plan shows what will change (add, modify, destroy)
  4. Apply β€” terraform apply makes the changes
  5. Destroy β€” terraform destroy tears down all managed resources

Essential Commands

CommandDescription
terraform initInitialize working directory, download providers
terraform planPreview changes without applying
terraform applyApply changes to infrastructure
terraform destroyDestroy all managed resources
terraform fmtFormat .tf files to canonical style
terraform validateValidate configuration syntax
terraform state listList all resources in state
terraform importImport existing resource into state
terraform outputShow output values
terraform workspaceManage workspaces (dev/staging/prod)

HCL Configuration

Terraform uses HCL (HashiCorp Configuration Language), a declarative language designed for infrastructure. Key elements:

  • Providers β€” Configure which cloud services to use
  • Resources β€” Define infrastructure components
  • Variables β€” Parameterize configurations
  • Locals β€” Computed values for DRY code
  • Outputs β€” Export values for other modules or display
  • for_each / count β€” Create multiple similar resources
  • dynamic blocks β€” Generate repeated nested blocks

State Management

Terraform state is a JSON file that maps your configuration to real-world resources. It is the most critical part of Terraform β€” if you lose state, Terraform cannot manage existing resources.

Remote State Backends

BackendBest ForLocking
S3 + DynamoDBAWS teamsYes (DynamoDB)
Azure BlobAzure teamsYes (native)
GCSGCP teamsYes (native)
Terraform CloudTeams, CI/CDYes (managed)
Critical: Never store terraform.tfstate in Git. It may contain secrets (passwords, keys). Always use a remote backend with state locking to prevent concurrent modifications.

Modules

Modules are reusable packages of Terraform configuration. The Terraform Registry has thousands of community modules for common infrastructure patterns.

Workspaces

Workspaces let you manage multiple environments (dev, staging, prod) from the same configuration with separate state files.

Terraform vs Alternatives

FeatureTerraformOpenTofuPulumiCloudFormation
LanguageHCLHCL (compatible)Python/TS/Go/JavaYAML/JSON
Multi-cloudYes (3000+ providers)Yes (same providers)YesAWS only
LicenseBSL 1.1MPL 2.0 (open source)Apache 2.0Proprietary
CommunityLargestGrowing fastGrowingAWS ecosystem
Best forMost teamsOpen-source focusedDevs who prefer codeAWS-only shops

Best Practices

  1. Use remote state β€” S3/Azure Blob with locking. Never local state in production
  2. Version pin providers β€” Use version = "~> 5.0" to prevent breaking changes
  3. Use modules β€” DRY principle. Create modules for repeated infrastructure patterns
  4. Plan before apply β€” Always review terraform plan output
  5. Use workspaces or directories β€” Separate environments (dev/staging/prod)
  6. Format code β€” Run terraform fmt before committing
  7. Use variables β€” Never hardcode values. Use .tfvars for environment-specific values
  8. Enable state locking β€” Prevent concurrent modifications
  9. Tag everything β€” Tags enable cost tracking, ownership, and automation
  10. Use CI/CD β€” Automate plan/apply with GitHub Actions or GitLab CI

Download the Terraform Cheat Sheet

All commands, HCL syntax, state management, and alternatives comparison in a printable PDF.

Download Free PDF

Related Articles

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.