Deploying a FastAPI app on AWS Lambda using AWS SAM
sam build --use-container
sam deploy --guided
#You can find your API Gateway Endpoint URL in the output values displayed after deployment.
# next time
sam build && sam deploy --no-confirm-changeset
Build your application with the `sam build --use-container` command.
sam build --use-container
sam local start-api
curl http://localhost:3000/
# API test
curl -XPOST -H 'Content-Type: application/json' -d '{"id":1, "name":"test"}' 'http://localhost:3000/items/create'
# response {"item_id":1,"name":"test"}
curl 'http://localhost:3000/items/42?q=test'
#Response: {"item_id":42,"q":"test"
sam logs -n FastAPIFunction --stack-name "fastapi-on-aws-lambda" --tail
Tests are defined in the `tests` folder in this project. Use PIP to install the test dependencies and run tests.
pip install -r tests/requirements.txt --user
# unit test
python -m pytest tests/unit -v
# integration test, requiring deploying the stack first.
# Create the env variable AWS_SAM_STACK_NAME with the name of the stack we are testing
AWS_SAM_STACK_NAME="fastapi-on-aws-lambda" python -m pytest tests/integration -v
To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:
sam delete --stack-name "fastapi-on-aws-lambda"