- What is this?
- Why would I want to do this?
- Some of the important concepts you'd have to consider to deploy a web application
- Other benefits of using SST
- How to run
- How to deploy
- Disclaimer
This is a close to minimal example of how to run locally and how to deploy a FastAPI application with SST v2.
This repository also includes insights on why using SST for FastAPI deployments could be advantageous.
You might be overwhelmed by the numerous ways to deploy Python web applications. SST offers a free, open-source solution that's both easy to use and feature-rich. It simplifies serverless deployments, provides local development tools, and integrates well with AWS services.
ref: https://fastapi.tiangolo.com/deployment/concepts/
- Security - HTTPS
- Running on startup
- Restarts
- Replication (the number of processes running)
- Memory
- Previous steps before starting
Let's go over these concepts and how SST can help us:
- HTTPS is handled for you. You are also provided a very straightforward way to set up custom domains: https://docs.sst.dev/custom-domains
- Running on startup, Restarts, Replication, and Memory management are all handled automatically by AWS Lambda and can be fine-tuned in SST through your
Api
orFunction
construct configurations. Lambda provides built-in logging and monitoring, integrating seamlessly with Amazon CloudWatch, CloudWatch Logs, and AWS CloudTrail. For a comprehensive understanding of these features and how to leverage them effectively, I recommend exploring "AWS Lambda: The Ultimate Guide" available at https://www.serverless.com/aws-lambda.
- SST provides a CLI for setting up reliable CI/CD pipelines. This includes commands for:
- Managing secrets securely
- Validating configurations through typechecking
- Deploying your application to specific AWS accounts, regions, and stages
- These tools streamline the deployment process, ensuring consistency and reliability across different environments.
- For more advanced CI/CD setups, SST integrates well with popular CI/CD platforms like GitHub Actions, GitLab CI, and AWS CodePipeline.
- SST features a local development environment that lets you debug and test your Lambda functions locally.
- Infrastructure as Code (IaC) allows you to manage and provision cloud resources through code. To learn more about IaC, see: (https://aws.amazon.com/what-is/iac/)[https://aws.amazon.com/what-is/iac/].
- SST's constructs are based on the idea of progressive disclosure. This means that the basic configuration for these constructs is simple, easy to understand, and readable. While this example uses a single construct, SST allows you to define and connect multiple infrastructure resources. You can learn more about managing complex infrastructures and their permissions in the official SST documentation on Resource Binding.
- Cost-Effective: Leveraging serverless architecture, SST allows you to pay only for the resources you actually use. This can lead to significant cost savings, especially for applications with variable traffic.
- Reduced Operational Overhead: With SST and serverless, you can focus more on developing your application and less on managing infrastructure, leading to increased productivity.
- Node.js
- Python
- poetry
- npm
- pnpm
- AWS account (with credentials configured in your environment. For setup instructions, please consult Configure the AWS CLI)
After you've installed all the prerequisites, follow these steps to set up and start your local development environment:
-
Install npm dependencies:
pnpm install
-
Install
/app
's Python dependencies with poetry:- Detailed steps:
/app/README.md
- Detailed steps:
-
Start the local development server:
pnpm dev
This will initialize your SST development environment, allowing you to test and debug your FastAPI application locally before deployment.
After your local development environment has been deployed, you will see an ApiEndpoint
output in your terminal. Follow this link to view your application running locally. If you navigate to the /docs
endpoint, you will see the Swagger UI for your application, which provides interactive documentation for your API.
To deploy your application to AWS you can simply run:
pnpm sst deploy --stage <YOUR_STAGE_NAME>
You can also find a close to minimal example of a deployment workflow in the production.yaml file within this repository. This file demonstrates a basic GitHub Actions setup for deploying the application to AWS using SST.
I haven't used this exact setup in production yet. However, I've had some really awesome experiences using SST for TypeScript projects. So while I can't vouch for this specific configuration in a production environment, I'm pretty excited about its potential! :P
If you have experience deploying Python web applications, you may be interested in contributing to research on FastAPI cloud deployments. The creator of FastAPI, @tiangolo, is conducting a survey to gather insights on how developers are deploying FastAPI applications to the cloud.