sureshdsk.dev

sureshdsk.dev

Deploy a serverless flask application on AWS Lambda

Subscribe to my newsletter and never miss my upcoming articles

1_vhdmyJrtqT6n0G2djDo0EQ.jpeg

Serverless

Serverless is one of the new buzz word right now, what does it really means? Serverless computing allows us to focus on building the application without managing infrastructure and scaling. Serverless doesn’t mean no servers at all. It is still there, but end users are not going to manage it. The best part is that you only pay for the amount of resources each time you consume the serverless function(lambda). On each request to the serverless function, the provider(aws) allocates necessary resources and runs application code in a stateless container.

Advantages:

  • Horizontally scalable
  • Pay for what you use
  • Infrastructure managed by service provider

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. It To make this tutorial easier, we are going to use an popular open source serverless framework called "Zappa" and run "Flask" application on aws lambda.

Prerequisites

  • experience on AWS
  • python 3

Install dependancies

pip3 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

Enter all required details in the interactive prompt, 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 a web accessible url of the flask application.

Deploy Latest Build to AWS Lambda

When you update your flask application code, zappa has a command to update aws lambda deployment.

zappa update dev

Originally published on hackernoon

Interested in reading more such articles from Suresh Kumar?

Support the author by donating an amount of your choice.

 
Share this