go - Netlify 函数 (AWS Lambda) 示例 "hello"Golang 函数 : Invalid or unexpected token

标签 go aws-lambda netlify

我正在尝试让 Ne​​tlify Functions 与 Go 一起使用。 首先,我尝试克隆官方示例存储库 ( https://github.com/netlify/aws-lambda-go-example ) 并且它有效。

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

所以我的下一步是手动编译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 中。

hello.js

但是当我请求函数 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"

关于go - Netlify 函数 (AWS Lambda) 示例 "hello"Golang 函数 : Invalid or unexpected token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53734532/

相关文章:

go - 如何在grpc go中实现非阻塞客户端?

go - Go 会改变单个 goroutine 中两个互斥锁的执行顺序吗?

python - AWS LAMBDA "errorMessage": "[Errno 30] Read-only file system: ' drive-python-quickstart. json'"

python - Telegram bot webhook 和 amazon lambda

aws-lambda - AWS Lambda : Unable to access SQS Queue from a Lambda function with VPC access

在 netlify 中部署时未拾取 CSS 样式

css - 如何将 CSS 文件与 golang 中的 html 页面链接起来

go - 在 go (1.18) 中对泛型进行多态实现的最佳方法是什么?

firebase - 部署 sveltekit 应用程序时出现 Netlify 错误

gatsby - 部署的 Netlify 页面在显示实际内容之前显示 404 页面