Prerequisites

  1. AWS Free Tier Account: https://aws.amazon.com/
  2. Terraform
  3. AWS CLI (Command Line Interface)

Install Terraform on Linux (Ubuntu)

Install Terraform via Snap with Classic Mode:

1
sudo snap install terraform --classic

Check Version:

1
terraform -v

Install AWS CLI on Linux (Ubuntu)

Download AWS CLI:

1
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

Install Unzip:

1
2
sudo apt update
sudo apt install unzip -y

Unzip AWS CLI:

1
unzip awscliv2.zip

Run the Installer:

1
sudo ./aws/install

Check Version:

1
aws --version

Configure AWS

Step 1

Configure AWS Credentials:

1
aws configure

Enter:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region
  • Output format (json)

Step 2

Create a Terraform Project Folder:

1
mkdir terraform-aws-project && cd terraform-aws-project

Step 3

Create Terraform Configuration Use ‘nano’ OR ‘vim’:

1
nano main.tf

Paste this Terraform script:

1
2
3
4
5
6
7
8
9
10
11
12
provider "aws" {
region = "" # input your default region
}

resource "aws_instance" "web" {
ami = "" # input your AWS AMI ID
instance_type = "t2.micro"

tags = {
Name = "TerraformEC2"
}
}

Press Ctrl + X, then Y, then Enter to save.

Explain:

  • provider “aws” → Specifies that we’re using AWS as our cloud provider.
  • region = “” → Sets the AWS region.
  • aws_instance resource → Creates an EC2 instance.
  • t2.micro → A free-tier eligible instance.

Find your own AMI ID

AWS AMI Finder: https://cloud-images.ubuntu.com/locator/ec2/
To check your Ubuntu version, run:

1
lsb_release -a

Step 4

Initialise Terraform and downloads the necessary AWS provider plugin:

1
terraform init

Step 5

Preview what Terraform is about to create in AWS:

1
terraform plan

Step 6

Grant IAM Permissions

  1. https://eu-west-2.console.aws.amazon.com/iam/home#/home
  2. click Users and go to the Permissions tab.
  3. click “Add Permissions” -> “Attach policies directly”.
  4. select the policy “AmazonEC2FullAccess”.

Deploy the EC2 Instance:

1
terraform apply

Step 7 (Optional)

How to Create a New SSH Key

  1. Go to AWS EC2 Console: https://eu-west-2.console.aws.amazon.com/ec2/home?region=eu-west-2#Home:
  2. In the left sidebar, click Key Pairs under Network & Security.
  3. Click Create key pair.
  4. Set the key name (e.g. terraform-key); Key type: RSA; Private key format: .pem
  5. Move the key to your Terraform project directory:
1
mv ~/Downloads/terraform-key.pem ~/terraform-aws-project/
  1. Set correct permissions:
1
chmod 400 ~/terraform-aws-project/terraform-key.pem
  1. Add key name to resource “aws_instance” “web”:
1
nano ~/terraform-aws-project/main.tf
1
key_name = "terraform-key"

Save the file: Ctrl + X → Y → Enter.

Obtain the New Public IP

Run:

1
terraform show | grep "public_ip"

Connect to Your EC2 Instance

If you created an SSH key for AWS, you can connect:

1
ssh -i terraform-key.pem ubuntu@your-ec2-public-ip

Replace your-ec2-public-ip with the instance’s public IP (found in AWS Console under “Instances”).

Troubleshooting SSH Connection

  1. Click on your instance → Security tab → Security Groups.
  2. Click the security group → Inbound Rules → Ensure port 22 (SSH) is open:
1
Type: SSH | Protocol: TCP | Port: 22 | Source: 0.0.0.0/0

If SSH is blocked, edit the inbound rules and allow SSH from 0.0.0.0/0 (or your IP only).

Step 8

What You Can Do Inside Your EC2 Instance

Check OS version:

1
lsb_release -a

Check system architecture:

1
uname -m

Update packages:

1
sudo apt update && sudo apt upgrade -y

Install essential tools:

1
sudo apt install -y curl wget git vim

Exit the SSH Session:

1
exit

This will disconnect you from the EC2 instance.

Step 9 (If Needed)

Destroy Resources

To delete the EC2 instance and avoid charges:

1
terraform destroy