I'm experimenting in putting my FastAPI on AWS lambda. Unfortunately the PhotoScavenger API is too big and exceeds the 250MB filesize accepted by lambda. Other than that this repo is very easy to use in launching a simple backand with FastAPI. That's why I'm saving the code.
How to run:
- cd into the api folder
- run the ./package_for_lambda.sh command this packages all the requirements up in a .zip file for Lambda
- cd into the crud-infra folder and run: cdk deploy
You may need to use NPM install to install packages or run cdk bootstrap in order to get the environment set up. The installation also assumes you already are logged into the AWS CLI. In case you cannot redeploy check for S3 buckets with config files stored.
- landing: https://ipbanonekkpi5swbgpiq5pqhqy0oalrt.lambda-url.eu-central-1.on.aws
- Docs: https://ipbanonekkpi5swbgpiq5pqhqy0oalrt.lambda-url.eu-central-1.on.aws/docs
The /api
folder contains the Python FastAPI code. Run this shell script to build the code into
a zip (required for CDK to upload to Lambda):
# Only work on UNIX (Mac/Linux) systems!
./package_for_lambda.sh
This should generate a lambda_function.zip
.
The /crud-infra
folder contains the CDK code to deploy all the infrastructure
(Lambda function and DynamoDB table) to your AWS account.
You must have AWS CLI configured, and AWS CDK installed on your machine.
First, install the node modules.
npm install
Then run bootstrap if you never used CDK with your account before.
cdk bootstrap
Now you can use this to deploy.
cdk deploy
This contains the Pytest integration tests you can use to test your endpoint directly. Don't
forget to change your ENDPOINT
to the one you want to use (in api_integration_test.py
).
You can run the test like this (but you have to have pytest
installed):
pytest