Let's take another look at our function configuration in serverless.yml: functions: This does give a framework in which you can work to set up those things. This isn't a full-fledged REST API, and you'll want to add things like error handling, authentication, and additional business logic. To get this application deployed, create a serverless.yml in the working directory: # serverless.yml service: serverless-flask plugins: - serverless-python-requirements - serverless-wsgi custom: wsgi: app: app.app packRequirements: false pythonRequirements: dockerizePip: non-linux provider: name: aws runtime: python3.6 stage: dev region: us-east-1 functions: app: handler: wsgi.handler events: - http: ANY / - http: 'ANY /users/alexdebrie1 It's the example application shown on Flask's landing page with no modifications. This is a very simple application that returns "Hello World!" when a request comes in on the root path /. Create a file app.py with the following contents: # app.py from flask import FlaskĪpp = "/") def hello(): return "Hello World!" With our libraries installed, let's write our Flask application. If you want a deeper dive on the serverless-python-requirements plugin, check out our previous post on handling Python packaging with Serverless.
$ npm install -save-dev serverless-wsgi serverless-python-requirements We'll also use the serverless-python-requirements plugin for handling our Python packages on deployment.
We're going to use the serverless-wsgi plugin for negotiating the API Gateway event type into the WSGI format that Flask expects. Let's start by deploying a single endpoint.įirst, create a new directory with a package.json file: $ mkdir my-flask-application & cd my-flask-application You'll also need your environment configured with AWS credentials. To get started, you'll need the Serverless Framework installed. If you want to skip the walkthrough and just get started with a fully-configured template, check out the Using the Quick Start Template section below. If you already have a Flask application that you want to convert to Serverless, skip to the Converting an existing Flask application section below. Configure your environment for local development for a faster development experience.Set up path-specific routing for more granular metrics and monitoring.Add a DynamoDB table and two endpoints to create and retrieve a User object.
Let’s go over how to use the Python web framework Flask to deploy a Serverless REST API. Your existing web framework tooling can work seamlessly with the Serverless Framework.