๐ Project: Deploy a Red Hat EC2 Instance on AWS Using Terraform

2 min read
Goal: Use Terraform to provision an EC2 instance running Red Hat Linux with secure SSH access, proper tagging, and reusable configuration.
๐งฑ Project Structure
terraform-ec2-redhat/
โ
โโโ main.tf # Main infrastructure definition
โโโ variables.tf # Input variables for flexibility
โโโ outputs.tf # Outputs like public IP
โโโ README.md # (for GitHub/Hashnod
โ Step 1: Prerequisites
AWS account
AWS CLI configured (
aws configure
)Terraform installed (
terraform -v
)Create an EC2 key pair in AWS Console
โ Name:master-slave
(or any name)
๐ main.tf
provider "aws" {
region = var.aws_region
}
resource "aws_instance" "redhat_master" {
ami = var.ami_id / direct ami_id
instance_type = var.instance_type / direct instance_type
key_name = var.key_name / direct key_name
tags = {
Name = "RedHatMaster"
Environment = "Dev"
Owner = "Harshitha"
}
}
if required you can use variables.tf
๐ variables.tf
variable "aws_region" {
description = "AWS region to deploy the instance"
default = "ap-south-1"
}
variable "ami_id" {
description = "Red Hat AMI ID for the region"
default = "ami-0" # Example: Red Hat in ap-south-1
}
variable "instance_type" {
description = "EC2 instance type"
default = "t2.micro"
}
variable "key_name" {
description = "Name of the EC2 Key Pair"
default = "master-slave" # Replace with your actual key pair
}
you can use output.tf file to view public key and id
๐ outputs.tf
output "instance_public_ip" {
description = "Public IP of the EC2 instance"
value = aws_instance.redhat_master.public_ip
}
output "instance_id" {
description = "ID of the created EC2 instance"
value = aws_instance.redhat_master.id
}
โ Step 3: Initialize Terraform
terraform init
โ Step 4: Preview with Plan
terraform plan
โ Step 5: Apply Configuration
terraform apply
Type yes
when prompted.
โ Step 6: Output the Instance Details
Youโll see:
Outputs:
instance_id = "i-xxxxxxxxxxxxx"
instance_public_ip = "13.xx.xx.xx"
Use that IP to SSH into your instance:
ssh -i "master-slave.pem" ec2-user@xx.xx.xx.xx
โ Step 7: Destroy the Infrastructure (optional)
To remove everything:
terraform destroy
3
Subscribe to my newsletter
Read articles from Harshitha G M directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
