FastAPI
FastAPI applications on monolayer
This section describes the integration of FastAPI applications on monolayer, including supported platform capabilities, automatic database detection, and custom launch configurations using a Procfile.
Introduction to FastAPI on monolayer
FastAPI is a modern, fast, high-performance web framework designed for building backend APIs with Python. monolayer provides native support for FastAPI and other Python applications, featuring automatic database connection configurations and flexible process launching.
When you deploy a FastAPI application, monolayer automatically sets up your execution environment and configures API access endpoints.
FastAPI Platform Capabilities
Your FastAPI Python application has access to core database and lifecycle functionalities on monolayer. While advanced real-time or task queuing capabilities are currently not natively supported, core API services are fully optimized.
Managed PostgreSQL Database
monolayer automatically provisions a fully managed PostgreSQL database when it detects PostgreSQL usage in your Python project.
- Automatic detection. The system checks your project requirements files (such as
requirements.txtorpyproject.toml) for Python PostgreSQL drivers. - Database provisioning. monolayer provisions a managed PostgreSQL instance inside your AWS account upon driver detection.
- Connection injection. monolayer injects database credentials directly into your environment variables automatically, allowing seamless connection from database toolkits such as SQLAlchemy or SQLModel.
Detection Conditions
monolayer automatically detects your database configuration when your project satisfies the following conditions:
- Dependencies: Your project must include both
sqlalchemyandalembicas dependencies. - PostgreSQL drivers: Your project must define at least one of the following PostgreSQL drivers in your dependencies:
psycopg,psycopg2,asyncpg, orpg8000.
When monolayer detects PostgreSQL usage and an alembic.ini file exists, it automatically creates the following database lifecycle workloads:
- Database creation (
createDatabase):- If your project includes database migrations, the workload runs
alembic upgrade head.
- If your project includes database migrations, the workload runs
- Database migrations (
migrateDatabase):- The workload runs
alembic upgrade head.
- The workload runs
Runtime Environment Variables
At runtime, monolayer automatically injects the following environment variables into your application environment:
DATABASE_URL: The full connection string used to connect to your PostgreSQL database.PGPASSWORD: The secure password for database authentication.PGUSER: The username for your database.PGHOST: The endpoint host address of your database.PGPORT: The port number used to connect to your database instance.PGDATABASE: The name of your PostgreSQL database.
Application Lifecycle Workloads
You can execute custom scripts and tasks during specific phases of your deployment lifecycle. For more information on process configurations, see the Procfile Guide.
- Deployment orchestration. You can run database migrations or prepare data using Procfile-managed commands before or after your web server starts.
- Zero-ops environment. The platform coordinates the initialization of resources so your lifecycle scripts execute in the correct order.
For FastAPI Python applications, you can define custom processes in a root Procfile to run lifecycle workloads when deploying your application.
bootstrap: _your-command_
beforeRollout: _your-command_
afterRollout: _your-command_
createDatabase: _your-command_
migrateDatabase: _your-command_IMPORTANT: If your Python application has a root Procfile, monolayer skips its automatic entrypoint heuristics, which includes database process contributions. You must define createDatabase and migrateDatabase in your Procfile explicitly if you require those processes for a Python application with a Procfile.
Capabilities Roadmap
Support for the remaining monolayer platform capabilities is actively being developed. The following services will be available soon:
- Asynchronous background tasks. Support for offloading background tasks and queuing work asynchronously is coming soon.
- Real-time communications. Native real-time WebSocket support is coming soon.
- Scheduled cron jobs. Running scheduled tasks using cron syntax is coming soon.
- Object storage with buckets. Secure object storage backed by Amazon Simple Storage Service (Amazon S3) is coming soon.