Create an EC2 Instance Using Terraform — Terraform Assignment 1

Visal Tyagi
DevOps-Guides
Published in
5 min readApr 7, 2024

Tasks to Be Performed:

1. Create an EC2 service in the default subnet in the Ohio region

GitHub Repository Link for this assignment:

EC2 Creation Using Terraform
EC2 Creation Using Terraform

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”.

EC2
EC2

Step 2: Click on “Launch Instance”.

Launch Instance
Launch Instance

Step 3: Choose “Name” as “Master” in the “Name and tags” section.

Choose Instance Name

Step 4: Choose “AMI” as “Ubuntu” & “AMI Version” as “Ubuntu Server 20.04 LTS (HVM), SSD Volume Type”.

Ubuntu AMI
Ubuntu AMI

Step 5: Remain “Instance type” as “t2.micro”.

t2.micro

Step 6: Click on “Create new key pair” in the “Key pair (login)” section.

Create new key pair
Create a new key pair

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”.

Create Key Pair
Create Key Pair

Step 8: The key pair will be successfully created & downloaded.

Key Pair Downloaded
Key Pair 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

Network Settings
Network Settings

Step 10: Click on “Launch Instance”.

Launch Instance

Step 11: The instance will be successfully launched & click on “hyperlink”.

Hyperlink
Hyperlink

Step 12: The instance will be shown in the “Running” State.

Running State
Running State

Step 13: Select the “Master” instance & click on “Connect”.

Master
Master

Step 14: Click on “Connect” again.

Connect EC2 Instance
Connect EC2 Instance

Step 15: The “Master” instance will be successfully launched. Update the machine using the command: sudo apt-get update.

Update EC2 Machine
Update EC2 Machine

Step 16: Create a script file to install “Terraform” using this command: sudo nano script.sh

script.sh
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
Paste Terraform Installation Commands
Paste Terraform Installation Commands

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

script.sh file
script.sh file

Step 19: Run this command to check the “Terraform Version”.

terraform --version

You will get the “Terraform Version” — “v1.7.5”.

on linux_amd64

Terraform Version
Terraform Version

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

main.tf file
main.tf file

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"
}
}
Terraform Script

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”.

terraform init
terraform init

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

terraform plan
terraform plan
terraform plan created
terraform plan created

Step 5: Run this command: “terraform apply” to create the instance in the “Ohio” Region.

terraform apply
terraform apply

The instance will be started creating & take approximately 30 seconds to complete.

Instance created
Instance created

Step 6: An instance will be successfully launched in the “Ohio” region in the default subnet.

Instance Created
Instance Created

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

--

--