Skip to content

Instantly share code, notes, and snippets.



Created Apr 25, 2016
What would you like to do?
func (handler *LambdaHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Remove the leading slash and dispatch it to the golang handler
lambdaFunc := strings.TrimLeft(req.URL.Path, "/")
decoder := json.NewDecoder(req.Body)
var request lambdaRequest
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%v", r)
errorString := fmt.Sprintf("Lambda handler panic: %#v", err)
http.Error(w, errorString, http.StatusBadRequest)
err := decoder.Decode(&request)
if nil != err {
errorString := fmt.Sprintf("Failed to decode proxy request: %s", err.Error())
http.Error(w, errorString, http.StatusBadRequest)
"Request": request,
"LookupName": lambdaFunc,
lambdaAWSInfo := handler.lambdaDispatchMap[lambdaFunc]
var lambdaFn LambdaFunction
if nil != lambdaAWSInfo {
lambdaFn = lambdaAWSInfo.lambdaFn
} else if strings.Contains(lambdaFunc, "::") {
// Not the most exhaustive guard, but the CloudFormation custom resources
// all have "::" delimiters in their type field. Even if there is a false
// positive, the customResourceForwarder will simply error out.
lambdaFn = customResourceForwarder
if nil == lambdaFn {
http.Error(w, "Unsupported path: "+lambdaFunc, http.StatusBadRequest)
lambdaFn(&request.Event, &request.Context, w, handler.logger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.