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

容器化AWS SAM CLI遇连接超时,调整SAM_CLI_CONTAINER_CONNECTION_TIMEOUT无效

容器化AWS SAM CLI时连接Lambda容器超时问题

我正在尝试容器化AWS SAM CLI,对比复杂的DinD方式,选择挂载macOS宿主机的/var/run/docker.sock方案。当前SAM容器能正常启动,调用http://127.0.0.1:5432/hello也能命中容器,但日志报连接超时错误,同时提示Lambda响应不是有效JSON。我用的是AWS官方Hello World API示例,可排除代码问题。已经在docker-compose里设置SAM_CLI_CONTAINER_CONNECTION_TIMEOUT=120,日志显示超时时间已改为120秒,但还是超时。

容器初始日志报错

Timed out while attempting to establish a connection
sam_api_test | to the container. You can increase this timeout by setting the
sam_api_test | SAM_CLI_CONTAINER_CONNECTION_TIMEOUT environment variable. The current timeout
sam_api_test | is 20.0 (seconds).
sam_api_test | 2024-04-19 13:45:53,656 | Invalid lambda response received: Lambda response must
sam_api_test | be valid json

调整超时后的日志

/var/task:ro,delegated, inside runtime container
2024-04-19 15:27:02,253 | [Container state] OOMKilled False
2024-04-19 15:27:02,732 | Cleaning all decompressed code dirs
2024-04-19 15:27:02,734 | Timed out while attempting to establish a connection
to the container. You can increase this timeout by setting the
SAM_CLI_CONTAINER_CONNECTION_TIMEOUT environment variable. The current timeout
is 120.0 (seconds).

docker-compose.yml配置

services:
  sam-api:
    build:
      context: ./
      dockerfile: sam-api.Dockerfile
    container_name: sam_api_test
    working_dir: /usr/src/app
    ports:
      - "5432:5432"
    volumes:
      - ./sam-api-test:/usr/src/app
      - /var/run/docker.sock:/var/run/docker.sock
    restart: "no"
    tty: true
    entrypoint: sam local start-api --host 0.0.0.0 --port 5432 --profile default --debug
    networks:
      - "sam-api-test-network"

networks:
  sam-api-test-network:
    name: sam_api_test_network
    driver: bridge

sam-api.Dockerfile配置

FROM node:latest
RUN npm install -g npm@10.5.2
# Install AWS CLI
RUN apt-get update && \
    apt-get install -y awscli && \
    apt-get clean
# Install AWS SAM CLI
# RUN npm install -g aws-sam-cli
RUN curl -L https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip -o aws-sam-cli-linux-x86_64.zip
RUN unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
RUN ./sam-installation/install
# Install Nano
RUN apt-get install -y nano && \
apt-get clean
# Create symbolic links for AWS CLI and AWS SAM CLI
RUN ln -s /usr/local/bin/aws aws && \
    ln -s /usr/local/bin/sam sam
# Set the working directory
WORKDIR /usr/src/app
# Copy the application files
COPY . .
EXPOSE 5432
# Set the entry point
CMD ["/bin/sh"]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 21:42:48