Terraform AWS Tutorial: Your First Resource
Hands-on Terraform from zero to applied. The fastest path to understanding what state, plan, apply mean.
Step 1: Install Terraform
brew install terraform (macOS); verify with terraform version.
Have AWS credentials configured (aws CLI works).
Step 2: Write configuration
resource "aws_s3_bucket" "learn" { bucket = "tf-learn-$(uuid)" }- Save as
main.tfin a fresh directory.
Step 3: Plan and apply
The init-plan-apply triplet is the entire Terraform workflow. Every change goes through these three commands; learn them once and the rest is parameters.
- init.
terraform initdownloads the AWS provider and sets up the backend. - plan.
terraform planshows what will change; read it before applying. - apply.
terraform applycreates the bucket; type 'yes' at the prompt to confirm. - State written.
terraform.tfstateappears locally; this is the source of truth Terraform compares against next time.
Step 4: Destroy
Destroy is the inverse of apply. Same plan format, same confirmation, only the diff is negative. Run it when you are done with the lab.
- destroy.
terraform destroytears down everything in state; confirm at the prompt. - Inspect state.
terraform state listshows what Terraform manages; useful before destroy to confirm scope. - Targeted destroy.
terraform destroy -target=aws_s3_bucket.learnremoves one resource without touching the rest. - Cleanup.
rm -rf .terraform terraform.tfstate*when finished; do not commit state files to git.
Antipatterns
- Skipping plan. Apply blindly is dangerous.
- Committing tfstate to git. Use remote state.
- Hard-coded names. Use random_id or variables.
What to do this week
Three moves. (1) Run the tutorial end-to-end on your own laptop / sandbox. (2) Apply the pattern to one production workload. (3) Document the variations you needed; share with the team.