Serverless?
Serverless is the new buzz word right now, what does it really means? Serverless computing actually means the server resources like memory and cpu are allocated dynamically. It lets us focus on building the application instead of focussing on what capacity of RAM, CPU machine to deploy it. On each request the resources are allocated automatically and dynamically. The best part is that you only pay for the amount of resources each request consumed. Literally, each request is given its own virtual http server. The main advantages are scaling, maintenance are taken care by the service provider and cost reduction is one of the huge advantages.
AWS Lambda
AWS provides serverless service called “Lambda” which can be used to deploy and run serverless applications. AWS lambda lets us run any Python WSGI based applications on cloud. To make this tutorial easier, we are going to use an popular opensource serverless framework called “Zappa” and run “Flask” application.
Prerequisites
- experience on AWS
- python 3
- experience in flask web framework
Install dependancies
pip install Flask zappa
Setup AWS Account
Create an user in IAM with administrative access and create api key and password. You will need this when you create zappa project.
Create Flask application
main.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
Initialize zappa project
zappa init
finally zappa_settings.json will look like this,
{
"dev": {
"app_function": "main.app",
"aws_region": "<aws_region>",
"profile_name": "<aws_profile>", // profile config at ~/.aws/credentials
"project_name": "serverless-bot",
"runtime": "python3.6",
"s3_bucket": "serverless-bot"
}
}
Deploy to AWS Lambda
zappa deploy dev
Zappa deploy process will automatically generate an web accessible url of the flask application.
Deploy Latest Build to AWS Lambda
zappa update dev