dherault/serverless-offline

Golang handler confusion

Open

#1,358 opened on Mar 28, 2022

View on GitHub
 (5 comments) (16 reactions) (0 assignees)JavaScript (792 forks)batch import
help wanted

Repository metrics

Stars
 (5,072 stars)
PR merge metrics
 (Avg merge 1d 11h) (6 merged PRs in 30d)

Description

There doesn't seem to be a way to make the project runnable in both AWS and offline.

Attempt 1 (from aws-go)

So I have this config (from aws-go template):

functions:
  hello:
    handler: bin/hello
    events:
      - httpApi:
          path: /hello
          method: get
  world:
    handler: bin/world
    events:
      - httpApi:
          path: /world
          method: get

This works on AWS and with --useDocker, but fails locally with an error:

$ curl 127.0.0.1:5555/hello
{"errorMessage":"ENOENT: no such file or directory, open '/home/anderson/src/serverlessapp.go'","errorType":"Error","stackTrace":[]}

Please note the path above: it's pointing to an unexisting serverlessapp.go file (which in fact is a folder of my project, serverlessapp)

Attempt 2 (from PR #1320)

I've checked #1320 and that PR contains a different config, so I've rewritten my config by changing handler string:

functions:
  hello:
    handler: hello/main.go  # <-------
    events:
      - httpApi:
          path: /hello
          method: get
  world:
    handler: world/main.go  # <-------
    events:
      - httpApi:
          path: /world
          method: get

Now, local execution succeeds:

$ curl 127.0.0.1:5555/hello
{"message":"Go Serverless v1.0! Your function executed successfully!"}

However, now the code does not work when deployed to AWS:

$ curl https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/hello
{"message":"Internal Server Error"}
$ serverless logs --function hello
Running "serverless" from node_modules
START
fork/exec /var/task/hello/main.go: no such file or directory: PathError
null
END Duration: 2.96 ms (init: 19.76 ms) Memory Used: 19 MB
START
fork/exec /var/task/hello/main.go: no such file or directory: PathError
null
END Duration: 2.62 ms (init: 19.52 ms) Memory Used: 19 MB

Question: What is the correct way to configure my project? I cannot seem to find a way to run a project with the same config both locally and on AWS. Config 1 works on AWS only, config 2 works locally only...

Thanks in advance!

Contributor guide