Welcome to Day 3 of my Terraform learning journey! Today, I will walk through deploying your first server on AWS using Terraform. This guide is designed for beginners, so don’t worry if you’re new to infrastructure as code I've got you covered.
Why Terraform?
Terraform simplifies infrastructure management by using code to provision and manage resources. Instead of manually creating servers in the AWS console, Terraform automates this process, ensuring consistency and repeatability
What We’ll Achieve
By the end of this guide, you’ll have:
- Deployed an AWS EC2 instance running a simple web server.
- Configured the instance with an HTTP server (Apache) using a startup script.
- Accessed your web server via a public IP.
Prerequisites:
- AWS Account: Ensure you have an AWS account.
- Terraform Installed: Make sure you have Terraform installed on your local machine.
- AWS CLI Installed: Optional, but recommended for managing AWS resources.
Step 1: Configure AWS Credentials
Set Up AWS CLI :
Open your terminal and run the following command to configure your AWS credentials:
Enter your AWS Access Key ID, Secret Access Key, region, and output format.
Note: When you are working with AWS using Terraform, you need the Access keys and Secret Access key unlike when you are working on the AWS Console where you require passwords.
To get the Access Keys,
- login into your AWS Console as IAM user
- Go to your name and select security credentials
- Go to access keys and click on create Access key
- Select the use case as Command Line Interface
- Enter the description tag, then go on to create the access key. P.S: Ensure you don't share the access keys with anyone
Step 2: Set Up Your Project
- Create Project Directory
- Create a new directory for your Terraform project:
mkdir terraform_project
cd terraform_project
Step 3: Create Configuration Files
- main.tf: This file will contain the main configuration for your infrastructure.
- variables.tf: This file is for defining variables.
- outputs.tf: This file will contain outputs to display after deployment.
File Structure
terraform_project/
├── main.tf
├── variables.tf
└── outputs.tf
Write Your Terraform Configuration
- main.tf
provider "aws" {
region = "us-east-1"
}
Create a VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
Create a Public Subnet
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
}
Create an Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
}
Create a Route Table for Public Subnet
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
}
}
Associate the Route Table with the Public Subnet
resource "aws_route_table_association" "public_association" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
Create a Security Group
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"]
}
}
Create an EC2 Instance
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0" # Example AMI ID for Amazon Linux 2
instance_type = "t2.micro"
subnet_id = aws_subnet.public.id
security_groups = [aws_security_group.allow_ssh.name]
user_data = <<-EOF
#!/bin/bash
echo "Hello, World!" > /var/www/html/index.html
apt-get update
apt-get install -y apache2
systemctl start apache2
systemctl enable apache2
EOF
tags = {
Name = "WebServer"
}
}
Step 4: Deploy The Server
Initialize Terraform
Plan the deployment
Apply The Configuration
When successfully created ;
Step 5: Test the Deployment
- Copy the public IP of your EC2 instance.
- Open a browser and visit:
You should see "Hello, World" displayed on the page.
Congratulations !
You've successfully used Terraform to create EC2 instance, and also access the Apache Server through the IP Address.
Top comments (0)