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

Go开发AWS Lambda调用API Gateway POST请求遇Runtime.InvalidEntrypoint错误

AWS Lambda Go函数API Gateway 502错误排查(Runtime.InvalidEntrypoint)

核心错误原因

你的CloudWatch日志明确指出Lambda找不到bootstrap文件,这是因为PROVIDED_AL2023运行时要求部署包根目录必须存在名为bootstrap的可执行文件,而你当前编译出的文件是main,不符合运行时的启动要求。另外Windows环境下的编译配置问题也导致二进制文件无法在Lambda的Linux环境运行。

具体解决步骤

1. 重命名Go二进制文件为bootstrap并添加交叉编译配置

修改Makefile中的编译命令,将输出文件改为bootstrap,同时指定Linux环境交叉编译(Lambda运行在Linux系统,Windows直接编译的二进制无法兼容):

# 定义输出文件名为bootstrap
BINARY_NAME := bootstrap

build:
    @echo "Building functions..."
    @$(foreach func, $(FUNCTIONS), \
        GOOS=linux GOARCH=amd64 go build -o $(FUNCTIONS_DIR)/$(func)/$(BINARY_NAME) $(FUNCTIONS_DIR)/$(func)/main.go && \
        echo "Built $(func)/$(BINARY_NAME)"; \
    )

2. 修正CDK函数配置

检查love-go-serverless-stack.ts中的HelloFunction配置,确保handler指向bootstrap,且代码目录正确指向包含该文件的路径:

const helloFunction = new lambda.Function(this, 'HelloFunction', {
  runtime: lambda.Runtime.PROVIDED_AL2023,
  handler: 'bootstrap', // PROVIDED_AL2023的handler对应启动文件名
  code: lambda.Code.fromAsset(path.join(__dirname, 'functions/hello')), // 确保该目录下直接存放bootstrap
});

3. 修复Windows 11下的Makefile问题

Windows下的Make工具对缩进和环境变量语法敏感,可做以下调整:

  • 确保Makefile中的缩进使用Tab键而非空格(你在Mac上修复缩进后可运行,说明Windows下缩进格式仍有问题)
  • 若Windows无法识别GOOS=linux前缀式环境变量,改用Windows风格的环境变量设置:
    build:
        @echo "Building functions..."
        @set GOOS=linux&& set GOARCH=amd64&& $(foreach func, $(FUNCTIONS), \
            go build -o $(FUNCTIONS_DIR)/$(func)/$(BINARY_NAME) $(FUNCTIONS_DIR)/$(func)/main.go && \
            echo "Built $(func)/$(BINARY_NAME)"; \
        )
    
    也可直接用PowerShell替代Makefile执行编译:
    $env:GOOS="linux"
    $env:GOARCH="amd64"
    go build -o functions/hello/bootstrap functions/hello/main.go
    

4. 验证部署包结构

部署完成后,进入Lambda控制台的「代码」标签页,确认函数代码根目录下存在bootstrap文件,而非嵌套在子文件夹中。

额外排查点

  • 直接在Lambda控制台测试函数,排除API Gateway配置问题
  • 若在Linux/Mac环境编译,需确保bootstrap文件有可执行权限:chmod +x functions/hello/bootstrap

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 02:20:29