2

I want to proxy all my HTTP requests from root through a single lambda function call. I tried setting /{proxy+} as the path in my serverless.yml. But when I deploy it I get "This page isn't redirecting properly" when visiting the root.

serverless.yml snippet

functions:
  hello:
    handler: bin/hello
    url: true
    events:
      - httpApi:
#          path: /{proxy+}
          path: /{any+}
          method: get

main.go


import (
    "fmt"
    "github.com/apex/gateway"
    "github.com/gin-gonic/gin"
    "log"
    "net/http"
    "os"
)

func inLambda() bool {
    if lambdaTaskRoot := os.Getenv("LAMBDA_TASK_ROOT"); lambdaTaskRoot != "" {
        return true
    }
    return false
}

func setupRouter() *gin.Engine {
    gin.SetMode(gin.DebugMode)

    r := gin.Default()

    r.GET("/", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"message": "home page"})
    })
    r.GET("/hello-world", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"message": "hello test completed successfully"})
    })

    return r
}

func main() {
    if inLambda() {
        fmt.Println("running aws lambda in aws")
        log.Fatal(gateway.ListenAndServe(":8080", setupRouter()))
    } else {
        fmt.Println("running aws lambda in local")
        log.Fatal(http.ListenAndServe(":8080", setupRouter()))
    }
}

1 Answer 1

1

path: /{proxy+} is correct.

Make sure you are using correct apex version. apex README states that you need to use apex v2 for HTTP API. You seem to be using v1 which supports REST API only.

Sign up to request clarification or add additional context in comments.

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.