【问题标题】:Netlify functions (AWS Lambda) example "hello" Golang function: Invalid or unexpected tokenNetlify 函数 (AWS Lambda) 示例“hello”Golang 函数:无效或意外的令牌
【发布时间】:2018-12-12 00:49:41
【问题描述】:

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

我的问题是,我有一个需要 hugo 构建命令的 Hugo 网站,但我不知道如何使用 hugo 构建 Hugo 和使用 make build 构建源文件(如示例 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。

【问题讨论】:

    标签: go aws-lambda netlify


    【解决方案1】:

    我没有在 Netlify 上测试过压缩的 go 功能。

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

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

    [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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 2021-08-04
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多