Rename EC2 Instances After Creation —Terraform Assignment 3
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’
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.
Step 2: Type “yes” to destroy all the resources.
Step 3: All the resources will be destroyed along with “Elastic IP Address.
Step 4: Go to “Instances” & you will notice that “assignment2” has been successfully terminated.
The “Elastic IP addresses” section will be shown empty now.
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
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”.
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.
Step 4: Run the “terraform plan” command to execute the plan.
Step 5: Now, we will run the “terraform apply” command to create the infrastructure or 2 Instances in the separate regions.
Step 6: Type “yes” to create the separate instances successfully.
Step 7: Now, go to the “North Virginia” region, an instance named “hello-virginia” has been successfully created.
Step 8: Now, go to the “Ohio” region, an instance named as “hello-ohio” has been successfully 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