localstack/serverless-localstack

How to use lambda mountCode: true with Golang?

jagonzalr opened this issue · 2 comments

Hello I'm trying to run Golang with localstack and mountCode: true

But I just get the error

FileNotFoundError: [Errno 2] No such file or directory: '/Users/jagonzalr/Development/dummy/src/hello'

serverless.yml

service: 'dummy'

provider:
  name: aws
  runtime: go1.x
  region: us-east-1
  stage: local

plugins:
  - serverless-localstack

package:
  individually: true

custom:
  localstack:
    debug: true
    stages: [local]
    lambda:
      mountCode: true

functions:
  hello:
    handler: src/hello
    events:
      - http:
          path: hello
          method: get
          cors: true

resources:
  Resources:

docker-compose.yml

version: '3.8'

services:
  localstack:
    image: localstack/localstack:latest
    environment:
      - EDGE_PORT=4566
      - LAMBDA_EXECUTOR=local
      - LAMBDA_REMOTE_DOCKER=0
      - SERVICES=lambda,dynamodb,cloudformation,s3,sts,iam,apigateway,ecr
      - HOST_TMP_FOLDER="${TMPDIR:-/tmp}/localstack"
      - DEFAULT_REGION=us-east-1
      - DEBUG=1
    ports:
      - '4566-4583:4566-4583'
    volumes:
      - '${TMPDIR:-/tmp/localstack}:/tmp/localstack'
      - '/var/run/docker.sock:/var/run/docker.sock'

package.json

"scripts": {    
    "deploy:local": "serverless deploy --stage local",
    "start":  "npm run deploy:local && npm run watch",
    "watch":  "nodemon --watch src -e go,js --exec npm run deploy:local"
  },

Go code
src/hello/main.go

func main() {
	handler := handler.Create()
	lambda.Start(handler.Run)
}

Update

I missed adding the code location inside volumes in docker-compose.yml

But now I got another issue: when I try to run the lambda function from the provided endpoint url I get in the console
Error: fork/exec path/src/hello: permission denied

Found my issue, I was pointing to the Go code instead of the compiled binary, once that was done everything works.