我正在GO中构建一个AWS Lambda函数,该函数应该通过API Gateway端点触发.我面临的问题是,通过POST请求调用端点会导致502错误.我把我的项目建立在YouTube tutorial的基础上,但似乎有些不对劲.

为了以编程方式编译GO函数,我使用一个Makefile在每个函数的目录中生成一个main可执行文件.以下是我的Makefile条中的build条:

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日志(log)指示与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

我对"invalid entrypoint"错误感到困惑,因为我已经在CDK堆栈(love-go-serverless-stack.ts)中配置了/hello路由.这可能与Go二进制的命名或包装方式有关吗?任何见解或建议,可能是什么导致这一问题和如何解决它将不胜感激.

The repository for the code

My environment:

  • Windows 11
  • 1.21.5
  • node 18.18.2

enter image description here


更新

  • 在切换到Mac并修复了Makefile中的缩进后,我能够运行make build.我仍然无法在Windows 11上运行我的代码.

推荐答案

根据这个Blog Post,将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_REQ)文件夹中创建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,
});

Go相关问答推荐

困扰围棋官方巡回赛的S建议所有方法都使用同一类型的接收器

使用ciph.AEAD.Seal()查看内存使用情况

如何将GoFr筛选器用于查询参数?

如何用Golang解码这个嵌套的json?

无法使用exec从管道中读取.Go中的命令

go测试10m后如何避免超时

Golang Gorm Fiber - 如何将定义为别名的名称发送到索引模板?

Golang 发送Post请求出现400错误

使用 Go Colly 抓取所有可能的标签并将它们放入一个变量中

golang 上基于标头的版本控制

你如何在 Golang 代码中测试 filepath.Abs​​ 失败?

Go Colly 如何找到请求的元素?

CBC Decrypter 解密加密文本,但部分文本被随机字符替换

当函数返回一个函数时,为什么 Go 泛型会失败?

如何为导入的嵌入式 struct 文字提供值?

Golang Gin 绑定请求正文 XML 到 Slice

有没有办法将 yaml node 添加到 golang 中现有的 yaml 文档中?

如何根据 Go 中第二次出现的分隔符拆分字符串?

即使一个测试用例失败,如何运行所有测试用例

Golang LinkedList 删除第一个元素