Install Apache 2 & Print the IP Address of the Instance in a File on Local: Terraform Assignment 5
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
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.
Step 2: Type “yes” to destroy the resources.
Step 3: The “assignment-4” instance will be terminated successfully.
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
Step 2: Paste this script here:
#!/bin/bash
sudo apt-get update -y
sudo apt-get install apache2 –y
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
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
}
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
Step 4: Add the plan using the command: terraform plan
Step 5: Create the infrastructure using the command: terraform apply.
Step 6: Type “yes” to create infrastructure.
Step 7: Go to “Instances” & an instance named “assignment-5” has been successfully created with the public IP address (3.15.147.219).
Step 8: When we click on “Open Address” in “Public IP Address”. An apache2 page will be successfully 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