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

Harshitha G MHarshitha G M
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

Harshitha G M
Harshitha G M