[Flask] 04 - Serverless Flask by Zappa
Ref: The Official Guide to Serverless Flask
Ref: 使用 AWS X-Ray 跟踪 API Gateway API 执行
用户请求通过您的 Amazon API Gateway API 传输到底层服务时,您可以使用 AWS X-Ray 来跟踪和分析用户请求,API Gateway 支持所有 API Gateway 终端节点类型的 AWS X-Ray 跟踪:区域、边优化和私有。
借助 Amazon API Gateway,您可以在 X-Ray 可用的所有区域使用 AWS X-Ray。
X-Ray 为您提供了整个请求的端到端视图,因此您可以分析 API 中的延迟及其后端服务。
您可以使用 X-Ray 服务地图,以查看整个请求的延迟及集成了 X-Ray 下游服务的延迟。
您可以配置规则以告知 X-Ray 根据您指定的标准以哪种采样率记录哪些请求。
如果您从正在跟踪的服务中调用 API Gateway,则 API Gateway 会通过跟踪,即使 X-Ray 跟踪在 API 上未启用。
您可以使用 X-Ray 管理控制台启用 API Gateway 的 API 阶段,或使用 API Gateway API 或 CLI。
Ref: https://app.cloudcraft.co/
实践笔记
一、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
Ref: Building a Simple API with Amazon Lambda and Zappa
pip install --upgrade pip setuptools pipenv
pipenv --three
pipenv shell
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, dev@codeguru.io, 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
三、AWS X-Ray
Ref: Instrumenting web frameworks deployed to serverless environments
Ref: AWS X-Ray console
四、CI/CD for Zappa
Ref: Build and Deploy Serverless Python Applications on AWS Using Zappa
Ref: Serverless application with CI/CD based on AWS and Bitbucket Pipelines [参考]
/* implement */