Skip to content

AWS EC2 Instance Setup (Terraform)

Infrastructure is managed via Terraform in the terraform/ directory.

What Terraform Creates

Resource Details
EC2 Instance t3.small, Amazon Linux 2023, 30GB gp3
Security Group SSH (22), HTTP (80), HTTPS (443) open
Key Pair ED25519, auto-generated and saved locally
Elastic IP Stable public IP that survives stop/start

Instance Specs

Setting Value
Region us-east-2 (Ohio)
Instance type t3.small (2 vCPU, 2GB RAM)
Storage 30 GiB gp3, 3000 IOPS
Credit specification Standard (no surprise burst charges)
Termination protection Enabled
Metadata V2 only (IMDSv2)
EBS-optimized Enabled

Prerequisites

  1. Install Terraform (v1.5+)
  2. Install AWS CLI
  3. Configure AWS credentials (we use a named profile 2sigma):
    aws configure --profile 2sigma
    # Enter your Access Key ID, Secret Access Key, region: us-east-2, output: json
    

Usage

First Time Setup

cd ai-tutor-backend/terraform

cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars if you want to change any defaults

AWS_PROFILE=2sigma terraform init
AWS_PROFILE=2sigma terraform plan
AWS_PROFILE=2sigma terraform apply

Terraform will output: - public_ip -- your instance's stable IP - ssh_command -- ready-to-use SSH command - app_url -- where your app will be accessible - private_key_file -- path to the generated .pem file

SSH Into the Instance

# Use the output directly
terraform output -raw ssh_command

# Or manually:
ssh -i terraform/ai-tutor-staging.pem ec2-user@<public-ip>

Destroy Everything

AWS_PROFILE=2sigma terraform destroy

Update Infrastructure

Edit the .tf files or terraform.tfvars, then:

AWS_PROFILE=2sigma terraform plan    # Preview changes
AWS_PROFILE=2sigma terraform apply   # Apply changes

Customization

Edit terraform.tfvars to change:

instance_type  = "t3.medium"              # Upgrade instance size
volume_size    = 50                        # More storage
ssh_allowed_cidrs = ["203.0.113.10/32"]   # Restrict SSH to your IP

Estimated Monthly Cost

Item Cost
EC2 t3.small on-demand ~$15
30 GiB gp3 EBS ~$2.40
Elastic IP (while instance runs) $0
S3 backups ~$0.50
Total ~$18/mo

Post-Launch Steps

After terraform apply completes, follow aws-deployment-guide.md starting from "Install Docker" (Section 2).

File Structure

terraform/
  provider.tf              # AWS provider config
  variables.tf             # Input variables with defaults
  main.tf                  # EC2, security group, key pair, Elastic IP
  outputs.tf               # Useful outputs (IP, SSH command)
  terraform.tfvars.example # Example config (copy to terraform.tfvars)
  .gitignore               # Excludes state files, keys, secrets