This repository is your one stop solution for Terraform for DevOps Engineers
# Linux & macOS
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
# Verify Installation
terraform -vterraform init- Downloads provider plugins
- Sets up the working directory
terraform fmt # Formats Terraform code
terraform validate # Validates Terraform syntaxterraform plan # Shows execution plan without applying
terraform apply # Creates/updates infrastructure
terraform apply -auto-approve # Applies without manual confirmationterraform destroy # Destroys all managed resources
terraform destroy -auto-approve # Without confirmationterraform state list # Lists all managed resources
terraform show # Shows detailed resource infoterraform state mv <source> <destination> # Move resource in state file
terraform state rm <resource> # Removes resource from state (not from infra)terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-lock"
encrypt = true
}
}terraform init # Reinitialize with remote backendvariable "instance_type" {
default = "t2.micro"
}
resource "aws_instance" "web" {
instance_type = var.instance_type
}terraform apply -var="instance_type=t3.small"output "instance_ip" {
value = aws_instance.web.public_ip
}terraform output instance_ipresource "aws_s3_bucket" "example" {
for_each = toset(["bucket1", "bucket2", "bucket3"])
bucket = each.key
}variable "env" {}
resource "aws_instance" "example" {
instance_type = var.env == "prod" ? "t3.large" : "t2.micro"
}mkdir -p modules/vpc# modules/vpc/main.tf
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}# Root module
module "vpc" {
source = "./modules/vpc"
}terraform init
terraform applyterraform workspace new dev
terraform workspace new prod
terraform workspace select prod
terraform workspace listexport TF_LOG=DEBUG # Enable debug logs
terraform apply 2>&1 | tee debug.log # Save logsThis README covers all the Terraform commands needed for your "Terraform in One Shot" video. Let me know if you need modifications or extra details! 🚀