Table of Contents
Introduction
Getting started with Terraform on AWS is one of the most common entry points for infrastructure as code. This guide walks you through the complete setup process, from AWS CLI configuration to deploying your first EC2 instance.
Prerequisites
- An AWS account (free tier works)
- Terraform installed (installation guide)
- AWS CLI installed (installation guide)
Step 1 - Configure AWS Credentials
Create an IAM User
- Go to AWS Console > IAM > Users
- Click “Create User”
- Name:
terraform-admin - Attach policy:
AdministratorAccess(for learning; restrict in production) - Create access key (CLI type)
Configure AWS CLI
aws configure
# AWS Access Key ID: AKIA...
# AWS Secret Access Key: xxxx...
# Default region: us-east-1
# Default output format: json
Verify Access
aws sts get-caller-identity
Step 2 - Create Your First Terraform Config
Project Setup
mkdir my-first-terraform && cd my-first-terraform
providers.tf
terraform {
required_version = ">= 1.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
main.tf
# Get the latest Amazon Linux 2 AMI
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
# Create a security group
resource "aws_security_group" "web" {
name = "web-server-sg"
description = "Allow HTTP and SSH"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Restrict in production!
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Launch an EC2 instance
resource "aws_instance" "web" {
ami = data.aws_ami.amazon_linux.id
instance_type = "t2.micro" # Free tier eligible
vpc_security_group_ids = [aws_security_group.web.id]
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello from Terraform!" > /var/www/html/index.html
EOF
tags = {
Name = "terraform-web-server"
}
}
outputs.tf
output "instance_id" {
value = aws_instance.web.id
}
output "public_ip" {
value = aws_instance.web.public_ip
}
output "public_dns" {
value = aws_instance.web.public_dns
}
Step 3 - Deploy
# Initialize Terraform
terraform init
# Preview changes
terraform plan
# Apply changes
terraform apply
# Type "yes" when prompted
# View outputs
terraform output
Step 4 - Verify
# Check the instance
curl http://$(terraform output -raw public_ip)
# Output: Hello from Terraform!
Step 5 - Clean Up
# Destroy all resources (stop billing)
terraform destroy
# Type "yes" when prompted
Next Steps
After your first deployment:
- Add remote state with S3 backend
- Use variables for reusability
- Create modules for common patterns
- Set up CI/CD with GitLab or GitHub Actions
- Explore more AWS services (VPC, RDS, ECS)
Hands-On Courses
- Terraform for Beginners — Full AWS projects
- Terraform Beginners on CopyPasteLearn
Conclusion
You’ve just deployed your first AWS infrastructure with Terraform. This is the foundation for managing everything from simple web servers to complex multi-service architectures. Keep exploring and automating!

