Skip to content

Weird-Sheep-Labs/fastapi-cdk-demo

Repository files navigation

fastapi-cdk-demo

Demo FastAPI application deployed with CDK

by Weird Sheep Labs

Overview 📖

The purpose of this repo is to serve as a simple example of how one can deploy a serverless FastAPI application to AWS using CDK. The basic architecture of the application is shown below:

Figure 1

API 🌐

There is nothing special or complex about the API itself, users can interact with a Song model via three endpoints that are exposed:

  • Create
  • List
  • Delete (all)

DynamoDB is used for persistence. The boto3 DynamoDB API is extrememly verbose, so Dyntastic is used to handle DB operations and define the data model due to its support for Pydantic/FastAPI.

IaC 🧱

All infrastructure code is written in Python 3.11 and defined in one stack, FastApiCdkDemoStack.

Lambda application dependencies are bundled using the BundlingOptions CDK interface along the lines of the example in the documentation.

Setup ⚙️

Dependency management is handled by Poetry. To install the project, simply run:

poetry install

The docker-compose.yml file defines a Docker container that runs dynamodb-local so that the application can be run locally. To spin up the container, first define the following environment variables:

DYNAMODB_HOST=http://localhost:8000
DYNAMODB_SONG_TABLE_NAME=Song

and then run:

docker-compose up

This will run DynamoDB locally on port 8000 (you can change this in the docker-compose.yml file). Then start the FastAPI dev server, making sure to specify a different port to the one in use by DynamoDB:

cd api/v1
uvicorn main:app --reload --port 8001

Note

You will need to have AWS API access configured to access the /songs endpoints, even during local development. This is because of the Dyntastic DDB wrapper.

Deployment 🚀

Configuring the AWS deployment environment requires specifying the following environment variables:

CDK_DEPLOY_ACCOUNT=<aws_account_number>
CDK_DEPLOY_REGION=<aws_region>

Otherwise, the default account/region will be used. Then, deploy with:

cdk deploy

About

Demo FastAPI application deployed with CDK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages