This document describes how to deploy the API on AWS Lambda. If you do not want to use AWS S3, you can skip the final section about linking the API to AWS S3.
Prerequisites:
- Python 3.9/3.10 on your local machine (AWS Lambda runs on Python 3.9/3.10)
- poetry installed (https://python-poetry.org/)
- AWS Account (https://aws.amazon.com/)
- AWS CLI installed (https://aws.amazon.com/cli/). Alternatively you can do everything manually on the AWS console.
Create a Lambda function on your AWS account to run the API. You can use the console to create your lambda function. This will automatically link a AWSLambdaBasicExecutionRole to it.
After creation, alter the following for your lambda function:
- Set Runtime to
Python 3.10
(Code > Runtime Settings) - Set the Runtime Handler to
app.main.handler
(Code > Runtime Settings) - Enable function URL with Auth type
NONE
(Configuration > Function URL)- Explanation: authentication will be done by FastAPI
On the lambda console of your function, head over to 'Configuration -> Environment variables' and add the following:
- set
API_KEY_READ_ACCESS
to<a_random_string>
- set
API_KEY_WRITE_ACCESS
to<a_different_random_string>
- set
USE_LAMBDA_DEV_SETTINGS
totrue
Note: The LAMBDA_DEV_SETTINGS
are not very restrictive on security.
You should disable this when deploying for production.
Create a python 3.10 environment:
poetry env use 3.10
Then install the dependencies:
poetry install
Since in-project = true
is configured in poetry.toml
, this will create a virtual environment within the project folder.
From the root of the project, run:
./lambda_zip.sh
This script will zip the whole project into package.zip
in the root of the project.
From the root of the project, run:
./lambda_deploy.sh
- Note: This script assumes you have a lambda function on AWS with the same name as the current directory.
Alternatively, you can use the AWS console to upload the zip file.
You can now test your API on AWS Lambda. The URL of your API is available on the lambda console under 'Configuration > Function URL'.
Visit https://<your_function_url>/docs
to see the Swagger page of your API.
Create a S3 Bucket on your AWS account to store the json files in. Either use the console or AWS CLI:
aws s3 mb s3://<your_unique_bucket_name>
On the lambda console of your function, head over to 'Configuration -> Environment variables' and add the following:
- set
S3_BUCKET_NAME
to<your_unique_bucket_name>
- set
LOCAL_ASSETS_DIR
to/tmp
Next, head to 'Configuration > Permissions' and click the link to the IAM role.
Attach the following S3 policy to the role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::<your_unique_bucket_name>/assets",
"arn:aws:s3:::<your_unique_bucket_name>/assets/*"
]
}
]
}
Your API now has read/write access to your S3 bucket. You can now test your API on AWS Lambda. The URL of your API is available on the lambda console under 'Configuration > Function URL'.
Visit https://<your_function_url>/docs
to see the Swagger page of your API.