Skip to content

Instantly share code, notes, and snippets.

@nicosingh
Last active June 28, 2018 11:57
Show Gist options
  • Save nicosingh/816184f07a5b5c03edce1380da52a1a3 to your computer and use it in GitHub Desktop.
Save nicosingh/816184f07a5b5c03edce1380da52a1a3 to your computer and use it in GitHub Desktop.
serverless.com Flask app

Create a Flask Serverless Python App

Initialise a new app

$ [~] sls create --template aws-python --path my-cool-app
$ [~] cd my-cool-app

Install sls plugins

$ [my-cool-app] sls plugin install --name serverless-python-requirements
$ [my-cool-app] sls plugin install --name serverless-wsgi

Create a Flask app

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 Configuration

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+}'

Add Flask dependencies

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

Deploy

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

Test deployed App

(my-cool-app) $ [my-cool-app] curl https://xxx.execute-api.us-east-1.amazonaws.com/dev
Hello World!%

Test local App

(my-cool-app) $ [my-cool-app] sls wsgi serve &
(my-cool-app) $ [my-cool-app] curl http://localhost:5000
Hello World!%
from flask import Flask
app = Flask(__name__)
@app.route("/")
def main():
return "Hello World!"
if __name__ == "__main__":
app.run()
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
Werkzeug==0.12.2
service: flask-serverless
provider:
name: aws
runtime: python2.7
plugins:
- serverless-python-requirements
- serverless-wsgi
custom:
wsgi:
app: application.app
packRequirements: false
functions:
app:
handler: wsgi.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment