🚀 Deploy a simple Python Flask-based FinTrack personal finance tracker on an AWS EC2 instance using Terraform

Project Solution for Week 1 Terraform Project

Project Spec :

✅ Prerequisites

  • AWS Account with access credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

  • Terraform installed (v1.11+ recommended)

  • GitHub Copilot (optional, for code completion)

  • Basic knowledge of:

    • EC2

    • SSH key pairs

    • Security Groups

    • User data scripts


🧠 Project Structure

fintrack-app/
├── terraform/
│   ├── main.tf
│   ├── outputs.tf
│   └── scripts/
│       └── user_data.sh
├── app/
│   └── (Flask app source)
├── README.md

🛠️ Step-by-Step Guide

Step 1: Fork & Clone the Repo

git clone https://github.com/realops/fintrack.git
cd fintrack

Step 2: Setup Terraform Project

Inside the repo, create a directory:

mkdir -p terraform/scripts
cd terraform

Create main.tf and start by defining:

  • AWS provider

  • SSH key pair resource

  • Security group (open ports 22 & 5001)

  • EC2 instance resource (t2.micro)

  • User data script reference

Step 3: Write Terraform Code (main.tf)

Provider & Key Pair


required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_key_pair" "fintrack_key" {
  key_name   = "fintrack-key"
  public_key = file("~/.ssh/id_rsa.pub")
}

Security Group

resource "aws_security_group" "fintrack_sg" {
  name        = "fintrack-sg"
  description = "Allow SSH and App access"
  ingress = [
    {
      description = "SSH"
      from_port   = 22
      to_port     = 22
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
    },
    {
      description = "App"
      from_port   = 5001
      to_port     = 5001
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
    }
  ]
  egress = [{
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }]
}

EC2 Instance with User Data

resource "aws_instance" "fintrack" {
  ami                    = "ami-xxxxxxxxxxxxxxx" # Replace with latest Ubuntu AMI for us-east-1
  instance_type          = "t2.micro"
  key_name               = aws_key_pair.fintrack_key.key_name
  vpc_security_group_ids = [aws_security_group.fintrack_sg.id]
  user_data              = file("${path.module}/scripts/user_data.sh")

  tags = {
    Name = "FinTrackApp"
  }
}

Step 4: Create user_data.sh in scripts/

You can use the AI-generated script from the video context. Here's a simplified version:

#!/bin/bash

# Update and install required packages
sudo apt-get update -y
sudo apt-get install -y python3 python3-pip python3-venv git

# Create a directory for the application
cd /home/ubuntu
sudo mkdir -p fintrack
sudo chown ubuntu:ubuntu fintrack
cd fintrack

# Clone the repository
git clone https://github.com/gouravshah/fintrack.git .

# Create a virtual environment and activate it
python3 -m venv venv
source venv/bin/activate

# Install application dependencies
pip install -r requirements.txt

# Start the Flask app using Gunicorn
gunicorn app:app --bind 0.0.0.0:5001 --workers 4 --daemon

Replace YOUR_USERNAME with your GitHub username if using your own fork.


Step 5: Output Public DNS (outputs.tf)

output "app_url" {
  value = "http://${aws_instance.fintrack.public_dns}:5001"
  description = "URL of the FinTrack application"
}

Step 6: Run Terraform Commands

terraform init
terraform plan
terraform apply

Use terraform apply -auto-approve for faster testing.

Once deployed, copy the output app_url and open it in your browser 🎉


✅ Bonus Tasks

  • Use S3 as a remote backend for Terraform state.

  • Replace hardcoded AMI with a data source lookup for the latest Ubuntu AMI.

  • Convert the setup into a reusable module with input variables.


🔗 Solutions Repository

👉 https://github.com/gouravshah/fintrack


Start your DevOps Career Journey