DEV Community

Samuel Udeh
Samuel Udeh

Posted on • Edited on

Deploying Your First Server with Terraform: A Beginner's Guide"

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.

Image description

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:

  1. Deployed an AWS EC2 instance running a simple web server.
  2. Configured the instance with an HTTP server (Apache) using a startup script.
  3. Accessed your web server via a public IP.

Prerequisites:

  1. AWS Account: Ensure you have an AWS account.
  2. Terraform Installed: Make sure you have Terraform installed on your local machine.
  3. 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:

Image description
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,

  1. login into your AWS Console as IAM user
  2. Go to your name and select security credentials
  3. Go to access keys and click on create Access key
  4. Select the use case as Command Line Interface
  5. Enter the description tag, then go on to create the access key. P.S: Ensure you don't share the access keys with anyone

Image description

Step 2: Set Up Your Project

  1. Create Project Directory
  2. Create a new directory for your Terraform project:

mkdir terraform_project
cd terraform_project

Step 3: Create Configuration Files

  1. main.tf: This file will contain the main configuration for your infrastructure.
  2. variables.tf: This file is for defining variables.
  3. 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

  1. 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"
}
}

Image description

Step 4: Deploy The Server

Initialize Terraform

Image description

Plan the deployment

Image description

Apply The Configuration

Image description
Type yes when prompted.

When successfully created ;

Image description

Image description

Step 5: Test the Deployment

  1. Copy the public IP of your EC2 instance.
  2. Open a browser and visit:

Image description

You should see "Hello, World" displayed on the page.

Image description

Congratulations !

You've successfully used Terraform to create EC2 instance, and also access the Apache Server through the IP Address.

Top comments (0)