Skip to content

Files

Latest commit

67e0e48 · Oct 9, 2023

History

History
195 lines (147 loc) · 4.58 KB

README.md

File metadata and controls

195 lines (147 loc) · 4.58 KB

Serverless REST API on AWS with FastAPI

Banner

Deployment

This starter kit shows how to deploy a serverless REST API on AWS using:

  • Python
  • Miniconda
  • Poetry
  • FastAPI
  • Serverless framework

This project was inspired by Cox65 with some improvements.

A. Installation

All the following installation steps for the first time only

1. Install Make

  • Make sure to setup make on your OS, if you are not able to setup make, please use commands on the Makefile

2. Install Nodejs and projects node_modules

  • If you havent install Nodejs before please install it.
  • It is recommended to install LTS versions.
  • Note: the project is using Nodejs v16.20.2 but any Nodejs newer versions should be fine.

When you have Nodejs installed, let install the node_modules:

npm i

Or if you have make:

make install-serverless

3. Install Python and packages

  • If you havent install Python before please install it.
  • Note: the project is using Python3.9 but any Python3 newer versions should be fine.

4. Install Miniconda

  • If you are using Linux:
curl -sL \
  "https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh" > \
  "Miniconda3.sh"
bash Miniconda3.sh
  • If you are using MacOS:
curl -sL \
  "https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh" > \
  "Miniconda3.sh"
bash Miniconda3.sh
  • If you are using Windows:

Go to this page: https://repo.anaconda.com/miniconda/ and download and install the latest .exe package

Note: when install miniconda, please read carefully its instruction, it is pretty straight forward

5. Install new conda environment

conda env create -f environment.yml

6. Activate the new conda environment

conda activate serverless_env

6. Install Python packages

poetry install

or if you have make:

make install-python-requirements

Now its time to go to the next steps!

B. Local development

  1. If you havent activated your conda environment, let's activate it:
conda activate serverless_env
  1. Run the following command to start local server
npx sls offline

or if you have make:

make serve
  1. Testing:
curl localhost:3000/dev/health

Or you can visit this link using your browser.

It should return:

{"status":"OK"}

C. Deployment

1. Deploy locally

a. Pre-deployment
  • If you have a Serverless account

Please use code on main branch which is default:

git checkout main
git checkout -b your_branch

After that, open the serverless.yaml file, change org and app to your org and app respectively!

Note: Read more about Setup an Serverless account here

And run the following command:

npx serverless login

Then you will be able to login using your browsers.

If you havent created an app before, please go to your Serverless dashboard and create a new project, it must have same name as your app name (on the serverless.yaml file):

In this example:

  • If you do not have Serverless account:
git checkout without_serverless_account
git checkout -b your_branch

And then go to the Deployment step.

b. Deployment

Note: If you havent activated your conda environment, let's activate it:

conda activate serverless_env

Run the following command to deploy

make deploy

Note the endpoint so it can be used later.

c. Post-Deployment

Run the following command to test your endpoint (the default path is health):

curl your-serverless-endpoint

Here your-serverless-endpoint is the endpoint that you can get from the Deployment step above.

D. Remove app:

Note: If you havent activated your conda environment, let's activate it:

conda activate serverless_env

If you want to remove your app, run the following command:

npx sls remove

Or if you have make:

make remove