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

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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