dash serverless deployment to AWS lambda
AWS lambda has different stages, the pathname will mess up the communication between front end and back end. to avoid that, need to set a pathname prefix equal to the environment name corresponding to AWS API gateway
- detailed instruction is from: https://medium.com/@Twistacz/flask-serverless-api-in-aws-lambda-the-easy-way-a445a8805028
-
make sure you already have AWS account and have awscli installed on your machine
-
install serverless
npm install -g serverless
-
create python project and virtual environment. (NOTE: use virtualenv to create virtual environment as the python3 default venv is not working properly with serverless deployment)
# create new dir and jump in $ mkdir quotes $ cd quotes/ # create virtualenv, activate it $ virtualenv venv -p python3 $ . venv/bin/activate.fish # install Flask and freeze requirements $ (venv) pip install Flask $ (venv) pip freeze > requirements.txt
-
write your flask codes
-
create a serverless config yaml file with the following format as example:
service: quotes provider: name: aws runtime: python3.6 stage: dev region: us-east-1 memorySize: 128
-
install two additional sls plugins.
$ sls plugin install -n serverless-wsgi $ sls plugin install -n serverless-python-requirements
-
test app locally with
sls wsgi serve
-
if good, deploy with
sls deploy
Additional documentation on serverless can be found at https://serverless.com/framework/docs/
- use deploy function or package for existing project for speedier deployment
or
sls deploy function --function myFunction
sls deploy --package path-to-package