我无法使用 localstack 12.5 lambda在 4566 端口运行调用在 localhost:9090 上运行的另一个容器中的 rest api

问题描述

本地堆栈 12.5

用于 lambda 处理程序的 dummy.py 文件

import sys
import requests

def handler(event,context):
    print("Inside handler")
    x = requests.get('http://localhost:9090/ping')
    print(x.status_code)
    return str(x.content)

在处理程序中,当我调用一个容器中的 get api 时,我收到连接被拒绝的错误

错误: "ConnectionError","errorMessage":"httpconnectionPool(host='localhost',port=9090): url 超过最大重试次数: 由 NewConnectionError('\u003curllib3.connection.httpconnection object at 0x7ff8185c8ad0\u003e: 无法建立新连接:

但是通过邮递员我可以访问 http://localhost:9090/ping API

另外,如果我替换 x = requests.get('https://w3schools.com') 那么我得到 200 。

解决方法

问题已通过在 localstack 中添加以下 env 变量得到解决

LAMBDA_DOCKER_NETWORK="host"