by akrylysov

akrylysov / algnhsa

AWS Lambda Go net/http server adapter

215 Stars 23 Forks Last release: Not found Apache License 2.0 27 Commits 11 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

algnhsa GoDoc Build Status

algnhsa is an AWS Lambda Go

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 ( "fmt" "net/http" "strconv"



func indexHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("index")) }

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 ( "net/http"



func main() { r := chi.NewRouter() r.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("index")) }) algnhsa.ListenAndServe(r, nil) }

Setting up API Gateway

  1. Create a new REST API.

  2. In the "Resources" section create a new

    method to handle requests to
    (check "Use Lambda Proxy Integration").

    API Gateway index

  3. Add a catch-all

    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".

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.