How to Create a VPC with Terraform - Complete AWS Networking Guide
Build a production-ready AWS VPC with Terraform. Covers subnets, route tables, NAT gateways, security groups, and network ACLs step by step.
AWS
Set up Terraform with AWS from scratch. Covers AWS CLI configuration, provider setup, IAM permissions, and your first EC2 instance deployment step by step.
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.
terraform-adminAdministratorAccess (for learning; restrict in production)aws configure
# AWS Access Key ID: AKIA...
# AWS Secret Access Key: xxxx...
# Default region: us-east-1
# Default output format: jsonaws sts get-caller-identitymkdir my-first-terraform && cd my-first-terraformterraform {
required_version = ">= 1.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}# 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"
}
}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
}# Initialize Terraform
terraform init
# Preview changes
terraform plan
# Apply changes
terraform apply
# Type "yes" when prompted
# View outputs
terraform output# Check the instance
curl http://$(terraform output -raw public_ip)
# Output: Hello from Terraform!# Destroy all resources (stop billing)
terraform destroy
# Type "yes" when promptedAfter your first deployment:
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!
Build a production-ready AWS VPC with Terraform. Covers subnets, route tables, NAT gateways, security groups, and network ACLs step by step.
Deploy an AWS EC2 instance with Terraform step by step. Complete guide with VPC, security groups, key pairs, user data, and production-ready configuration.
Learn to use Terraform data sources to query existing infrastructure. Covers AWS AMI lookup, VPC discovery, AZ listing, and cross-state data access patterns.
Learn the AWS services essential for Terraform — IAM for authentication, S3 for state storage, DynamoDB for state locking. Complete setup guide.