AWS Lambda 与 Go:对 API 网关端点的 POST 请求出现“Runtime.InvalidEntrypoint”错误

问题描述 投票:0回答:1

我正在 Go 中构建一个 AWS Lambda 函数,该函数应通过 API 网关端点触发。我遇到一个问题,通过 POST 请求调用端点会导致 502 错误。我已经根据 YouTube 教程 设置了我的项目,但似乎有些问题。

为了以编程方式编译 Go 函数,我使用 Makefile 在每个函数的目录中生成

main
可执行文件。这是我的
build
Makefile
部分:

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

# Deploy stack with AWS CDK
.PHONY: deploy
deploy:
    echo "Deploying stack..."
    cdk deploy --profile $(AWS_PROFILE)

我使用命令

make deploy
部署我的堆栈。

我的

love-go-serverless-stack.ts
文件的相关部分如下所示:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { RestApi, LambdaIntegration } from 'aws-cdk-lib/aws-apigateway';

export class LoveGoServerlessStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Hello Lambda function
    const helloFunction = new lambda.Function(this, 'HelloFunction', {
      code: lambda.Code.fromAsset('src/functions/hello'),
      handler: 'main',
      runtime: lambda.Runtime.PROVIDED_AL2023,
    });

    // The API Gateway
    const gateway = new RestApi(this, 'MyGateway', {
      defaultCorsPreflightOptions: {
        allowOrigins: ['*'],
        allowMethods: ['GET', 'POST'],
      },
    });

    // The Lambda integration
    const integration = new LambdaIntegration(helloFunction);

    // Creating the '/hello' resource
    const helloResource = gateway.root.addResource('hello');
    helloResource.addMethod('POST', integration); // POST method for the 'hello' resource
  }
}

当我向 API Gateway 提供的 CloudFront URL 发出 POST 请求时,收到 502 错误。 CloudWatch 日志指示与 Lambda 函数入口点相关的重复出现的错误:

RequestId: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxx Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]
Runtime.InvalidEntrypoint

并且:

INIT_REPORT Init Duration: 0.27 ms Phase: invoke Status: error Error Type: Runtime.InvalidEntrypoint

我对“无效入口点”错误感到困惑,因为我已经在我的 CDK 堆栈中配置了

/hello
路由 (
love-go-serverless-stack.ts
)。这是否与 Go 二进制文件的命名或打包方式有关?任何有关可能导致此问题以及如何解决此问题的见解或建议将不胜感激。

代码存储库

go aws-lambda makefile aws-cdk
1个回答
0
投票

根据此博客文章,将 Lambda 函数从

GO_1_X
环境迁移到
PROVIDED_AL2023
环境需要将可执行文件重命名为通用
bootstrap
名称。此修改需要更改您的构建脚本。此外,我将二进制文件压缩为 ZIP 文件以创建部署包,为 Lambda 部署做好准备。

build:
  @echo "Building functions..."
  @$(foreach func, $(FUNCTIONS), \
      go build -o $(FUNCTIONS_DIR)/$(func)/bootstrap $(FUNCTIONS_DIR)/$(func)/main.go && \
      cd $(FUNCTIONS_DIR)/$(func); zip ../$(func).zip bootstrap 
      echo "Built $(func)/$(BINARY_NAME)"; \
  )

# Deploy stack with AWS CDK
.PHONY: deploy
deploy:
    echo "Deploying stack..."
    cdk deploy --profile $(AWS_PROFILE)

这将在 $(FUNCTIONS_DIR) 文件夹中创建 zip 包,允许您修改在 CDK 项目中创建 lambda 函数的方式。请注意,处理程序必须重命名为

bootstrap

const helloFunction = new lambda.Function(this, 'HelloFunction', {
    code: lambda.Code.fromAsset(path.join(__dirname, `../../bin/${func}.zip`)),,
    handler: 'bootstrap',
    runtime: lambda.Runtime.PROVIDED_AL2023,
});
© www.soinside.com 2019 - 2024. All rights reserved.