Crushing the Command Line: How I Used Amazon Q to Build a Smarter FastAPI Scaffolder

KEN MWAURAKEN MWAURA
5 min read

This is a submission for the Amazon Q Developer "Quack The Code" Challenge: Crushing the Command Line.

What I Built

I created the FastAPI Scaffolder CLI – a zero-config generator for production-ready FastAPI apps. It solves common pain points:

  • πŸ•’ Saves over an hour per project setup.

  • πŸ›‘οΈ Bakes in best practices (async DB pools, retry logic, secure defaults).

  • ☁️ Generates Infrastructure as Code (Terraform/Docker for AWS/Local).

With one command, you get:

fastapi-scaffold create myapp \
    --db=postgresql \
    --broker=redis \
    --prod=aws

This generates a directory with all the files you need to get started, including a Dockerfile, docker-compose.yaml, and Terraform files for AWS deployment.

Key Benefits

  • βœ… Simplifies database and configuration setup with auto-generated async SQLAlchemy/Motor integrations.

  • βœ… Production-ready: Includes Terraform, Docker Compose, and monitoring tools.

  • βœ… Flexible and fast: Designed to be less opinionated while still providing a solid foundation.

Existing scaffolding tools like the official FastAPI template often lack production readiness or are too rigid. This tool is built to be both flexible and easy to use.

Demo

The scaffolder supports:

  • πŸ›’οΈ Multiple databases: PostgreSQL, MongoDB, SQLite.

  • πŸ’¬ Message brokers: Redis, RabbitMQ.

  • πŸ“¦ Deployment options: Minimal, Full (AWS ECS), Serverless.

Let me walk you through a typical workflow:

  1. Install the CLI:

     uv pip install scaffold-fastapi
    

Package installation

  1. Run the scaffolder command:

     scaffold-fastapi customer-api --db=postgresql --broker=redis --stack=full
    

    OR just pass the name of the app to see the default options:

     scaffold-fastapi customer-api
    

Choosing options

  1. Generated files: The tool generates a complete project structure:
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── v1/
β”‚   β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ db/
β”‚   β”œβ”€β”€ models/
β”‚   └── main.py
β”œβ”€β”€ tasks/
β”‚   β”œβ”€β”€ celery_app.py
β”‚   └── sample_tasks.py
β”œβ”€β”€ infra/
β”‚   β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ terraform/
β”‚   └── helm/
β”œβ”€β”€ Dockerfile
└── docker-compose.yml

generated files

  1. Run the app:

     cd customer-api
     docker-compose up
    
  2. Access the API: Open your browser and navigate to http://localhost:8000/docs to see the auto-generated API documentation.

  3. Run the Celery worker:

     docker-compose run --rm worker
    
  4. Your FastAPI application is now running with:

  • PostgreSQL database with async connection pool

  • Redis for caching and message brokering

  • Celery worker for background tasks

  • Health check endpoints

  • API documentation at /api/v1/docs

When you're ready to deploy:

cd infra/terraform
terraform init
terraform apply

Scaffold Showcase

Project Structure

The scaffolder itself is organized into modular generators:

scaffold-fastapi/
β”œβ”€β”€ scaffold_fastapi/
β”‚   β”œβ”€β”€ generators/
β”‚   β”‚   β”œβ”€β”€ app.py       # FastAPI application files
β”‚   β”‚   β”œβ”€β”€ celery.py    # Celery task configuration
β”‚   β”‚   β”œβ”€β”€ docker.py    # Docker and docker-compose files
β”‚   β”‚   β”œβ”€β”€ env.py       # Environment variable files
β”‚   β”‚   └── terraform.py # AWS deployment files
β”‚   └── __init__.py
β”œβ”€β”€ cli.py               # CLI entrypoint
└── pyproject.toml
β”œβ”€β”€ README.md
β”œβ”€β”€ LICENSE

Code Repository

The complete code is available on GitHub: scaffold-fastapi. The repository includes detailed instructions and examples of the generated code.

The package is also available on PyPI: scaffold-fastapi.

How I Used Amazon Q Developer

Amazon Q Developer was the secret weapon that made this project possible. Here's how it helped:

Initial Concept and Planning

  1. Defining the scope: Amazon Q helped identify the most valuable features for a FastAPI scaffolder, such as database support, message broker integration, and deployment options.

  2. Designing the CLI interface: It suggested using Typer for a clean, intuitive command-line experience.

  3. Planning the project structure: Amazon Q provided best practices for organizing FastAPI projects, including separating concerns and using async database connections.

  4. Generating code snippets: It created boilerplate code for components like async database setup, Celery configuration, and Docker files.

Rapid Prototyping

Amazon Q accelerated development by:

  • Implementing the CLI: It generated the core CLI logic with proper argument handling and validation.

  • Creating generator modules: Each component had its own generator function, producing well-structured, idiomatic code.

Debugging and Refinement

Amazon Q was invaluable for:

  • Troubleshooting: It helped resolve Docker Compose networking issues.

  • Code optimization: Suggestions improved efficiency and maintainability.

  • Feature expansion: New features were implemented cleanly with its guidance.

Packaging and Distribution

Amazon Q assisted in:

  • Creating the pyproject.toml file: It generated a well-structured configuration for packaging and dependencies.

  • Setting up the CLI entry point: It provided the necessary boilerplate for Typer to work seamlessly with the package.

  • Writing documentation: It helped draft the README and usage instructions, ensuring clarity for users.

  • Creating a license file: It suggested using the MIT license, which is widely accepted and easy to understand.

  • Creating Github Actions CI/CD workflows: It generated a basic CI/CD pipeline for testing and deploying the scaffolder.

  • Creating a Dockerfile: It generated a Dockerfile for the CLI tool, making it easy to run in a containerized environment.

Find more about the packaging process in the packaging guide.md and packaging-and-dist.md

Lessons Learned

  1. Start with clear requirements: The more specific your initial prompt, the better the results.

  2. Iterative development: Break complex tasks into smaller chunks and refine incrementally.

  3. Collaborative problem-solving: Treat Amazon Q as a pair programming partner, not just a code generator.

Impact and Future Plans

This scaffolder has already saved me countless hours. What used to take a full day now takes minutes. The generated code is robust and follows best practices.

Future Enhancements

  1. Support for more databases like CockroachDB and DynamoDB.

  2. Authentication templates for OAuth, JWT, and API keys.

  3. Testing frameworks with pytest fixtures and example tests.

  4. CI/CD templates for GitHub Actions and AWS CodePipeline.

Conclusion

Amazon Q Developer has transformed how I approach development. It's not just about writing code fasterβ€”it's about writing better code with fewer bugs and better architecture. This FastAPI scaffolder is just one example of how Amazon Q can help developers build smarter tools.

If you're building CLI tools or automation scripts, I highly recommend leveraging Amazon Q Developer. Start with a clear vision, break down the problem, and let Amazon Q help you implement the solution.

I hope you have enjoyed this article. If you have any questions, please feel free to reach out to me on Twitter. or LinkedIn. You can also check out my other articles on Dev.to. Thanks for reading!

0
Subscribe to my newsletter

Read articles from KEN MWAURA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

KEN MWAURA
KEN MWAURA

Nairobi, Kenya