Netlify 函数 (AWS Lambda) 示例 "hello" Golang 函数:无效或意外的标记

Netlify functions (AWS Lambda) example "hello" Golang function: Invalid or unexpected token

我正在尝试让 Ne​​tlify Functions 与 Go 一起工作。 首先,我尝试克隆官方示例 repo (https://github.com/netlify/aws-lambda-go-example),它成功了。

我的问题是,我有一个需要 hugo 构建命令的 Hugo 网站,我不知道如何使用 hugo 构建 Hugo 和使用 make build 构建 Go 源文件(就像在示例 repo 中一样)-我认为它可以解决问题,但我找不到描述此选项的相关文档。

所以我的下一步是手动编译 Go 函数文件并将其放入 functions 文件夹中。

源文件(来自上面的例子):

package main

import (
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    return events.APIGatewayProxyResponse{
        StatusCode: 200,
        Body:       "Hello AWS Lambda and Netlify",
    }, nil
}

func main() {
    // Make the handler available for Remote Procedure Call by AWS Lambda
    lambda.Start(handler)
}

我使用了 https://github.com/aws/aws-lambda-go#building-your-function 上可用的指令来编译 Go 二进制文件:

GOOS=linux GOARCH=amd64 go build -o hello hello.go
zip hello.zip hello
mv hello.zip ./functions/hello.zip

这被推送到 Git,因此部署到 Netlify。到目前为止一切顺利,我的函数出现在 Netlify UI.

但是当我请求函数 URL 时,我收到错误消息:

    {
  "errorMessage": "Invalid or unexpected token",
  "errorType": "SyntaxError",
  "stackTrace": [
    "",
    "SyntaxError: Invalid or unexpected token",
    "createScript (vm.js:80:10)",
    "Object.runInThisContext (vm.js:139:10)",
    "Module._compile (module.js:616:28)",
    "Object.Module._extensions..js (module.js:663:10)",
    "Module.load (module.js:565:32)",
    "tryModuleLoad (module.js:505:12)",
    "Function.Module._load (module.js:497:3)",
    "Module.require (module.js:596:17)",
    "require (internal/module.js:11:18)"
  ]
}

这是来自 Netlify 的函数日志:

1:18:16 AM: hello invoked
1:18:17 AM: Syntax error in module 'hello': SyntaxError
(function (exports, require, module, __filename, __dirname) { ELF
                                                              ^
SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
1:19:02 AM: hello invoked
1:19:03 AM: Syntax error in module 'hello': SyntaxError
                                                              ^
SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

此外,函数名称在 Netlify UI 中似乎是 hello.js - 我不知道它是否应该是这样的。在我看来,AWS 认为它是 Javascript 而不是 Go。

I have not tested a zipped go function on Netlify.

如果您不想在这种情况下进行手动构建,您可以在 Netlify 部署中内联构建命令。

添加一个构建命令,为项目执行两个构建。

[build]
  command = "make build && hugo"
  functions = "functions"
  publish = "public"
[build.environment]
  # Change this path with the path to your repository
  GO_IMPORT_PATH = "github.com/netlify/aws-lambda-go-example"