Create an EC2 Instance Using Terraform — Terraform Assignment 1
Tasks to Be Performed:
1. Create an EC2 service in the default subnet in the Ohio region
GitHub Repository Link for this assignment:
A. Create an EC2 Instance & Install Terraform in the Ohio Region
Step 1: Go to the “Services” section & search “EC2” here. Put the cursor over “EC2” & click on “Instances”.
Step 2: Click on “Launch Instance”.
Step 3: Choose “Name” as “Master” in the “Name and tags” section.
Step 4: Choose “AMI” as “Ubuntu” & “AMI Version” as “Ubuntu Server 20.04 LTS (HVM), SSD Volume Type”.
Step 5: Remain “Instance type” as “t2.micro”.
Step 6: Click on “Create new key pair” in the “Key pair (login)” section.
Step 7: Choose the following options here:
Key pair name: — Terraform
Key pair type: — RSA
Private key file format: — .pem
Click on “Create key pair”.
Step 8: The key pair will be successfully created & downloaded.
Step 9: In “Network Settings”, choose the following options here:
Firewall (security groups): — Create a security group
Enable these options here:
A. Allow SSH Traffic from Anywhere
B. Allow HTTP Traffic from the Internet
Step 10: Click on “Launch Instance”.
Step 11: The instance will be successfully launched & click on “hyperlink”.
Step 12: The instance will be shown in the “Running” State.
Step 13: Select the “Master” instance & click on “Connect”.
Step 14: Click on “Connect” again.
Step 15: The “Master” instance will be successfully launched. Update the machine using the command: sudo apt-get update.
Step 16: Create a script file to install “Terraform” using this command: sudo nano script.sh
Step 17: Paste these commands to run the “Terraform installation”.
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform –y
Do “CTRL+X” to exit & press “Y” to save. Press “enter” from the keyboard to save & exit from the file.
Step 18: Run this script using the command: bash script.sh
Step 19: Run this command to check the “Terraform Version”.
terraform --version
You will get the “Terraform Version” — “v1.7.5”.
on linux_amd64
B. Create an EC2 service in the default subnet in the Ohio region
Step 1: Create a terraform script file using this command: sudo nano main.tf
Step 2: Paste this script here in this file:
provider "aws" {
region = "us-east-2"
access_key = "AKIAQRH4ND34WNGRNWOP"
secret_key = "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"
}
resource "aws_instance" "assignment-1" {
ami = "ami-0b4750268a88e78e0"
instance_type = "t2.micro"
key_name = "Terraform"
tags= {
Name = "assignment-1"
}
}
Do “CTRL+X” to exit & press “Y” to save. Press “enter” from the keyboard to save & exit from the file.
Step 3: Run this command to initialize the terraform: terraform init
Install the necessary plugins to initialize the “terraform”.
Step 4: Run the “terraform plan” command to plan the instance creation.
Step 5: Run this command: “terraform apply” to create the instance in the “Ohio” Region.
The instance will be started creating & take approximately 30 seconds to complete.
Step 6: An instance will be successfully launched in the “Ohio” region in the default subnet.
Read This Also:
Create an EC2 Instance With an Elastic IP Address: Terraform Assignment 2
Rename EC2 Instances After Creation — Terraform Assignment 3
Create a VPC & Deploy an EC2 Instance Inside It — Terraform Assignment 4
Install Apache 2 & Print the IP Address of the Instance in a File on Local — Assignment 5
Creating an Architecture using Terraform on AWS — Terraform Case Study