Rename EC2 Instances After Creation —Terraform Assignment 3

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

Tasks to Be Performed:

1. Destroy the previous deployment

2. Create 2 EC2 instances in Ohio and N.Virginia respectively

3. Rename Ohio’s instance to ‘hello-ohio’ and Virginia’s instance to ‘hello-virginia’

Rename the EC2 Instances
Rename the EC2 Instances

Terraform Assignment 3 GitHub Repository Link

Problem (1) Solution: Destroy the previous deployment

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

Step 1: Run the “terraform destroy” command to destroy the resources.

Terraform Destroy Command

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

Type “Yes”
Type “Yes”

Step 3: All the resources will be destroyed along with “Elastic IP Address.

Resources Destroyed
Resources Destroyed

Step 4: Go to “Instances” & you will notice that “assignment2” has been successfully terminated.

The “Elastic IP addresses” section will be shown empty now.

Instance Terminated
Instance Terminated

Problem (2) & (3) Integrated Solution:

2. Create 2 EC2 instances in Ohio and N.Virginia respectively

3. Rename Ohio’s instance to ‘hello-ohio’ and Virginia’s instance to ‘hello-virginia’

Step 1: Go to the “Master” instance & create a “main.tf” file to run the script. Use this command: sudo nano main.tf

Create main.tf file
Create main.tf file

Step 2: Paste this script here:

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

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

resource "aws_instance" "assignment3-1" {
provider = aws.North-Virginia
ami = "ami-0d7a109bf30624c99"
instance_type = "t2.micro"
key_name = "Terraform-NV"

tags = {
Name = "hello-virginia"
}
}

resource "aws_instance" "assignment3-2" {
provider = aws.Ohio
ami = "ami-0b4750268a88e78e0"
instance_type = "t2.micro"
key_name = "Terraform"

tags = {
Name = "hello-ohio"
}
}

Note: “Key Pair” & “AMI ID” will different.

In the “Name” section, put “hello-virginia” & “hello-ohio”.

Terraform Script (main.tf file)
Terraform Script (main.tf file)

Do “CTRL+X” to exit & Press “Y” to save the file. Press “enter” to exit the file completely.

Step 3: Run the “terraform init” command for initializing the terraform.

terraform init
terraform init

Step 4: Run the “terraform plan” command to execute the plan.

terraform plan
terraform plan
Plan Added Successfully
Plan Added Successfully

Step 5: Now, we will run the “terraform apply” command to create the infrastructure or 2 Instances in the separate regions.

terraform apply
terraform apply

Step 6: Type “yes” to create the separate instances successfully.

Type yes
Type yes

Step 7: Now, go to the “North Virginia” region, an instance named “hello-virginia” has been successfully created.

hello virginia instance renamed & created
hello-virginia instance renamed & created

Step 8: Now, go to the “Ohio” region, an instance named as “hello-ohio” has been successfully created.

Rename the EC2 Instances
hello-ohio instance renamed & created

Other Terraform Resources:

Create an EC2 Instance Using Terraform

Create a VPC & Deploy an EC2 Instance Inside It

Creating an Architecture using Terraform on AWS

Install Apache 2 & Print the IP Address of the Instance in a File on Local

--

--