0

I am trying to port a server running a small golang app to AWS Lambda. I am not very familiar with golang and to deploy to a server I have just followed the instructions in the repo.

It runs a server with net/http, the main.go is as follows:

func main() {
    r := new(route.Router)
    r.HandleFunc("/squares", squares.Random)

    // ... more Handlers

    log.Println("Listening on " + os.Getenv("PORT"))
    err := http.ListenAndServe(":"+os.Getenv("PORT"), r)
    if err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}

Now I have found this drop-in replacement repo on Github for ListenAndServe, apex/gateway, but I think I am missing a fundamental step in making it work.

What I've done is download and import of the library

import (
    ...
    
    "github.com/apex/gateway/v2"
)

then simply replace the function in main, zip and upload to aws lambda

func main() {
    r := new(route.Router)
    r.HandleFunc("/squares", squares.Random)

    // ... more Handlers

    // log.Println("Listening on 8080")
    err := gateway.ListenAndServe(":8080", r)
    if err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}

then I set up an http API Gateway and link to the lambda function.

It doesn't work. I think I'm missing something but I can't figure out what. From the example on the apex/gateway repo, I don't see what I'm missing.

The app is Tinygraphs fwiw.

Thank you

Edit: As per Adrians comment, when I go to the api link I get

{"message":"Not Found"}
3
  • 1
    When posting, it is helpful to provide more detail on the problem than "It doesn't work". What specifically is the problem? Do you get an error? If so, what's the error? If not, how did you determine that it didn't work? What actually happened? Commented Oct 27, 2020 at 13:21
  • "Internal Server Error" means you need to look at the backend logs. Either the AWS Lambda logs, or API Gateway logs to see what the actual error is. Without that information all anyone can do is guess at the problem. Commented Oct 27, 2020 at 13:39
  • correct error is {"message":"Not Found"}, sorry Commented Oct 27, 2020 at 13:41

2 Answers 2

1

Hey User I believe you are not understanding how API Gateway and Lambda works properly

You do not need to set up a route listening on 8080 that is effectively what API Gateway is doing and then forwarding the request to your code running on the lambda. Which is normally in this format:

package main

import (
        "fmt"
        "context"
        "github.com/aws/aws-lambda-go/lambda"
)

type MyEvent struct {
        Name string `json:"name"`
}

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
        return fmt.Sprintf("Hello %s!", name.Name ), nil
}

func main() {
        lambda.Start(HandleRequest)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you David, I did read that somewhere but I assumed it was handled in the apex/gateway repo. Also, in the example on the apex/gateway repo, they do specify a port. What do you think is missing then in the apex/gateway example to deploy on aws, or how would you extend your example to run my app?
0

Was a pretty stupid mistake.

I had defined the api path as / instead of /{proxy+}, so it wasn't accepting a non-root url whereas the invoke url is like this: https://XXXXXXXX.execute-api.us-east-1.amazonaws.com/squares/tinygraphs?theme=frogideas&numcolors=4&size=220&fmt=svg

Thank you everyone who looked at this.

(regarding the port, according to this, the port specified is just ignored by gateway.ListenAndServe)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.