Using Terraform to deploy a Redhat 9 AWS EC2

Building an AWS Redhat Enterprise 9 instance using Terraform is a quick and efficient way to deploy and manage your infrastructure in the cloud. Terraform is a powerful tool that allows you to automate the process of creating, updating, and deleting resources in your AWS environment. In this article, we will go through the steps required to build an AWS Redhat Enterprise 9 instance using Terraform.

Step 1: Setting up your AWS credentials in Terraform

Before you can start building your Redhat Enterprise 9 instance, you must set up your AWS credentials in Terraform. This can be done by creating an AWS access key and secret key and then adding them to your Terraform configuration file or adding them as environment files. Open a terminal and enter the credentials in the following format.

Linux or MAC
export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
export AWS_REGION=”us-east-1″

Windows Powershell
$env:AWS_ACCESS_KEY_ID=”XXXXXXXXXXXXXXXXXXXX”
$env:AWS_SECRET_ACCESS_KEY=”XXXXXXXXXXXXXXXXXXXXXXXXXXXxxxxxxxxxxX”
$env:AWS_REGION=”us-east-1″

Step 2: Create a new Terraform configuration file

The next step is to create a new Terraform configuration file, for example, “main.tf”. This file will contain all the necessary information for creating your Redhat Enterprise 9 instance, creating the security group, and provision a web server on the VM. You can add a variables file and TFVARS file at a later date. I am trying to keep this simple. Create a main.tf with the following code. I am assuming you are using Visual Studio Code IDE.

//Create security Group to open the proper ingress ports
resource "aws_security_group" "main" {
  tags = {
    Name        = "IAC Security Group"
    Environment = "Dev"
    Purpose     = "IAC01"
    Type        = "Terraform"
    GroupTag    = "IAC01 EC2"
  }
  egress = [
    {
      cidr_blocks      = ["0.0.0.0/0", ]
      description      = ""
      from_port        = 0
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      protocol         = "-1"
      security_groups  = []
      self             = false
      to_port          = 0
    }
  ]
  ingress = [
    {
      cidr_blocks      = ["0.0.0.0/0", ]
      description      = "ssh"
      from_port        = 22
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      protocol         = "tcp"
      security_groups  = []
      self             = false
      to_port          = 22
    }
    ,{
      cidr_blocks      = ["0.0.0.0/0", ]
      description      = "https"
      from_port        = 443
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      protocol         = "tcp"
      security_groups  = []
      self             = false
      to_port          = 443
    }
  ,{//This is just to test the default page
      cidr_blocks      = ["0.0.0.0/0", ]
      description      = "http"
      from_port        = 80
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      protocol         = "tcp"
      security_groups  = []
      self             = false
      to_port          = 80
    }
  ]

}


//Primary Resourse for EC2 VM. Using RedHat 9 AMI (HVM-EBS) - Kernel 5.10, SSD Volume Type
// t2.micro 0.0162 per hour. 1V/1G. Running local now.  
resource "aws_instance" "IAC-RH9-01" {

  ami                    = "ami-0176fddd9698c4c3a" //RedHat 9
  instance_type          = "t2.micro" //Free tier
  key_name               = "myexample" //SSH key pair name created in AWS
  vpc_security_group_ids = [aws_security_group.main.id]
  
  //Various Tags
  tags = {
    Name         = "IAC-RH9-01"
    POC          = "Joe Snuffy"
    Environment  = "IAC"
    CreateMethod = "Terraform"
    OS           = "RedHat 9"
    Owner        = "Joe Snuffy Jr"
    Reason       = "IAC Test Server"
    Email        = "info@home127001.com"
    Phone        = "813-555-5555 or 813-666-6666"
  }

  //install apache web server. 
  //of this EC2 instance.  
  provisioner "remote-exec" {
    inline = [
      "sudo dnf -y update",
      "sudo dnf -y install httpd",
      "sudo systemctl start httpd",
      "sudo systemctl enable httpd",
    ]
  }

  //test ssh connection.  Make sure your .PEM file is in the path stated below
  //in the Private_key argument. This TF script is run from any directory.
  //The pem is in the default home/user/.ssh directory. 
  connection {
    type        = "ssh"
    host        = self.public_ip
    user        = "ec2-user"
    private_key = file("~/.ssh/myexample.pem")
    timeout     = "4m"
  }
}


//Run Terraform output public_ip from the terminal whenever you need the Public IP information
output "public_ip" {
  value = "${aws_instance.IAC-RH9-01.public_ip}"
}

output "ssh_login" {
  value = "ssh -i ~/.ssh/myexample.pem ec2-user@${aws_instance.IAC-RH9-01.public_ip}"
}

Step 3: Defining the provider and region

Create a providers.tf file with the following code. This defines the provider for AWS and the region where you want to deploy the Redhat Enterprise 9 instance. The provider to communicates with the AWS API and performs the necessary actions.

//Check provider version. Set Region
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.51.0"
    }
  }
}
provider "aws" {
  region = "us-east-1"
}

Step 4: Defining the EC2 instance resource

Check the AWS AMI EC2 VM image in the main.tf file. Ensure the Redhat Enterprise 9 image ID (ami-0176fddd9698c4c3a), instance type is listed properly.

Step 5: The provisioner block

In order to install the necessary packages and configure the instance, you must add a provisioner block to the EC2 instance resource. This block will contain the commands that will be executed on the instance after it is created. Refer to the “provisioned” block in the main.tf. This block updates the Redhat VM and installs/starts the httpd service. You can remove this code if not needed.

//install apache web server. 
  //of this EC2 instance.  
  provisioner "remote-exec" {
    inline = [
      "sudo dnf -y update",
      "sudo dnf -y install httpd",
      "sudo systemctl start httpd",
      "sudo systemctl enable httpd",
    ]
  }

Step 6: Initializing the Terraform environment

Use the “terraform init” command to initialize the Terraform environment and download the required providers. This will ensure that Terraform has access to all the necessary information and resources to build your Redhat Enterprise 9 instance. Also run Terraform FMT and Terraform validate.

Step 7: Previewing the changes

Before applying the changes, use the “terraform plan” command to preview the changes that will be made to the infrastructure. This allows you to make any necessary adjustments before deploying your Redhat Enterprise 9 instance.

Step 8: Deploying the Redhat Enterprise 9 instance

Once you have reviewed the changes, use the “terraform apply” command to deploy the Redhat Enterprise 9 instance on AWS. This will create the instance, install the necessary packages, and configure it according to your specifications.

That’s it. You may need to make adjustments to the Terraform files to fit your environment. I hope you find this useful.

You will also need an AWS provider.tf file configured properly. Example of a provider file. Or, you can add this information to the top of the main.tf file.

This is the main.tf file. It will create a Security Group and a RedHat 9 EC2 instance. All required information is hard coded. Modify if you like. Information following “//” denotes comments. You can configure a variables.tf and TFVARS file at a later date.

Building an AWS Redhat Enterprise 9 instance using Terraform is a quick and efficient way to deploy and manage your infrastructure in the cloud. By following the steps outlined in this article, you can easily build and configure a Redhat Enterprise 9 instance on AWS without the need for manual intervention. Terraform provides a powerful tool that allows you to automate the process of creating, updating, and deleting resources in your AWS environment and makes it easy to scale and manage your infrastructure.

Leave a Reply

%d bloggers like this: