dpyoh6553
2018-12-12 00:49
浏览 116
已采纳

Netlify函数(AWS Lambda)示例“ hello” Golang函数:无效或意外的令牌

I'm trying to get Netlify Functions work with Go. First, I tried cloning official example repo (https://github.com/netlify/aws-lambda-go-example) and it worked.

My problem is, I have a Hugo website which require hugo build command and I don't know how to build Hugo with hugo and Go source files with make build (like in example repo) - I think it could solve the problem, but I couldn't find relevant docs describing this option.

So my next step was to manually compile Go function file and put it into functions folder.

source file (from the example above):

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)
}

I used instruction available at https://github.com/aws/aws-lambda-go#building-your-function to compile Go binary:

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

This was pushed to Git and therefore deployed to Netlify. So far so good, my function appeared in Netlify UI.

hello.js

But when I requested the function URL, I got error message:

    {
  "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)"
  ]
}

This is function log from 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)

Also, function name appears to be hello.js in Netlify UI - I don't know if it should be like that. It seems to me that AWS thinks it's Javascript instead of Go.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doyhq66282 2018-12-12 17:52
    已采纳

    I have not tested a zipped go function on Netlify.

    If you do not want to do the manual build in this case, you can inline your build commands on Netlify deploy.

    Add a build command that does both builds for the project.

    [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"
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题