Terraform AWS Auto Deploy
Created|Updated
|Post Views:
Prerequisites
- AWS Free Tier Account: https://aws.amazon.com/
- Terraform
- AWS CLI (Command Line Interface)
1
| sudo snap install terraform --classic
|
Check Version:
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:
Run the Installer:
Check Version:
Step 1
Enter:
- AWS Access Key ID
- AWS Secret Access Key
- Default region
- Output format (json)
Step 2
1
| mkdir terraform-aws-project && cd terraform-aws-project
|
Step 3
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:
Step 4
Step 5
Step 6
Grant IAM Permissions
- https://eu-west-2.console.aws.amazon.com/iam/home#/home
- click Users and go to the Permissions tab.
- click “Add Permissions” -> “Attach policies directly”.
- select the policy “AmazonEC2FullAccess”.
Deploy the EC2 Instance:
Step 7 (Optional)
How to Create a New SSH Key
- Go to AWS EC2 Console: https://eu-west-2.console.aws.amazon.com/ec2/home?region=eu-west-2#Home:
- In the left sidebar, click Key Pairs under Network & Security.
- Click Create key pair.
- Set the key name (e.g. terraform-key); Key type: RSA; Private key format: .pem
- Move the key to your Terraform project directory:
1
| mv ~/Downloads/terraform-key.pem ~/terraform-aws-project/
|
- Set correct permissions:
1
| chmod 400 ~/terraform-aws-project/terraform-key.pem
|
- 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
- Click on your instance → Security tab → Security Groups.
- 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:
Check system architecture:
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:
This will disconnect you from the EC2 instance.
Step 9 (If Needed)
Destroy Resources
To delete the EC2 instance and avoid charges:
Terraform AWS Auto Deploy