Simple HTTP adapter for AWS Lambda
- AWS Lambda Function URL (both normal and streaming)
- API Gateway (v1)
- API Gateway (v2)
- Application Load Balancer
package main
import (
"github.com/its-felix/aws-lambda-go-http-adapter/adapter"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("pong"))
})
adapter := adapter.NewVanillaAdapter(mux)
}
package main
import (
"github.com/labstack/echo/v4"
"github.com/its-felix/aws-lambda-go-http-adapter/adapter"
"net/http"
)
func main() {
e := echo.New()
e.Add("GET", "/ping", func(c echo.Context) error {
return c.String(200, "pong")
})
adapter := adapter.NewEchoAdapter(e)
}
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/its-felix/aws-lambda-go-http-adapter/adapter"
)
func main() {
app := fiber.New()
app.Get("/ping", func(ctx *fiber.Ctx) error {
return ctx.SendString("pong")
})
adapter := adapter.NewFiberAdapter(app)
}
package main
import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
)
func main() {
adapter := [...] // see above
h := handler.NewAPIGatewayV1Handler(adapter)
lambda.Start(h)
}
package main
import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
)
func main() {
adapter := [...] // see above
h := handler.NewAPIGatewayV2Handler(adapter)
lambda.Start(h)
}
package main
import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
)
func main() {
adapter := [...] // see above
h := handler.NewFunctionURLHandler(adapter)
lambda.Start(h)
}
(read the additional notes about streaming below)
package main
import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
)
func main() {
adapter := [...] // see above
h := handler.NewFunctionURLStreamingHandler(adapter)
lambda.Start(h)
}
package main
import (
"github.com/aws/aws-lambda-go/events"
"github.com/gofiber/fiber/v2"
"github.com/its-felix/aws-lambda-go-http-adapter/adapter"
)
func main() {
app := fiber.New()
app.Get("/ping", func(ctx *fiber.Ctx) error {
event := adapter.GetSourceEventFiber(ctx)
switch event := event.(type) {
case events.APIGatewayProxyRequest:
// do something
case events.APIGatewayV2HTTPRequest:
// do something
case events.LambdaFunctionURLRequest:
// do something
}
return ctx.SendString("pong")
})
}
package main
import (
"github.com/aws/aws-lambda-go/events"
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
event := handler.GetSourceEvent(r.Context())
switch event := event.(type) {
case events.APIGatewayProxyRequest:
// do something
case events.APIGatewayV2HTTPRequest:
// do something
case events.LambdaFunctionURLRequest:
// do something
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("pong"))
})
}
To handle panics, first create the handler as described above. You can then wrap the handler to handle panics like so:
package main
import (
"context"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
)
func main() {
adapter := [...] // see above
h := [...] // see above
h = handler.WrapWithRecover(h, func(ctx context.Context, event events.APIGatewayV2HTTPRequest, panicValue any) (events.APIGatewayV2HTTPResponse, error) {
return events.APIGatewayV2HTTPResponse{
StatusCode: 500,
Headers: make(map[string]string),
Body: fmt.Sprintf("Unexpected error: %v", panicValue),
}, nil
})
lambda.Start(h)
}
Have a look at the existing event handlers:
Have a look at the existing adapters:
You can opt-in to enable partial build by using the build-tag lambdahttpadapter.partial
.
Once this build-tag is present, the following build-tags are available:
lambdahttpadapter.vanilla
(enables the vanilla adapter)lambdahttpadapter.echo
(enables the echo adapter)lambdahttpadapter.fiber
(enables the fiber adapter)lambdahttpadapter.apigwv1
(enables API Gateway V1 handler)lambdahttpadapter.apigwv2
(enables API Gateway V2 handler)lambdahttpadapter.functionurl
(enables Lambda Function URL handler)lambdahttpadapter.alb
(enables Application Load Balancer handler)
Also note that Lambda Function URL in Streaming-Mode requires the following build-tag to be set:
lambda.norpc
Response streaming is currently not supported for the fiber adapter. The code will work, but the response body will only be sent downstream as soon as the request was processed completely.
This is because there seems to be no way in fasthttp
to provide a io.Writer
to be populated while the request is being processed.