algnhsa GoDoc Build Status

algnhsa is an AWS Lambda Go net/http server adapter.

algnhsa enables running Go web applications on AWS Lambda and API Gateway or ALB without changing the existing HTTP handlers:

package main

import (


func indexHandler(w http.ResponseWriter, r *http.Request) {

func addHandler(w http.ResponseWriter, r *http.Request) {
    f, _ := strconv.Atoi(r.FormValue("first"))
    s, _ := strconv.Atoi(r.FormValue("second"))
    w.Header().Set("X-Hi", "foo")
    fmt.Fprintf(w, "%d", f+s)

func contextHandler(w http.ResponseWriter, r *http.Request) {
    proxyReq, ok := algnhsa.ProxyRequestFromContext(r.Context())
    if ok {
        fmt.Fprint(w, proxyReq.RequestContext.AccountID)

func main() {
    http.HandleFunc("/", indexHandler)
    http.HandleFunc("/add", addHandler)
    http.HandleFunc("/context", contextHandler)
    algnhsa.ListenAndServe(http.DefaultServeMux, nil)

Plug in a third-party HTTP router

package main

import (


func main() {
    r := chi.NewRouter()
    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
    algnhsa.ListenAndServe(r, nil)

Setting up API Gateway

  1. Create a new REST API.

  2. In the "Resources" section create a new ANY method to handle requests to / (check "Use Lambda Proxy Integration").

    API Gateway index

  3. Add a catch-all {proxy+} resource to handle requests to every other path (check "Configure as proxy resource").

    API Gateway catch-all

Setting up ALB

  1. Create a new ALB and point it to your Lambda function.

  2. In the target group settings enable "Multi value headers".

Expand ▾ Collapse ▴







This section is empty.


This section is empty.


func ListenAndServe

func ListenAndServe(handler http.Handler, opts *Options)

    ListenAndServe starts the AWS Lambda runtime (aws-lambda-go lambda.Start) with a given handler.

    func ProxyRequestFromContext

    func ProxyRequestFromContext(ctx context.Context) (events.APIGatewayProxyRequest, bool)

      ProxyRequestFromContext extracts the APIGatewayProxyRequest event from ctx.

      func TargetGroupRequestFromContext

      func TargetGroupRequestFromContext(ctx context.Context) (events.ALBTargetGroupRequest, bool)

        TargetGroupRequestFromContext extracts the ALBTargetGroupRequest event from ctx.


        type Options

        type Options struct {
        	// RequestType sets the expected request type.
        	// By default algnhsa deduces the request type from the lambda function payload.
        	RequestType RequestType
        	// BinaryContentTypes sets content types that should be treated as binary types.
        	// The "*/* value makes algnhsa treat any content type as binary.
        	BinaryContentTypes []string
        	// Use API Gateway PathParameters["proxy"] when constructing the request url.
        	// Strips the base path mapping when using a custom domain with API Gateway.
        	UseProxyPath bool
        	// contains filtered or unexported fields

          Options holds the optional parameters.

          type RequestType

          type RequestType int
          const (
          	RequestTypeAuto RequestType = iota