0
0
TerraformHow-ToBeginner · 4 min read

Terraform Best Practices: Clean, Safe, and Scalable Infrastructure

Use modules to organize reusable code, keep state files secure and remote, and write clear variable and output definitions. Always plan changes with terraform plan before applying to avoid surprises.
📐

Syntax

Terraform code is written in HCL (HashiCorp Configuration Language). The main parts include providers to specify cloud services, resources to define infrastructure, variables for input, and outputs to expose information.

Modules group resources for reuse. State files track deployed infrastructure.

terraform
provider "aws" {
  region = "us-east-1"
}

variable "instance_type" {
  type    = string
  default = "t2.micro"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
}

output "instance_id" {
  value = aws_instance.example.id
}
💻

Example

This example shows a simple AWS EC2 instance deployment using variables and outputs. It demonstrates clean structure and reusable input.

terraform
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.0"
    }
  }

  backend "s3" {
    bucket = "my-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "us-east-1"
  }
}

provider "aws" {
  region = "us-east-1"
}

variable "instance_type" {
  description = "Type of EC2 instance"
  type        = string
  default     = "t3.micro"
}

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
  tags = {
    Name = "ExampleInstance"
  }
}

output "instance_id" {
  description = "The ID of the EC2 instance"
  value       = aws_instance.web.id
}
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: instance_id = "i-0abcd1234efgh5678"
⚠️

Common Pitfalls

  • Not using remote state: Storing state locally risks loss and conflicts. Use remote backends like S3 with locking.
  • Hardcoding values: Avoid fixed values; use variables for flexibility.
  • Ignoring terraform plan: Always preview changes to prevent accidental resource destruction.
  • Mixing environments: Separate dev, staging, and prod with workspaces or folders.
terraform
### Wrong: Hardcoded instance type
resource "aws_instance" "bad" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

### Right: Use variable
variable "instance_type" {
  type    = string
  default = "t2.micro"
}

resource "aws_instance" "good" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
}
📊

Quick Reference

  • Use modules to organize and reuse code.
  • Keep terraform state remote and secure.
  • Use variables for inputs and outputs to expose info.
  • Run terraform plan before apply.
  • Separate environments with workspaces or folders.

Key Takeaways

Always use remote state storage with locking to avoid conflicts and data loss.
Organize code into modules for reuse and clarity.
Use variables for flexible configuration instead of hardcoding values.
Run terraform plan before apply to preview changes safely.
Separate environments to prevent accidental cross-environment changes.