Terraform Essentials – Study Notes
1. Terraform Installation & Setup
- Terraform is a single binary executable.
- Download from: terraform.io
- Installation:
- Just copy the binary to your system path.
- Run 'terraform version' to verify.
- Supported OS: Windows, Mac, Linux
- This course uses Linux and Terraform v0.13
2. What Is a Resource?
- A resource is any object Terraform manages:
- Local files
- Cloud services (EC2, S3, IAM, GCP, Azure, etc.)
- Managed through HCL (.tf) configuration files.
3. Understanding HCL (HashiCorp Configuration Language)
HCL Basics:
resource "local_file" "pet" {
filename = "/root/pets.txt"
content = "We love pets"
}
Key Terms:
- 'resource': Type of block used to define infrastructure
- 'local_file': Resource type (provider: local, type: file)
- 'pet': Logical resource name
- 'filename': Required argument (file path)
- 'content': Required argument (file content)
4. Terraform Workflow
1. terraform init - Initializes Terraform in the current directory
2. terraform plan - Shows what Terraform will do (preview)
3. terraform apply - Applies changes (creates or modifies resources)
4. terraform show - Displays current state
5. Example Resources
Local File:
resource "local_file" "pet" {
filename = "/root/pets.txt"
content = "We love pets"
}
AWS EC2 Instance:
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
AWS S3 Bucket:
resource "aws_s3_bucket" "data" {
bucket = "my-data-bucket"
acl = "private"
}
6. Providers and Arguments
- A provider is a plugin to manage a platform (e.g., AWS, local, Azure).
- Each resource type has required and optional arguments.
- Refer to Terraform Documentation for complete details.
7. Updating Resources
- Edit .tf file (e.g., add file_permission = "0700").
- Run 'terraform plan' → Shows change (-+ means replace).
- Run 'terraform apply' to update.
- Some updates force resource replacement (immutable infrastructure concept).
8. Destroying Resources
- Command: 'terraform destroy'
- Shows plan with '-' (means destroy).
- Confirm by typing 'yes'.
- Deletes all resources in the current config.
Summary:
- terraform init → Init project
- terraform plan → Show plan
- terraform apply → Apply changes
- terraform show → View state
- terraform destroy → Destroy infra