使用Go构建AWS Lambda(provided.al2023)时遇InvalidEntryPoint错误求助
解决AWS Lambda provided.al2023的InvalidEntryPoint错误
问题根源
你遇到的Runtime.InvalidEntrypoint错误,核心原因是Lambda部署包的根目录下没有bootstrap可执行文件。当前的配置和构建流程导致bootstrap被打包到了子目录中,Lambda无法识别。
关键澄清:serverless.yml中的路径规则
serverless.yml里的所有文件路径,都是相对于该yaml文件所在的目录(也就是你的项目根目录)的。例如backend/.bin/bootstrap指的是项目根目录下的backend/.bin/bootstrap文件。
修复步骤
1. 调整Go构建与打包逻辑
修改Makefile,确保编译后的bootstrap最终能被打包到部署包的根目录:
back_sls_build: # 编译Go代码到backend/.bin目录 @cd backend && env GOARCH=arm64 GOOS=linux go build -tags lambda.norpc -o ./.bin/bootstrap main.go && cd .. # 从项目根目录打包,将backend/.bin/bootstrap直接放到zip根目录 @zip -FS bootstrap.zip backend/.bin/bootstrap -j back_deploy: back_sls_build @serverless deploy --stage dev
2. 修正serverless.yml配置
在serverless.yml中直接指定预打包好的zip文件,避免自动打包时的路径问题:
service: my-service provider: name: aws runtime: provided.al2023 architecture: arm64 region: ap-southeast-1 stage: ${opt:stage, 'dev'} httpApi: cors: true name: ${self:service}-${self:provider.stage} plugins: - serverless-plugin-go package: # 指定打包好的zip文件,该zip根目录就是bootstrap artifact: bootstrap.zip functions: healthcheck: handler: bootstrap # provided.al2023的handler可设为bootstrap events: - httpApi: path: /healthcheck method: GET
3. 可选:简化插件自动处理流程
如果你依赖serverless-plugin-go,可以让插件自动完成构建与打包,无需手动写Makefile:
- 删除Makefile中的手动构建逻辑
- 确保serverless.yml中插件启用,调整package配置为:
package: patterns: - "!**/*" - backend/main.go
插件会自动编译Go代码,并将生成的bootstrap放到部署包根目录。
验证方法
部署前可以查看.serverless目录下生成的zip包,解压后确认根目录下存在bootstrap可执行文件,且权限为可执行(Go编译产物默认具备可执行权限)。
内容的提问来源于stack exchange,提问作者Namit Deb
相关产品推荐
相关产品推荐

