dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.19k stars 794 forks source link

Golang support? #351

Closed jsilvia721 closed 6 years ago

jsilvia721 commented 6 years ago

Are there plans to support runtime: go1.x now that Go is supported by AWS lambda?

daniel-cottone commented 6 years ago

@jsilvia721 Not by myself, however if you would like to raise a PR please feel free 😄

Ludonope commented 6 years ago

I looked a little and it work pretty simply with golang. I can call a lambda on my local environment with a simple binary, but I have no idea how to integrate it to the project.

package main

import (
    "encoding/json"
    "log"
    "net/rpc"

    "github.com/aws/aws-lambda-go/events"

    "github.com/aws/aws-lambda-go/lambda/messages"
)

func main() {
    // Connect to the lambda
    client, err := rpc.Dial("tcp", "localhost:8080")
    if err != nil {
        log.Fatal("Connection error: ", err)
    }

    // Create the payload
    payload := events.APIGatewayProxyRequest{
        PathParameters: map[string]string{"id": "bcodvikbqm4c7297sgq0"},
    }
    json, _ := json.Marshal(payload)

    // Create the request
    req := messages.InvokeRequest{
        ClientContext:         []byte(""),
        CognitoIdentityId:     "",
        CognitoIdentityPoolId: "",
        Deadline: messages.InvokeRequest_Timestamp{
            Seconds: 6,
            Nanos:   0,
        },
        InvokedFunctionArn: "",
        Payload:            []byte(json),
        RequestId:          "",
        XAmznTraceId:       "",
    }

    // Call lambda
    var resp messages.InvokeResponse
    err = client.Call("Function.Invoke", &req, &resp)

    if err != nil {
        log.Fatal("Problem calling getter: ", err)
    }
    log.Println(string(resp.Payload))
    log.Println(resp.Error)
}

We could pretty easily pass a json to this binary from the serverless-offline plugin, but I don't know where to start, I might continue to look about it since it can really help me right now

steve-winter commented 5 years ago

Enabling Golang, for my uses so far, has just required enabling the runtime, and remainder is handled by the sls invoke capabilities.

Dasio commented 5 years ago

It doesn't work for me. When I want to hit some endpoints my console is spammed by:

Proxy Handler could not detect JSON: Serverless: Warning the Serverless Dashboard doesn't support the following runtime: go1.x

Proxy Handler could not detect JSON: Serverless: Warning the Serverless Dashboard doesn't support the following runtime: go1.x

Proxy Handler could not detect JSON: Serverless: Packaging service...

Proxy Handler could not detect JSON: Serverless: Excluding development dependencies...
dnalborczyk commented 5 years ago

@Dasio what version of serverless-offline are you running?

Dasio commented 5 years ago

5.8.0 and sls version: 1.48.4 (Enterprise Plugin: 1.3.3, Platform SDK: 2.1.0)

Also I got these warnings:

Serverless: WARNING: Serverless Offline does not support non local authorizers (arn): arn:aws:cognito-idp:eu-central-1:<id>:userpool/eu-central-1_<id>

But I dont think there warnings causes errors?

dnalborczyk commented 5 years ago

@Dasio just tried it out myself and I got it working, although it seems to be somewhat flaky.

Warning the Serverless Dashboard doesn't support the following runtime: go1.x

this warning seems to be a serverless framework warning. not sure what the dashboard is, but it says it only supports node.js: https://serverless.com/framework/docs/dashboard/

Proxy Handler could not detect JSON: Serverless

I got the same in the console. probably needs to get fixed.

besides the warnings, did you get it working?

Dasio commented 5 years ago

No I didn't. I couldn't get any response from lambda. I'm using also serverless-plugin-split-stacks, not sure if it's connected with it.

package:
  individually: true
  exclude:
     - ./**
functions:
  user:
    handler: bin/user
    package:
      include:
      - ./bin/user
    events:
      - http:
          path: /user
          method: get
          cors: true
          authorizer:
            arn: ${self:custom.cognitoPool}