Skip to content

RESTful API that self-documents in Swagger leveraging AWS CDK, fastAPI, and Docker to build an API

License

Notifications You must be signed in to change notification settings

blake-enyart/restful_api_gateway

Repository files navigation

Self-documenting RESTful API!

You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (restful_api_gateway_stack) which contains a RESTful API consisting of AWS API Gateway linked to AWS Lambda. This system is completely self-documenting via FastAPI and all of this is Dockerized for deployment.

This API was developed based on the following 2 part series:

What is the CDK?

I find that a picture is worth a thousand words, but I look at the CDK as an abstraction on top of CloudFormation/Terraform that allows developers to rapidly build out solutions.

If you would like to learn more about it here is a great link covering this topic.

CDK Conceptual Image

Reference Architecture

Reference Architecture

Project Overview

The cdk.json file tells the CDK Toolkit how to execute your app.

This project is set up like a standard Python project. The initialization process also creates a virtualenv within this project, stored under the .venv directory. To create the virtualenv it assumes that there is a python3 executable in your path with access to the venv package. If for any reason the automatic creation of the virtualenv fails, you can create the virtualenv manually once the init process completes.

To manually create a virtualenv on MacOS and Linux:

$ python3 -m venv .venv

After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.

$ source .venv/bin/activate

If you are a Windows platform, you would activate the virtualenv like this:

% .venv\Scripts\activate.bat

Once the virtualenv is activated, you can install the required dependencies.

$ pip install -r requirements.txt
$ poetry install

Configure pre-commit hooks prior to development:

$ inv install-hooks

Note: this workflow will now looks something like:

  • git add <file>
  • git commit
  • git add . -- if there are code corrections
  • git cz -- to make a descriptive commit to the repo

At this point you can now synthesize the CloudFormation template for this code.

$ cdk synth

For local testing, you must be in the api directory and you can run:

$ uvicorn main:app --reload

This allows for rapid prototyping while in development on your local machine.

You can now begin exploring the source code, contained in the api directory. To add additional dependencies, for example other CDK libraries, just run poetry add <library> command.

When you are ready to deploy the application, ensure your tasks.py is configured with the appropriate AWS profile and run:

$ inv deploy

That's it! You are done and now have a fully functioning RESTful API that is completely self-documenting. Amazing!!!

Useful commands

  • inv ls list all stacks in the app
  • inv synth emits the synthesized CloudFormation template
  • inv deploy deploy this stack to your default AWS account/region
  • inv diff compare deployed stack with current state
  • cdk docs open CDK documentation

Useful endpoints to explore

  • /docs Interactivate documentation built from the code in the api directory
  • /prices/{crypto} Lookup price of a cryptocurrency based on its ticker symbol
  • /welcome Simple endpoint to say hello
  • /trading/{crypto} Consider whether to trade a crypto or not

Enjoy!

About

RESTful API that self-documents in Swagger leveraging AWS CDK, fastAPI, and Docker to build an API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published