You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS AppRunner Python应用TCP健康检查失败求助

AWS AppRunner Python应用TCP健康检查失败排查

我是AWS AppRunner新手,正尝试部署一个基础Python应用,但始终无法通过TCP健康检查。为了适配无法禁用的AWS健康检查,我给原本无网络的程序添加了运行在独立线程TCPServer上的SimpleHTTPRequestHandler,本地运行正常,但部署后失败。

服务日志

2023-02-18T15:20:20.856-05:00   [Build] Step 5/5 : EXPOSE 80
2023-02-18T15:20:20.856-05:00   [Build] ---> Running in abcxyz 
2023-02-18T15:20:20.856-05:00   [Build] Removing intermediate container abcxyz 
2023-02-18T15:20:20.856-05:00   [Build] ---> f3701b7ee4cf
2023-02-18T15:20:20.856-05:00   [Build] Successfully built abcxyz 
2023-02-18T15:20:20.856-05:00   [Build] Successfully tagged application-image:latest
2023-02-18T15:30:49.152-05:00   [AppRunner] Failed to deploy your application source code.

控制台日志

02-18-2023 03:30:49 PM [AppRunner] Deployment with ID : 123456789 failed. Failure reason : Health check failed.
02-18-2023 03:30:38 PM [AppRunner] Health check failed on port '80'. Check your configured port number. For more information, read the application logs.
02-18-2023 03:24:21 PM [AppRunner] Performing health check on port '80'.
02-18-2023 03:24:11 PM [AppRunner] Provisioning instances and deploying image for privately accessible service.
02-18-2023 03:23:59 PM [AppRunner] Successfully built source code.

应用代码

import socketserver
import threading
from http.server import SimpleHTTPRequestHandler


# handler for server
class HealthCheckHandler(SimpleHTTPRequestHandler):
    def do_GET(self) -> None:
        self.send_response(code=200)
        self.send_header("Content-Type", "text/html")
        self.end_headers()
        self.wfile.write("""<html><body>hello, client.</body><html>""".encode('utf-8'))


# runs the server
def run_healthcheck_server():
    with socketserver.TCPServer(("127.0.0.1", 80), HealthCheckHandler) as httpd:
        print(f"Fielding health check requests on: 80")
        httpd.serve_forever()


# dummy
def my_app_logic():
    while True:
        print('hello, server.')
        time.sleep(5)


# wrapper to run primary application logic AND TCPServer
def main():

    # run the server in a thread
    threading.Thread(target=run_healthcheck_server).start()

    # run my app
    my_app_logic()


if __name__ == "__main__":
    main()

问题排查与解决

问题出在TCP请求处理代码上:

  1. 绑定地址错误:你的TCPServer绑定了127.0.0.1,这个地址只接受容器内部本地的请求,而AppRunner的健康检查是从容器外部发起的,因此无法访问到端口80。需要将绑定地址改为0.0.0.0,让服务器监听容器的所有网络接口。
  2. 缺失依赖导入:my_app_logic函数中使用了time.sleep()但没有导入time模块,部署时会触发报错,需要添加import time。

修改后的关键代码片段:

import time  # 新增导入

# runs the server
def run_healthcheck_server():
    # 将绑定地址改为0.0.0.0
    with socketserver.TCPServer(("0.0.0.0", 80), HealthCheckHandler) as httpd:
        print(f"Fielding health check requests on: 80")
        httpd.serve_forever()

另外确认AppRunner的健康检查配置端口为80(从日志看已正确设置),Dockerfile的EXPOSE 80配置也没问题。

内容的提问来源于stack exchange,提问作者alphazwest

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:30:57