Create an EC2 Instance With an Elastic IP Address: Terraform Assignment 2
Tasks to Be Performed:
1. Destroy the previous deployment
2. Create a new EC2 instance with an Elastic IP
Assignment 2 Github Repository Link:
Problem (1) Solution: Destroy the previous deployment
Here, we have to destroy the resources created in “Terraform Assignment 1”.
Step 1: First, we will destroy the resources using the command: terraform destroy.
Step 2: Type “yes” to destroy.
Step 3: Instance “assignment-1” has been successfully terminated. It will take approximately 30 seconds to destroy.
Problem (2) Solution: Create a new EC2 instance with an Elastic IP
Step 1: Create a main.tf file using the command: sudo nano main.tf
Step 2: Paste this script to create an EC2 Instance with the help of “EIP Address”.
provider "aws" {
region="us-east-2"
access_key="AKIAQRH4ND34WNGRNWOP"
secret_key="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"
}
resource "aws_instance" "assignment2" {
ami = "ami-0b4750268a88e78e0"
instance_type = "t2.micro"
tags = {
Name = "assignment2"
}
}
resource "aws_eip" "eip" {
domain = "vpc"
}
resource "aws_eip_association" "eip_assoc" {
instance_id = aws_instance.assignment2.id
allocation_id = aws_eip.eip.id
}
Do “CTRL+X” to exit & Press “Yes” to save the content. Also, press “enter” from the keyboard for complete exit.
Step 3: Run this command to initialize the terraform: terraform init.
Step 4: Run the “terraform plan” command to execute the plan.
Step 5: Run the “terraform apply” command to create the infrastructure.
Step 6: Type “yes” to create the infrastructure.
Step 7: The instance with “Elastic IP Address” has been successfully created within 30 seconds.
Step 8: Go to the “Instance” section & your instance (assignment2) will be successfully created with Elastic IP Address (18.116.188.17).
More Terraform Assignments:
Creating an Architecture using Terraform on AWS — Case Study Solution
Rename EC2 Instances After Creation — Assignment 3
Create a VPC & Deploy an EC2 Instance Inside It — Assignment 4
Install Apache 2 & Print the IP Address of the Instance in a File on Local - Assignment 5