Terraform Basics for AWS Infrastructure as Code
In this video, you will learn how to use Terraform to define and deploy AWS cloud infrastructure as reliable, reusable code.
What this guide covers
After reading this, you’ll know how to define AWS infrastructure using Terraform configuration files, run Terraform commands to preview and provision your setup, and keep your environments consistent without clicking through the AWS Management Console.
When to use it
- Setting up a new AWS environment reliably without manual console clicks
- Reproducing or scaling existing infrastructure on AWS with the same configuration
- Avoiding configuration drift caused by manual changes over time
- Automating frequent infrastructure changes in a version-controlled way
The move, step by step
- Install Terraform: Download and install Terraform from https://www.terraform.io/downloads. Verify installation with
terraform version. - Create a working directory: Make a folder for your Terraform configs and navigate into it in your terminal.
- Write a resource block: Define AWS resources in a
.tffile using HCL syntax. Example for EC2:resource "aws_instance" "web" { ami = "ami-123456" instance_type = "t2.micro" } - Initialize Terraform: Run
terraform initto download AWS provider plugins and prepare your directory. - Preview changes: Use
terraform planto see what Terraform will create or change without applying it yet. - Apply your config: Run
terraform applyto provision resources described in your config files. Confirm withyeswhen prompted. - Check state: Terraform maintains state locally or remotely. This tracks your deployed infrastructure and enables consistent updates.
- Update safely: Change your
.tffiles to adjust infrastructure, then runterraform planandterraform applyagain to update AWS resources incrementally.
Example
Input config file (main.tf):
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI (example)
instance_type = "t2.micro"
}
Commands:
terraform init
terraform plan
terraform apply
Expected output snippet from terraform plan:
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.web will be created
+ resource "aws_instance" "web" {
+ ami = "ami-0c55b159cbfafe1f0"
+ instance_type = "t2.micro"
...
}
Common mistakes
- Mistake: Skipping
terraform init→ Fix: Always initialize your working directory before planning or applying. - Mistake: Running
terraform applywithout review → Fix: Useterraform planfirst to understand changes. - Mistake: Hardcoding AMI IDs without region context → Fix: Parameterize AMI or use data sources for portability (official Terraform docs).
- Mistake: Manually changing AWS resources outside Terraform → Fix: Avoid drift by making all changes through Terraform, so state stays accurate.
- Mistake: Not storing Terraform state remotely for team environments → Fix: Use remote state backends like S3 with locking to collaborate safely.
Next step
Create a simple Terraform file defining one AWS resource (an EC2 instance or S3 bucket), run terraform init and terraform plan today. Review the plan output. Then come back and try the next move from the video.
Pick the smallest version of this guide and try it in your tool of choice in the next 20 minutes.
Get the next AI/career guide in your inbox
One short, practical guide on AI tools, cloud, and the modern career stack. No fluff.