$ [~] sls create --template aws-python --path my-cool-app
$ [~] cd my-cool-app
$ [my-cool-app] sls plugin install --name serverless-python-requirements
$ [my-cool-app] sls plugin install --name serverless-wsgi
Create a file called application.py
:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def main():
return "Hello World!"
if __name__ == "__main__":
app.run()
Update serverless.yml
, adding custom
section and modifying functions
section:
custom:
wsgi:
# flask handler
app: application.app
packRequirements: false
functions:
app:
handler: wsgi.handler
events:
# create an API endpoint
- http: ANY /
- http: 'ANY {proxy+}'
Create a file called requirements.txt
with:
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
Werkzeug==0.12.2
Install those dependencies
$ [my-cool-app] virtualenv . && source bin/activate
(my-cool-app) $ [my-cool-app] pip install -r requirements.txt
Run sls deploy
to deploy our app to AWS:
(my-cool-app) $ [my-cool-app] sls deploy
Serverless: Installing requirements of requirements.txt in .serverless...
Serverless: Packaging Python WSGI handler...
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Injecting required Python packages to package...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (11.5 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.................................
Serverless: Stack update finished...
Service Information
service: flask-serverless
stage: dev
region: us-east-1
stack: flask-serverless-dev
api keys:
None
endpoints:
ANY - https://xxx.execute-api.us-east-1.amazonaws.com/dev
ANY - https://xxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
functions:
app: flask-serverless-dev-app
(my-cool-app) $ [my-cool-app] curl https://xxx.execute-api.us-east-1.amazonaws.com/dev
Hello World!%
(my-cool-app) $ [my-cool-app] sls wsgi serve &
(my-cool-app) $ [my-cool-app] curl http://localhost:5000
Hello World!%