DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

Terraform

🍏 INSTALL TERRAFORM ON macOS (100% working)

You have two options:


βœ… OPTION 1 β€” Install Terraform using Homebrew (RECOMMENDED)

Step 1: Update Homebrew

brew update
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Terraform

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the installation

terraform -version
Enter fullscreen mode Exit fullscreen mode

You should see something like:

Terraform v1.7.x
Enter fullscreen mode Exit fullscreen mode

🧹 OPTION 2 β€” Manual Installation for Mac

Step 1: Download Terraform

Go to:

https://developer.hashicorp.com/terraform/downloads

Download:

macOS 64-bit .zip file
Enter fullscreen mode Exit fullscreen mode

Step 2: Unzip

Double-click the .zip β†’ you will get a single file:

terraform
Enter fullscreen mode Exit fullscreen mode

Step 3: Move Terraform binary to /usr/local/bin

Run:

sudo mv terraform /usr/local/bin/
sudo chmod +x /usr/local/bin/terraform
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify:

terraform -version
Enter fullscreen mode Exit fullscreen mode

Done.


πŸͺŸ INSTALL TERRAFORM ON WINDOWS

You can install Terraform in two ways.


βœ… OPTION 1 β€” Install Terraform using Chocolatey (BEST)

Step 1 β€” Install Chocolatey (if not installed)

Open PowerShell as Administrator
Run:

