Zappaで”Error: Warning! Status check on the deployed lambda failed. A GET request to ‘/’ yielded a 502 response code.”が表示される

2022.09.13

はじめに

Zappaでlambdaを開発し、デプロイすると、"Error: Warning! Status check on the deployed lambda failed. A GET request to '/' yielded a 502 response code."というエラーが発生することがあります。

$ zappa deploy
Calling deploy for stage dev..
/home/XXXXXXXXXXXXXXX/.local/lib/python3.8/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils.
  warnings.warn("Setuptools is replacing distutils.")
Downloading and installing dependencies..
 - markupsafe==2.1.1: Downloading
100%|██████████████████████████████████████████████████████████████████████████████| 25.9k/25.9k [00:00<00:00, 9.91MB/s]
Packaging project as zip.
Uploading XXXXXXXXXXXXXXX-dev-XXXXXXXXXX.zip (2.2MiB)..
100%|██████████████████████████████████████████████████████████████████████████████| 2.29M/2.29M [00:00<00:00, 6.16MB/s]
Waiting for lambda function [XXXXXXXXXXXXXXX-dev] to become active...
Scheduling..
Scheduled XXXXXXXXXXXXXXXXXXXXX-zappa-keep-warm-handler.keep_warm_callback with expression rate(4 minutes)!
Uploading XXXXXXXXXXXXXXX-dev-template-XXXXXXXXXX.json (1.6KiB)..
100%|██████████████████████████████████████████████████████████████████████████████| 1.67k/1.67k [00:00<00:00, 20.7kB/s]
Waiting for stack XXXXXXXXXXXXXXX-dev to create (this can take a bit)..
 75%|███████████████████████████████████████████████████████████████                     | 3/4 [00:15<00:05,  5.22s/res]
Deploying API Gateway..
Waiting for lambda function [XXXXXXXXXXXXXXX-dev] to be updated...
Error: Warning! Status check on the deployed lambda failed. A GET request to '/' yielded a 502 response code.

🤔

環境

アプリケーションのソースコードはFlaskのサンプルそのままです。おかしいところはなさそうです。

app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

設定ファイルもinitしたままです。

zappa_settings.json

{
    "dev": {
        "app_function": "app.app",
        "aws_region": "ap-northeast-1",
        "profile_name": "default",
        "project_name": "XXXXXXXXXXXXXX",
        "runtime": "python3.8",
        "s3_bucket": "zappa-XXXXXXX"
    }
}

ログを見てみましょう

$ zappa tail
Calling tail for stage dev..
[1663035383616] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):
[1663035391674] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):
[1663035444807] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):
[1663035568999] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):
[1663035623242] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):
[1663035678406] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):
[1663035786581] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):
[1663035863124] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):
[1663035924295] [ERROR] Runtime.ImportModuleError: Unable to import module 'handler': attempted relative import with no known parent package
Traceback (most recent call last):

handlerはあるんだけど・・・?

Lambdaを単体でテストしてみてみましょうか。

{
  "errorMessage": "Unable to import module 'handler': attempted relative import with no known parent package",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}

スタックトレースがない!?

結論

このエラーは、venv使用時にZappaが仮想環境側にインストールされていない場合に発生します。Zappaを誤ってグローバル側にインストールすると、zappaコマンドは使えるが、デプロイされるzipファイルにzappaのランタイムが含まれないため、lambdaが実行できなくなるためです。

この状態では、venv側のpip listの結果は、以下のようになります。

$ pip list
Package            Version
------------------ -------
click              8.1.3
Flask              2.2.2
importlib-metadata 4.12.0
itsdangerous       2.1.2
Jinja2             3.1.2
MarkupSafe         2.1.1
pip                20.0.2
pkg-resources      0.0.0
setuptools         44.0.0
Werkzeug           2.2.2
zipp               3.8.1

zappaをvenv側にインストールすると、解決します。

$ pip install zappa
(略)

$ zappa update dev
Calling update for stage dev..
Downloading and installing dependencies..
WARNING:package_version(0.0.0) not found in pkg-resources metafile=/tmp/cached_pypi_info/pkg-resources-0.0.0.json
 - markupsafe==2.1.1: Downloading
100%|██████████████████████████████████████████████████████████████████████████████| 25.9k/25.9k [00:00<00:00, 9.26MB/s]
Packaging project as zip.
Uploading XXXXXXXXXXXXXXX-dev-XXXXXXXXXX.zip (5.6MiB)..
100%|██████████████████████████████████████████████████████████████████████████████| 5.88M/5.88M [00:00<00:00, 8.13MB/s]
Updating Lambda function code..
Waiting for lambda function [XXXXXXXXXXXXXXX-dev] to be updated...
Updating Lambda function configuration..
Uploading XXXXXXXXXXXXXXX-dev-template-XXXXXXXXXX.json (1.6KiB)..
100%|██████████████████████████████████████████████████████████████████████████████| 1.67k/1.67k [00:00<00:00, 20.7kB/s]
Deploying API Gateway..
Scheduling..
Unscheduled XXXXXXXXXXXXXXXXXXXXXXX-zappa-keep-warm-handler.keep_warm_callback.
Scheduled XXXXXXXXXXXXXXXXXXXXXXX-zappa-keep-warm-handler.keep_warm_callback with expression rate(4 minutes)!
Waiting for lambda function [XXXXXXXXXXXXXXX-dev] to be updated...
Your updated Zappa deployment is live!: https://XXXXXXXXXXXXXXX.execute-api.ap-northeast-1.amazonaws.com/dev

事故を防ぐため、zappaをグローバルにインストールしないことをおすすめします。