Creating VPC and EC2 on AWS for Free Tier With Terraform
/ 6 min read
Table of Contents
Overview
When experimenting with cloud infrastructure or establishing lightweight personal relays, staying strictly within the AWS Free Tier boundaries avoids surprise monthly billing.
Terraform enables declaring these baseline foundational resources—a Virtual Private Cloud (VPC), multi-AZ public and private subnets, internet gateways, and a micro compute instance—in modular code that can be created and destroyed cleanly on demand.
The baseline topology includes:
- VPC:
10.10.0.0/16CIDR block. - Subnets: Public subnets (
10.10.20.0/24ineu-central-1a,10.10.40.0/24ineu-central-1b) and private subnets (10.10.21.0/24,10.10.41.0/24). - Network Ingress/Egress: Security groups scoped for SSH and HTTP/HTTPS ingress.
- Compute: A single
t2.microEC2 instance with 25 GB gp3 root storage and an attached Elastic IP.
Pre-Requirements
- An AWS account.
- A Linux or macOS computer to run Terraform.
- Git
Install Terraform
You can skip this section if you already have Terraform installed on your computer.
If you are reading this tutorial, you know what Terraform is. Terraform is an open-source Infrastructure as Code (IaC) tool that enables users to define and provision infrastructure efficiently across various cloud providers.
Installation on MacOS
If you have Homebrew installed on your computer, you can use brew to
install Terraform.
Open a terminal and run these commands:
brew tap hashicorp/tap && \brew install hashicorp/tap/terraformOnce the installation is finished, you can check the Terraform version and verify the installation.
terraform --versionInstallation on Windows
You can head to the official Terraform installation document and download the latest Terraform version.
But my recommendation is to use Linux with WSL.
Installation on Linux
You can head to the official Terraform installation document and click on the tab for your distro.
If you are using Arch Linux and have yay tool installed , you can
simply install Terraform with the commands below.
yay -S terraformCreate an AWS Service Account for Terraform
If you have a service account for the terraform, you can skip this step.
To create a service account, you can follow the tutorial below step by step.
Creating a Service Account on AWS For Terraform
Overview For Terraform to create resources on your AWS account, it should get some access keys to authenticate to your account. In this step, we will create a service account for Terraform to use. We will create an admin user group, then create a Terraform user and add it to
Create an SSH Key For Linux Server Authentication
You can skip this section if you already have an SSH key to use on a Linux server.
To enhance Linux server security, generate an SSH key pair using the
ssh-keygen command, enabling secure authentication by pairing a
private key on the client with the server’s authorized public key.
A standard SSH keypair consists of a private signing key and a public authorized key. The public key is embedded into the Terraform EC2 resource definition, while the private key is retained locally to authenticate SSH sessions.
To generate a dedicated ed25519 keypair:
ssh-keygen -t ed25519 -f ~/.ssh/aws-ec2 -C "contact@burakberkkeskin"Configuring AWS Credentials
To authenticate Terraform against AWS APIs, credentials can be exported as session environment variables or configured in ~/.aws/credentials:
export AWS_ACCESS_KEY_ID="<your-access-key>"export AWS_SECRET_ACCESS_KEY="<your-secret-key>"We could run the export command below directly, but this will save the
secret access keys in the shell history, which is very unsafe. So we
will create a file and put the keys into it.
Create a file named terraform-aws.sh with vim
vim terraform-aws.shCopy this template and paste it into the file.
#!/bin/bashexport AWS_ACCESS_KEY_ID=YourAccessKeyIdexport AWS_SECRET_ACCESS_KEY=YourSecretAccessKeyPress the i button, move the cursor with the arrow buttons on your
keyboard, and edit the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
variables with yours.
Press esc, colon (:) and write wq! and hit enter on your
keyboard to save the changes and exit the editor.
Source the file to export the credentials into the active shell:
source terraform-aws.shClone Sample Terraform Configurations
To initialize the module structure:
cd $HOMEgit clone --depth 1 --branch blog-tutorial https://github.com/burakberkkeskin/personal_infra.git && \cd personal_infra/mainEnsure you are operating inside personal_infra/main before running initialization:
terraform initConfigure and Customize Terraform Variables
Add the SSH Key
Print the public key you created earlier and copy it.
cat ~/.ssh/aws-ec2.pubOpen the variables.auto.tfvars file with a file editor.
vim variables.auto.tfvarsPress the i button, move to a new line with the arrow buttons on your
keyboard, and create a new variable named ec2_public_key like in the
example below:
// general variables project_name = "main" aws_region = "eu-central-1" aws_zone = "eu-central-1a"
ec2_public_key = "ssh-ed25519 SuperSecretKeylZDI1NTE5AAAAINa3hu5qHgD3cS7dxMlO6I3dTTx/AFU5HqhNZnM6rnEJ contact@burakberk.dev"
...Press esc, colon (:) and write wq! and hit enter on your
keyboard to save the changes and exit the editor.
You can also configure other variables as you wish, like the
aws_region, project_name.
Terraform Plan
To preview the resources to be created:
terraform planReviewing the planned resource creations confirms valid configurations.
Terraform Apply
Once ready to provision the resources:
terraform applyEnter yes when prompted. Within 2–3 minutes, AWS allocates the resources:
Apply complete! Resources: 23 added, 0 changed, 0 destroyed.
Outputs:ec2_instance_private_ip = "10.10.20.136"ec2_instance_public_ip = "<public-ip>"In the AWS Console, the EC2 instance reflects in the running state:
SSH Connectivity Verification
Connect to the instance using the public IP and private SSH key:
ssh -i ~/.ssh/aws-ec2 ubuntu@<public-ip>If it is the first time that you are connecting to a server, it will ask
if you are sure to connect. Write yes enter hit Enter.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yesIf you have followed the tutorial until now, congratulations, You are on the Linux server!
You can use this server however you like. For example, you can host a website, make it a VPN server, a database server, or anything else you want.
Deploying a Test Container
To verify network routing, public ingress, and Docker functionality on the provisioned host:
sudo docker run --name "website" --rm -d -p "80:80" nginxVisiting http://<public-ip> in a browser verifies that security group ingress rules and route table associations function correctly.
Tearing Down Resources
To avoid incurring unexpected charges once testing concludes, tear down the allocated AWS infrastructure:
cd ~/personal_infra/mainsource terraform-aws.shterraform destroyConfirming with yes removes all 23 managed resources cleanly:
Destroy complete! Resources: 23 destroyed.