Set-ExecutionPolicy Bypass -Scope Process -Force; `
[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.ServicePointManager]::SecurityProtocol `
-bor 3072; `
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Enter fullscreen mode Exit fullscreen mode

Step 2 β€” Install Terraform

choco install terraform -y
Enter fullscreen mode Exit fullscreen mode

Step 3 β€” Verify

terraform -version
Enter fullscreen mode Exit fullscreen mode

Done.


🧹 OPTION 2 β€” Manual Installation for Windows

Step 1 β€” Download Terraform

Visit:

https://developer.hashicorp.com/terraform/downloads

Download:

Windows 64-bit .zip
Enter fullscreen mode Exit fullscreen mode

Step 2 β€” Unzip

You get:

terraform.exe
Enter fullscreen mode Exit fullscreen mode

Step 3 β€” Move it to system PATH

Create a folder:

C:\terraform
Enter fullscreen mode Exit fullscreen mode

Move terraform.exe into that folder.

Step 4 β€” Add to PATH

  1. Open Control Panel
  2. Click System
  3. Click Advanced system settings
  4. Click Environment Variables
  5. Under System Variables, find Path
  6. Click Edit
  7. Click New
  8. Add:
C:\terraform
Enter fullscreen mode Exit fullscreen mode

Save & close.

Step 5 β€” Verify

Open new PowerShell:

terraform -version
Enter fullscreen mode Exit fullscreen mode

Done.


πŸŽ‰ Terraform is installed on both systems!

🌱 ** What is Terraform? **

Terraform is:

  • IaC β€” Infrastructure as Code
  • Declarative tool β†’ you write WHAT you want, Terraform decides HOW to build it
  • Cloud-agnostic β†’ AWS, Azure, GCP, Kubernetes, GitHub, Datadog, Cloudflare, etc.

Terraform workflow:

Write β†’ Plan β†’ Apply β†’ Destroy
Enter fullscreen mode Exit fullscreen mode

State file:

terraform.tfstate
Enter fullscreen mode Exit fullscreen mode

Holds the real world infrastructure state.
Terraform compares:

desired (your code) vs real (state)
Enter fullscreen mode Exit fullscreen mode

And creates an execution plan.


🌱 ** Basic Concepts**

1️⃣ Providers

Example: AWS provider.

provider "aws" {
  region = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

2️⃣ Resources

The objects Terraform creates.

resource "aws_instance" "web" {
  ami = "ami-123"
  instance_type = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ Variables

Reusable values.

variable "region" {
  default = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

4️⃣ Outputs

Show results after apply.

output "public_ip" {
  value = aws_instance.web.public_ip
}
Enter fullscreen mode Exit fullscreen mode

5️⃣ Terraform commands

terraform init
terraform validate
terraform plan
terraform apply
terraform destroy
Enter fullscreen mode Exit fullscreen mode

🌿 ** State Management**

State is the MOST important Terraform concept.

Local state:

Stored at:

terraform.tfstate
Enter fullscreen mode Exit fullscreen mode

Remote state:

Recommended for teams.

Example: S3 + DynamoDB lock

terraform {
  backend "s3" {
    bucket         = "tf-state-1234"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-lock"
    encrypt        = true
  }
}
Enter fullscreen mode Exit fullscreen mode

Benefits:

  • Team collaboration
  • State locking
  • No corruption
  • Secure

🌿 *Terraform Best Practices *

πŸ“Œ 1 β€” Use .tfvars for environment values

dev.tfvars
prod.tfvars
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 2 β€” Use modules (DRY code)

Modules = reusable infrastructure blocks.

Directory structure:

modules/
  vpc/
  ec2/
  s3/
envs/
  dev/
  prod/
Enter fullscreen mode Exit fullscreen mode

Real module example:

module "vpc" {
  source = "../modules/vpc"
  cidr   = "10.0.0.0/16"
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 3 β€” Use workspaces (optional)

terraform workspace new dev
terraform workspace select dev
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 4 β€” Follow naming standards


🌳 ** Intermediate (4–5 Years DevOps Experience)**

At this level you must understand:


βœ”οΈ 1 β€” Terraform modules (deep)

Reusable infrastructure packages.

Module structure:

modules/vpc
  main.tf
  outputs.tf
  variables.tf
  versions.tf
Enter fullscreen mode Exit fullscreen mode

Module example:

module "eks" {
  source          = "terraform-aws-modules/eks/aws"
  cluster_name    = "my-eks"
  cluster_version = "1.29"
  subnets         = module.vpc.private_subnets
}
Enter fullscreen mode Exit fullscreen mode

βœ”οΈ 2 β€” Terraform Lifecycle Rules

resource "aws_security_group" "sg" {
  lifecycle {
    create_before_destroy = true
    prevent_destroy       = false
    ignore_changes        = [tags]
  }
}
Enter fullscreen mode Exit fullscreen mode

Used to avoid outages and control recreations.


βœ”οΈ 3 β€” Data sources

Read existing resources:

data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["099720109477"]
}
Enter fullscreen mode Exit fullscreen mode

βœ”οΈ 4 β€” Managing secrets (VERY IMPORTANT)

DO NOT store passwords in Terraform.

Use:

  • AWS Secrets Manager
  • SSM Parameter Store
  • Vault

Example:

data "aws_ssm_parameter" "db_password" {
  name = "/prod/db/password"
}
Enter fullscreen mode Exit fullscreen mode

βœ”οΈ 5 β€” Integrating Terraform in CI/CD

Typical pipeline:

terraform fmt β†’ terraform validate β†’ terraform plan β†’ terraform apply
Enter fullscreen mode Exit fullscreen mode

Using tools:

  • GitHub Actions
  • GitLab CI
  • Jenkins
  • Azure DevOps

Pipeline best practice:

  • No one runs terraform apply manually
  • Only pipeline applies to PROD
  • PR triggers plan output

βœ”οΈ 6 β€” Terraform Import

Import existing resources:

terraform import aws_s3_bucket.mybucket mybucket-name
Enter fullscreen mode Exit fullscreen mode

THEN you write the code for it.


βœ”οΈ 7 β€” Terraform Workspaces (when to use and when not)

Use workspaces for:

  • Small projects
  • Quickly switching environments

Do NOT use workspaces for:

  • Large teams
  • Lots of environments

Better: separate folders or separate state files.


🌳 ** Advanced DevOps (5–6 Years)**

At this level you must know:


πŸ›‘ 1 β€” Terraform Architecture for Large Organizations

You must be able to design:

  • Multi-account AWS structure
  • Shared VPC
  • Shared modules
  • Remote state separation
  • State locking
  • IAM permissions per team

Example enterprise layout:

terraform/
  global/
  network/
  platform/
  environments/
    dev/
    prod/
modules/
Enter fullscreen mode Exit fullscreen mode

πŸ›‘ 2 β€” Terraform with Terragrunt

Terragrunt solves:

  • Duplicate code
  • DRY principle
  • Remote state automatically
  • Module versioning

Terragrunt structure:

live/
  prod/
    vpc/
    eks/
  dev/
modules/
Enter fullscreen mode Exit fullscreen mode

πŸ›‘ 3 β€” Policy as Code (OPA + Sentinel)

Used to enforce rules such as:

  • No public S3
  • No 0.0.0.0/0
  • Mandatory tags
  • Only approved instance types

Terraform Cloud uses Sentinel
Local workflows can use OPA Conftest:

Example:

deny[msg] {
  input.resource.aws_security_group[*].ingress[*].cidr_blocks[_] == "0.0.0.0/0"
}
Enter fullscreen mode Exit fullscreen mode

πŸ›‘ 4 β€” Terraform for Kubernetes (Helm + EKS)

Terraform can:

  • Create cluster
  • Create IAM roles
  • Install Helm charts
  • Manage namespaces
  • Deploy OPA Gatekeeper
  • Deploy Argo CD

πŸ›‘ 5 β€” Terraform for Serverless

Terraform manages:

  • Lambda
  • API Gateway
  • DynamoDB
  • Step Functions
  • EventBridge
  • SQS/SNS

πŸ›‘ 6 β€” Troubleshooting (Senior Level)

You must know how to solve:

❌ Drift

Infrastructure changed manually.

Fix:

terraform plan
terraform refresh
Enter fullscreen mode Exit fullscreen mode

❌ State corruption

Fix with:

  • backup state
  • remote state repair

❌ Orphaned resources

Caused by deleting from code only.


🌟 LEVEL 6 β€” Senior DevOps Knowledge (Interview Answers)

Here’s how you answer:


Q: How do you structure Terraform in your organization?

Senior answer:

I design Terraform using a modular approach with separate state files per environment, stored in S3 with DynamoDB locking.
Each environment has its own pipeline that runs fmt, validate, plan, and apply.
Sensitive variables come from Secrets Manager.
We enforce security rules using OPA/Conftest, and we use Terragrunt to avoid repetitive code and manage multiple accounts.


Q: How do you handle Terraform state in a team?

We use remote S3 backend with DynamoDB locking.
CI/CD pipelines control all changes, and no one applies manually.
State is encrypted with SSE-KMS.
We use versioned state and tags for tracking deployments.


Q: How do you create reusable infrastructure?

Using modules with versioning, stored in a shared Git repository.
Each module includes variables, outputs, documentation, and examples.


Q: How do you prevent security issues in Terraform?

Using OPA Gatekeeper, Conftest, and Sentinel policies to detect public resources, uncontrolled IAM privileges, and missing encryption.

Top comments (0)