Skip to content

Commit

Permalink
Merge pull request #96 from demeringo/feat/serverless-deployment
Browse files Browse the repository at this point in the history
Deploy to lambda using serveless framework.
  • Loading branch information
da-ekchajzer authored Jun 16, 2022
2 parents 34b770f + 6fe3498 commit 3f0b0f5
Show file tree
Hide file tree
Showing 8 changed files with 1,162 additions and 322 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ dmypy.json
# IDE
.idea/
.vscode/

# Serverless
.serverless
node_modules
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fastapi = '*'
uvicorn = '*'
pandas = '*'
aiofile = '*'
mangum = "*"

[dev-packages]
mkdocs = '*'
Expand Down
640 changes: 319 additions & 321 deletions Pipfile.lock

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ $ uvicorn boaviztapi.main:app --host=localhost --port 5000

You can run the tests with `pytest`.

### Deploy to AWS as serverless application

Api can be self hosted to your own AWS account using the serverless framework.

```sh
# Install the serverless framework and plugins
npm install -g serverless
npm i
# Authenticate
export AWS_PROFILE=your-own-profile
# Deploy to dev
serverless deploy
```

_Fisrt packaging/deployment may takes a several minutes_

### OpenAPI specification (Swagger)

Once API server is launched API swagger is available at [httsp://localhost:5000/docs](https://localhost:5000/docs).
Expand Down
15 changes: 14 additions & 1 deletion boaviztapi/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import os

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from mangum import Mangum
from boaviztapi import __version__

from boaviztapi.routers.component_router import component_router
from boaviztapi.routers.server_router import server_router
from boaviztapi.routers.cloud_router import cloud_router

app = FastAPI()
# Serverless frameworks adds a 'stage' prefix to the route used to serve applications
# We have to manage it to expose openapi doc on aws and generate proper links.
stage = os.environ.get('STAGE', None)
openapi_prefix = f"/{stage}" if stage else "/"
app = FastAPI(root_path=openapi_prefix) # Here is the magic


app.include_router(server_router)
app.include_router(cloud_router)
Expand All @@ -18,6 +26,7 @@
uvicorn.run('main:app', host='localhost', port=5000, reload=True, debug=True)



@app.on_event("startup")
def my_schema():
openapi_schema = get_openapi(
Expand Down Expand Up @@ -50,4 +59,8 @@ def my_schema():
servers=app.servers,
)
app.openapi_schema = openapi_schema

return app.openapi_schema

# Wrapper for aws/lambda serverless app
handler = Mangum(app)
Loading

0 comments on commit 3f0b0f5

Please sign in to comment.