Install Apache 2 & Print the IP Address of the Instance in a File on Local: Terraform Assignment 5

Visal Tyagi
DevOps-Guides
Published in
4 min readApr 21, 2024

Tasks to Be Performed:

1. Destroy the previous deployments

2. Create a script to install Apache2

3. Run this script on a newly created EC2 instance

4. Print the IP address of the instance in a file on the local once deployed

Terraform Assignment 5
Terraform Assignment 5

Assignment 5 Github Repository Link:

Problem (1) Solution: Destroy the previous deployments

Here, we have to destroy the resources created in Terraform Assignment 4

Step 1: Go to “Master” & run the “terraform destroy” command to destroy the previous deployments.

terraform destroy
terraform destroy

Step 2: Type “yes” to destroy the resources.

Yes
Yes

Step 3: The “assignment-4” instance will be terminated successfully.

assignment-4 terminated
assignment-4 terminated

Problem (2) Solution: Create a script to install Apache2

Step 1: First, we will create a script file to install apache2. Run this command: sudo nano install-apache2.sh

Install apache2
Install apache2

Step 2: Paste this script here:

#!/bin/bash
sudo apt-get update -y
sudo apt-get install apache2 –y
Install Apache2
Install Apache2

Do “CTRL+X” to exit & press “yes” to save the file. Press “enter” from the keyboard to exit the file completely.

Problem (3) & (4) Integrated Solution:

3. Run this script on a newly created EC2 instance

4. Print the IP address of the instance in a file on the local once deployed

Step 1: Create a “.tf” file using the command: sudo nano main.tf

main.tf file
main.tf file

Step 2: Paste this script here to create a new instance & print the IP Address.

provider "aws" {
region = "us-east-2"
access_key = "AKIAQRH4ND34WNGRNWOP"
secret_key = "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"
}

resource "aws_instance" "assignment-5" {
ami = "ami-0b4750268a88e78e0"
instance_type = "t2.micro"
key_name = "Terraform"
user_data = "${file("install-apache2.sh")}"

tags = {
Name = "assignment-5"
}

}
output "IPv4" {
value = aws_instance.assignment-5.public_ip
}
main.tf script file
main.tf script file

Do “CTRL+X” to exit & press “yes” to save the file. Press “enter” from the keyboard to exit the file completely.

Step 3: Initialize the “Terraform” using the command: terraform init

terraform init

Step 4: Add the plan using the command: terraform plan

terraform plan
terraform plan
Plan Added
Plan Added

Step 5: Create the infrastructure using the command: terraform apply.

terraform apply
terraform apply

Step 6: Type “yes” to create infrastructure.

Terraform Assignment 5
yes

Step 7: Go to “Instances” & an instance named “assignment-5” has been successfully created with the public IP address (3.15.147.219).

Assignment-5 Instance Created
Assignment-5 Instance Created

Step 8: When we click on “Open Address” in “Public IP Address”. An apache2 page will be successfully opened.

Apache Page Opened

Other Terraform Resources:

Create an EC2 Instance Using Terraform — Terraform Assignment 1

Create an EC2 Instance With an Elastic IP Address: Terraform Assignment 2

Rename EC2 Instances After Creation — Terraform Assignment 3

Creating an Architecture using Terraform on AWS — Case Study

--

--