使用Python编写的Google Cloud Function触发DAG时出现404错误求助
我在GCP上运行Python编写的Cloud Function,尝试触发Airflow DAG,严格按照官方文档操作,但持续收到Airflow返回的404错误,错误日志如下:
EDT gcs-dag-trigger-function 68hsnlvog0g6 Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/__init__.py", line 171, in view_func function(data, context) File "/workspace/main.py", line 48, in trigger_dag make_iap_request( File "/workspace/main.py", line 87, in make_iap_request raise Exception( Exception: Bad response from application: 404 / {'Date': 'Fri, 15 Jul 2022 16:25:22 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Vary': 'Accept-Encoding', 'Server': 'gunicorn', 'X-Robots-Tag': 'noindex, nofollow', 'Set-Cookie': 'session=d8fb37f6-bd2d-4a2c-8abc-e528eadc9ac3.hUuadQnCRiJzbuufKObZIMtsTL8; Expires=Sun, 14-Aug-2022 16:25:22 GMT; HttpOnly; Path=/; SameSite=Lax', 'Content-Encoding': 'gzip', 'Via': '1.1 google', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"', 'Transfer-Encoding': 'chunked'} / ' <!DOCTYPE html> <html lang="en"> <head> <title>Airflow 404</title> <link rel="icon" type="image/png" href="/static/pin_32.png"> </head> <body> <div style="font-family: verdana; text-align: center; margin-top: 200px;"> <img src="/static/pin_100.png" width="50px" alt="pin-logo" /> <h1>Airflow 404</h1> <p>Page cannot be found.</p> <a href="/">Return to the main page</a> <p>3405c1ef5ce7</p> </div> </body> </html>'
以下是排查和解决方向:
核对Airflow API路径与版本匹配
Airflow 1.x和2.x的DAG触发API路径完全不同:- 2.x版本用
/api/v1/dags/<DAG_ID>/dagRuns - 1.x版本用
/api/experimental/dags/<DAG_ID>/dag_runs
很多404都是因为硬套了错误版本的API路径,先确认你的Airflow版本,再修正请求路径。
- 2.x版本用
检查DAG ID的准确性
Airflow对DAG ID的匹配是严格区分大小写和符号的,确保Cloud Function代码里写的DAG ID和Airflow UI里显示的完全一致,比如不要把my_dag写成My_Dag或者my-dag。验证IAP目标URL和请求完整路径
如果通过IAP访问Airflow,确保IAP保护的是Airflow的根域名,且Cloud Function里的请求URL是完整的根域名加API路径,比如https://your-airflow-url.com/api/v1/dags/your-dag-id/dagRuns,不要漏掉根域名或者写错路径层级。确认Airflow API未被禁用
对于Airflow 2.x,默认API是启用的,但如果自定义了webserver_config.py,要确保ENABLE_API = True;1.x版本要确认api_enabled配置为True,避免API端点被关闭。直接测试API端点
绕过Cloud Function,用curl或Postman带上正确的IAP认证令牌直接访问Airflow API端点,确认端点本身是可访问的,排除Cloud Function代码之外的问题。示例命令:curl -H "Authorization: Bearer <你的IAP令牌>" https://your-airflow-url.com/api/v1/dags/your-dag-id/dagRuns
内容的提问来源于stack exchange,提问作者DamianO

