🐍 How to Structure Your Python Projects for AWS Lambda, APIs, and CLI Tools

βœ… Beginner-friendly | πŸ”₯ Hands-on Guide | 🧩 File Structure Examples

When you start working on Python projectsβ€”whether for AWS Lambda, building an API, or creating a CLI toolβ€”it’s important to organize your code in a clean and simple way.

A well-structured project is easy to maintain, scale, and share with others.

In this post, I will show common file and folder structures I personally use in my projects. These structures are helpful for newcomers, junior devs, or even freelancers who want to start clean from the beginning.

🐍 1. Python Project for AWS Lambda (Multiple Files)

When working with AWS Lambda, you often split your code into reusable files. This makes your handler clean and easier to manage.

πŸ“ Folder Structure:

your-lambda-project/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ lambda_function.py       # Main Lambda handler
β”‚   β”œβ”€β”€ config.py                # Environment or API config
β”‚   β”œβ”€β”€ logger.py                # Custom logger setup
β”‚   β”œβ”€β”€ utils.py                 # Helper functions
β”‚   β”œβ”€β”€ services.py              # AWS or API calls
β”‚   β”œβ”€β”€ validator.py             # Input validations
β”‚   β”œβ”€β”€ exceptions.py            # Custom error handling
β”‚   β”œβ”€β”€ constants.py             # Static values
β”‚   └── integration.py           # API/database integration logic
β”œβ”€β”€ requirements.txt             # Dependencies
β”œβ”€β”€ serverless.yml               # Serverless Framework (optional)
β”œβ”€β”€ Dockerfile                   # If you deploy using containers
β”œβ”€β”€ README.md
└── CHANGELOG.md

🐍 2. Python Project for API (Flask or FastAPI)

Building RESTful APIs with Flask or FastAPI? You can group files like this for better readability and code separation.

πŸ“ Folder Structure:

your-api-project/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.py                  # Entry point to run the app
β”‚   β”œβ”€β”€ routes.py                # API routes/endpoints
β”‚   β”œβ”€β”€ controllers.py           # Logic for each route
β”‚   β”œβ”€β”€ services.py              # Handle DB/API calls
β”‚   β”œβ”€β”€ models.py                # Pydantic or DB models
β”‚   β”œβ”€β”€ config.py
β”‚   β”œβ”€β”€ logger.py
β”‚   β”œβ”€β”€ utils.py
β”‚   β”œβ”€β”€ exceptions.py
β”‚   └── constants.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env                         # Environment variables
β”œβ”€β”€ Dockerfile                   # Container support (optional)
β”œβ”€β”€ README.md
└── CHANGELOG.md

🐍 3. Python CLI Tool Project

Python is also great for building command-line tools. You can use argparse or click to handle user inputs.

πŸ“ Folder Structure:

your-cli-tool/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.py                  # CLI entry script
β”‚   β”œβ”€β”€ cli.py                   # Argument parsing or click commands
β”‚   β”œβ”€β”€ commands.py              # CLI command logic
β”‚   β”œβ”€β”€ utils.py
β”‚   β”œβ”€β”€ logger.py
β”‚   β”œβ”€β”€ config.py
β”‚   β”œβ”€β”€ constants.py
β”‚   └── exceptions.py
β”œβ”€β”€ setup.py                     # For making your CLI installable
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ Dockerfile                   # Optional for packaging
β”œβ”€β”€ README.md
└── CHANGELOG.md

πŸ› οΈ Tips for Beginners

  • βœ… Use src/ folder to keep your Python code separate.
  • βœ… Keep your requirements.txt updated.
  • βœ… Always add README.md to explain how your code works.
  • βœ… Group similar logic into their own files (e.g., services, utils).
  • βœ… Add Dockerfile only if needed (for deployment or testing).
  • βœ… If you use Serverless Framework, include serverless.yml.

✨ Final Thoughts

Whether you are a beginner, a junior dev, or a self-learner like meβ€”starting with the right folder structure helps you write better code, collaborate easier, and debug faster.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.