Serverless gives the hassle-free deployment and management of the code in both development and production environments, which decreases the development time and cost and also the struggle with deployments.
In AWS, we get many serverless services like Lambda
, DynamoDB
, API Gateway
etc. All of these services are managed by AWS and we do not need to manage the scalability, deployment, load management, etc.
For using the serverless framework, we need serverless.yml
inside the root directory of the project. In serverless.yml, we will define the application configuration, like, from where the app will be served, what is needed in DynamoDB, how many functions will be needed, and all their names, etc.
Creating the new Flask app with a serverless framework:
mkdir flask-serverless-application
cd flask-serverless-application
npm init -f
After this, we will need to install the serverless plugins, which is, serverless-wsgi
, which is needed to run the deployed Python application on lambda. Another one is, serverless-python-requirements
, which will handle all the python package requirements.
npm install --save-dev serverless-wsgi serverless-python-requirements
Now, we can start with the flask application. We will create app.py
file, where we will write our Flask API.
# app.py
from flask import Flask, make_response
app = Flask(__name__)
@app.route("/")
def root_route():
return make_response(({'message' : "Running Serverless Flask App."}))
The above-written app will return a JSON in response to the API call, which will return a message key with the value Running Serverless Flask App.
Now we will create serverless.yml
file
service: flask-serverless-app
plugins:
- serverless-python-requirements
- serverless-wsgi
custom:
wsgi:
app: app.app
packRequirements: false
pythonRequirements:
dockerizePip: non-linux
package:
exclude:
- node_modules/**
- venv/**
provider:
name: aws
runtime: python3.8
stage: dev
region: ap-southeast-2
iamManagedPolicies:
- arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess
versionFunctions: false
timeout: 29
functions:
app:
handler: wsgi.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
cors: true
In the above code, we have defined code.wsgi.app
as app.app
, which will tell wsgi from where the Flask app is needed to be served. All the required plugins are defined inside plugins
. And inside functions
we define handled as wsgi.handler
, as we have used serverless-wsgi
plugin, which will help in serving all the APIs through flask on the HTTP events.
To run the code, we will need to create the python environment and then run the flask app locally first and then deploy the code.
First, create the virtual environment and install the dependencies
virtualenv venv --python=python3
source venv/bin/activate
When the virtual environment is active, then we will run the installations and then freeze the packages in requirements.txt.
(venv) $ pip install flask
(venv) $ pip freeze > requirements.txt
Now, we will test running the code on the local machine. First, we will add the was keys using aws-cli.
aws configure
This can also be done by editing the credentials file in the aws root folder at `~/.aws/credentials`. Here you can create AWS profiles, which can be used to to deploy in multiple accounts and for multiple project development from one system.
Then we will run the code on the local environment.
sls wsgi serve
This should run the code on port 5000
.
Now to deploy the code, we will run the command:
sls deploy
This command should return URLs of the API gateway, from where the API can be accessed directly, which should be something like:
https://ny931jc3f1.execute-api.ap-southeast-2.amazonaws.com/dev
From this URL, we will be able to hit any API created in the Flask application. We can create as many APIs, and we do not need to make any changes in the serverless.yml file, as we have defined any endpoint in the HTTP events.
This is a simple Flask serverless application. We can achieve many things using Flask serverless. We can connect to different databases, and server applications and create many high-level applications.
In case you need more insights, contact me from the contact page.