Companion lab for the Medium article Creating Your First Instance with Terraform. Part of my Terraform fundamentals series — more on Medium.
How to provision your first AWS EC2 instance with Terraform from scratch — understanding providers, input variables, local state, and the plan → apply → destroy resource lifecycle.
┌──────────────────┐ ┌──────────────────────┐
│ Terraform │ │ AWS │
│ (local state) │ ────► │ EC2 t3.small │
│ │ │ region: us-east-1 │
└──────────────────┘ └──────────────────────┘
| File | Purpose |
|---|---|
provider.tf |
Declares the AWS provider (v5.31.0) and region |
variables.tf |
Defines region and vm_name input variables |
main.tf |
Creates the EC2 instance resource with tags |
backend.tf |
Configures local state backend |
- Terraform ≥ 1.5
- AWS CLI configured (
aws configure) with adefaultprofile - An AWS account (free tier covers this lab)
# Initialize providers and backend
terraform init
# Preview what will be created
terraform plan
# Provision the instance
terraform applyAfter apply, a t3.small EC2 instance named vm01 will exist in us-east-1, tagged with Terraform = true.
terraform destroyAlways destroy lab resources — EC2 instances accrue hourly charges.
- Providers — how Terraform talks to cloud APIs (
hashicorp/aws) - Input variables — parameterize your infrastructure (
var.region,var.vm_name) - State — Terraform's source of truth for what exists (local backend here)
- Resource lifecycle —
planpreviews,applycreates,destroyremoves
Part of my Terraform fundamentals series:
- Next: Managing Environments with Terraform Workspaces
- Also in series: Using Multiple Providers
All articles on Medium.