Table of contents
Welcome to this detailed guide on using Terraform to build and manage AWS infrastructure.
Terraform is a powerful tool for Infrastructure as Code (IaC), allowing you to define, provision, and manage cloud resources with configuration files.
This guide is designed for new learners, providing a step-by-step approach to creating a Virtual Private Cloud (VPC), subnets, an Internet Gateway (IGW), and launching an EC2 instance with a web server on AWS using Terraform.
Prerequisites
Before we start, ensure you have the following:
An AWS account: Sign up at AWS.
Terraform installed on your machine: Download it from the Terraform website.
AWS CLI configured with your AWS credentials: Follow the AWS CLI configuration guide.
Step 1 : Setting Up Your Provider
First, we need to tell Terraform that we're using AWS and specify the region. This is done in the provider
block.
Terraform Configuration:
provider "aws" {
region = "us-west-2"
}
Step 2 : Create a VPC
A Virtual Private Cloud (VPC) is a virtual network dedicated to your AWS account. We'll create a VPC with a CIDR block of 10.0.0.0/16
.
Terraform Configuration:
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "main-vpc"
}
}
This configuration defines a VPC named main-vpc
with the specified CIDR block.
Step 3 : Create Public and Private Subnets
Subnets are segments within a VPC. We'll create a public subnet with CIDR block 10.0.1.0/24
and a private subnet with CIDR block 10.0.2.0/24
.
Terraform Configuration:
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
tags = {
Name = "public-subnet"
}
}
resource "aws_subnet" "private" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
tags = {
Name = "private-subnet"
}
}
Public Subnet: This subnet allows resources to have a public IP address.
Private Subnet: This subnet does not allow public IP addresses, keeping resources private.
Step 4 : Create an Internet Gateway
An Internet Gateway (IGW) allows communication between your VPC and the internet. We'll create an IGW and attach it to our VPC.
Terraform Configuration:
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main-igw"
}
}
This IGW will enable internet access for our VPC.
Step 5 : Create a Route Table for the Public Subnet
We need a route table for the public subnet to route traffic to the Internet Gateway.
Terraform Configuration:
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "public-route-table"
}
}
resource "aws_route_table_association" "public_association" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
Route Table: Directs traffic to the Internet Gateway.
Route Table Association: Associates the route table with the public subnet.
Step 6 : Launch an EC2 Instance
Now, we'll launch an EC2 instance in the public subnet with a specific Amazon Machine Image (AMI) and instance type. The instance will have a security group allowing SSH access from anywhere and a user data script to install Apache and host a simple website.
Terraform Configuration:
resource "aws_security_group" "allow_ssh" {
vpc_id = aws_vpc.main.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "allow_ssh"
}
}
resource "aws_instance" "web" {
ami = "ami-0557a15b87f6559cf"
instance_type = "t2.micro"
subnet_id = aws_subnet.public.id
security_groups = [aws_security_group.allow_ssh.name]
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
echo "<h1>Hello, World from Terraform!</h1>" | sudo tee /var/www/html/index.html
EOF
tags = {
Name = "web-server"
}
}
Security Group: Allows SSH access to the instance.
EC2 Instance: Launches the instance with the specified AMI, instance type, and user data script to install and start Apache.
Step 7 : Create an Elastic IP
An Elastic IP (EIP) is a static IPv4 address for dynamic cloud computing. We'll create an EIP and associate it with our EC2 instance.
Terraform Configuration:
resource "aws_eip" "eip" {
vpc = true
instance = aws_instance.web.id
tags = {
Name = "web-eip"
}
}
This will allocate an EIP and associate it with our web server.
Step 8 : Verify the Website
After applying the Terraform configuration, open the Elastic IP address in a web browser to verify that the website is hosted successfully. You should see a message saying "Hello, World from Terraform!"
Conclusion
By following this guide, you have successfully created an AWS infrastructure using Terraform. This includes setting up a VPC, creating subnets, attaching an Internet Gateway, configuring a route table, launching an EC2 instance, and assigning an Elastic IP.