📅 Terraform for Cloud

Topic: Terraform & AWS CLI Integration

🧭 Key Discussion Points

  1. Terraform Introduction
    • What is Terraform?
    • Use cases in cloud automation
  2. HashiCorp Configuration Language (HCL)
    • Syntax overview
    • Declarative structure
  3. Installation & Setup
  4. Benefits & Cons of Terraform
    • Pros: modularity, scalability
    • Cons: state file complexity
  5. AWS CLI Integration
  6. Understanding IaC
    • What is Infrastructure as Code?
    • Why it matters in DevOps
  7. Terraform ↔ AWS Connectivity
    • Provider configuration
    • Authentication via access keys
  8. Practical Demo
    • Created IAM user and group
    • Attached policy for programmatic access
    • Configured AWS CLI with credentials
    • Used commands: terraform init, terraform plan, terraform apply
    • Created EC2 instance using Terraform ▸ Used AMI image and named it Devmachine
    • Validated Terraform configuration ▸ Used terraform validate to check syntax and logical consistency ▸ Optional: used JSON output for CI/CD integration

✅ Commands And process

🚀 Workflow Commands

CommandExplanation
terraform initInitializes working directory, downloads providers/modules.
terraform planPreviews changes without applying.
terraform plan -out=plan.tfplanSaves plan to file for later apply.
terraform applyApplies changes interactively.
terraform apply -auto-approveApplies without confirmation (CI/CD use).
terraform destroyDestroys all managed infra. Use carefully.
terraform destroy -target=aws_instance.webDestroys only a specific resource.

🛠 Validation & Formatting

CommandExplanation
terraform validateChecks `.tf` files for syntax/config errors.
terraform fmtFormats `.tf` files into canonical HCL style.
terraform fmt -checkChecks formatting only (CI-friendly).
terraform fmt -recursiveFormats files in subdirectories too.

📦 State Management

CommandExplanation
terraform state listLists all resources tracked in state.
terraform state show <resource>Shows attributes of a resource.
terraform state mv <src> <dst>Moves/renames resource in state.
terraform state rm <resource>Removes resource from state (infra stays).
terraform state pullDownloads remote state as JSON.
terraform state push <file>Uploads local state file to backend.
terraform import <resource> <id>Imports existing infra into state.
terraform force-unlock <lock-id>Releases a stuck state lock.

🌍 Workspaces

CommandExplanation
terraform workspace listLists all workspaces (dev, staging, prod).
terraform workspace showShows current workspace.
terraform workspace new <name>Creates new workspace for environment separation.
terraform workspace select <name>Switches workspace.
terraform workspace delete <name>Deletes a workspace (not current).

🔍 Inspection & Debugging

CommandExplanation
terraform showDisplays current state or saved plan.
terraform show plan.tfplanShows saved plan file.
terraform outputLists all output values.
terraform output -jsonOutputs values as JSON.
terraform graph | dot -Tpng > graph.pngGenerates dependency graph.
terraform providersLists required providers.
terraform versionShows installed version.

⚠️ Best Practices

✅ Action Items