1. 程式人生 > 實用技巧 >[Flask] 04 - Serverless Flask by Zappa

[Flask] 04 - Serverless Flask by Zappa

Ref:The Official Guide to Serverless Flask

實踐筆記

一、Zappa是什麼

Zappa 極大的簡化了在 AWS Lambda + API 閘道器上釋出所有 Python WSGI 應用。相當於是無伺服器的部署執行你的 Python Web 應用。

這意味著無限伸縮、零宕機、零維護。

二、實操

Ref: Deploy a Serverless Flask App using Zappa

  • Flask 專案檔案

flaskdeploy$ ls
app.py  credentials  Pipfile  Pipfile.lock  __pycache__  static/cat.jpg  templates/index.html  zappa_settings.json

[app.py]

from flask import Flask, render_template
 
 app = Flask(__name__)
 
 @app.route('/')
 def index():
     return render_template('index.html')
 
 @app.route('/api')
 def api():
     return {'hello': 'world'}

[index.html]

 <!DOCTYPE html>
 <html lang="en">
     <head>
         <
meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1>Hello, World!</h1>
<img src="{{ url_for('static', filename='cat.jpg') }}" /> </body> </html>

  • 部署 Zappa

pipenv install zappa flask
pipenv install --dev awscli
zappa init
zappa deploy dev

flaskdeploy$ zappa init

███████╗ █████╗ ██████╗ ██████╗  █████╗
╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
  ███╔╝ ███████║██████╔╝██████╔╝███████║
 ███╔╝  ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
███████╗██║  ██║██║     ██║     ██║  ██║
╚══════╝╚═╝  ╚═╝╚═╝     ╚═╝     ╚═╝  ╚═╝

Welcome to Zappa!

Zappa is a system for running server-less Python web applications on AWS Lambda and AWS API Gateway.
This `init` command will help you create and configure your new Zappa deployment.
Let's get started!

Your Zappa configuration can support multiple production stages, like 'dev', 'staging', and 'production'.
What do you want to call this environment (default 'dev'): 

AWS Lambda and API Gateway are only available in certain regions. Let's check to make sure you have a profile set up in one that will work.
We found the following profiles: default, [email protected], trendspek, and dev@codeguru\.io. Which would you like us to use? (default 'default'): 

Your Zappa deployments will need to be uploaded to a private S3 bucket.
If you don't have a bucket yet, we'll create one for you too.
What do you want to call your bucket? (default 'zappa-z184zc6iq'): 

It looks like this is a Flask application.
What's the modular path to your app's function?
This will likely be something like 'your_module.app'.
We discovered: app.app
Where is your app's function? (default 'app.app'): 

You can optionally deploy to all available regions in order to provide fast global service.
If you are using Zappa for the first time, you probably don't want to do this!
Would you like to deploy this application globally? (default 'n') [y/n/(p)rimary]: 

Okay, here's your zappa_settings.json:

{
    "dev": {
        "app_function": "app.app",
        "aws_region": "us-east-1",
        "profile_name": "default",
        "project_name": "flaskdeploy",
        "runtime": "python3.6",
        "s3_bucket": "zappa-z184zc6iq"
    }
}

Does this look okay? (default 'y') [y/n]: y 

Done! Now you can deploy your Zappa application by executing:

    $ zappa deploy dev

After that, you can update your application code with:

    $ zappa update dev

To learn more, check out our project page on GitHub here: https://github.com/Miserlou/Zappa
and stop by our Slack channel here: https://zappateam.slack.com

Enjoy!,
 ~ Team Zappa!


flaskdeploy$ zappa deploy dev
Calling deploy for stage dev.. Creating flaskdeploy-dev-ZappaLambdaExecutionRole IAM Role.. Creating zappa-permissions policy on flaskdeploy-dev-ZappaLambdaExecutionRole IAM Role. Downloading and installing dependencies.. - markupsafe==1.1.1: Downloading
100%|███████████████████████████████████████████████████████| 27.5k/27.5k [00:00<00:00, 48.4kB/s] Packaging project as zip. Uploading flaskdeploy-dev-1602573091.zip (9.6MiB).. 100%|████████████████████████████████████████████████████████| 10.1M/10.1M [00:24<00:00, 410kB/s] Scheduling.. Scheduled flaskdeploy-dev-zappa-keep-warm-handler.keep_warm_callback with expression rate(4 minutes)! Uploading flaskdeploy-dev-template-1602573138.json (1.6KiB).. 100%|█████████████████████████████████████████████████████████| 1.62k/1.62k [00:01<00:00, 849B/s] Waiting for stack flaskdeploy-dev to create (this can take a bit).. 75%|█████████████████████████████████████████████▊ | 3/4 [00:12<00:04, 4.16s/res] Deploying API Gateway.. Deployment complete!: https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev