Terraform is a powerful tool for Infrastructure as Code (IaC) that allows you to build, change, and version your infrastructure safely and efficiently. In this post, we’ll cover everything you need to get started with Terraform — from installation and configuration to advanced commands and workspaces. Plus, I’ll include a FAQ section with common technical interview questions and examples.

Prerequisites
Before we begin, ensure you have the following installed on your local machine:
- Terraform (Download & Install)
- AWS CLI (Install AWS CLI)
- An AWS account with IAM access to create resources
Step 1: Installing Terraform
macOS (via Homebrew)
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Linux
wget https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip
unzip terraform_1.6.0_linux_amd64.zip
sudo mv terraform /usr/local/bin/
Windows (via Chocolatey)
choco install terraform
Verify the installation:
terraform -v
Step 2: Configuring Terraform for AWS
Create a main.tf file with a simple Terraform configuration for an AWS S3 bucket:
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-terraform-demo-bucket"
acl = "private"
}
output "bucket_name" {
value = aws_s3_bucket.my_bucket.id
}
Initializing Terraform
Run:
terraform init
This downloads required providers and initializes the working directory.
Planning and Applying Changes
terraform plan
terraform apply -auto-approve
This creates the S3 bucket in AWS.
Step 3: Advanced Terraform Commands
1. terraform refresh — Updates state file with real-world changes
terraform refresh
2. terraform import — Imports existing resources into Terraform state
terraform import aws_s3_bucket.my_bucket existing-bucket-name
3. terraform taint — Marks a resource for recreation
terraform taint aws_s3_bucket.my_bucket
4. terraform state mv — Moves a resource within Terraform state
terraform state mv aws_s3_bucket.my_bucket module.s3.aws_s3_bucket.new_bucket
5. terraform destroy — Deletes all managed resources
terraform destroy
Step 4: Terraform Workspaces for Multi-Environment Management
Terraform workspaces enable you to manage multiple environments (e.g., dev, staging, prod) within the same configuration.
Creating and Switching Workspaces
terraform workspace new dev
terraform workspace new staging
terraform workspace new prod
List workspaces:
terraform workspace list
Switch to a workspace:
terraform workspace select staging
Get current workspace:
terraform workspace show
Using Workspaces in Configuration
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-bucket-${terraform.workspace}"
}
This dynamically names your bucket based on the active workspace.
FAQs:
Q1: What is Terraform state, and why is it important?
A: Terraform state (terraform.tfstate) tracks infrastructure resources. Losing it can lead to Terraform creating duplicate resources.
📌 Example:
cat terraform.tfstate
Q2: Difference between terraform apply and terraform plan?
A:
terraform planshows the execution plan before applying changes.terraform applyexecutes the planned changes.
📌 Example:
terraform plan -out=tfplan
terraform apply tfplan
Q3: How does Terraform handle dependencies between resources?
A: Terraform automatically determines the order of resource creation using implicit and explicit dependencies.
📌 Implicit Dependency Example:
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "subnet1" {
vpc_id = aws_vpc.main.id # Implicit dependency
cidr_block = "10.0.1.0/24"
}
📌 Explicit Dependency (depends_on)
resource "aws_instance" "example" {
depends_on = [aws_security_group.sg]
}
Q4: How do you store Terraform state remotely?
A: Use S3 with DynamoDB for state locking.
📌 Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
Run:
terraform init
Q5: How does Terraform handle secret management?
A:
- Environment Variables
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
- Use AWS Secrets Manager
- Use HashiCorp Vault with the Terraform Vault provider
Conclusion
You’ve now mastered:
✅ Installing and configuring Terraform
✅ Essential and advanced Terraform commands
✅ Using Terraform workspaces for multi-environment setups
✅ Common Terraform interview questions with examples
Terraform is a must-know tool for DevOps and Cloud Engineers. Keep learning, experimenting, and optimizing your infrastructure! 🚀
If you found this helpful, follow me for more DevOps tutorials!
📢 Have questions or feedback? Drop a comment below or connect with me on Twitter/X@spysood!
Originally published on Medium.