Go lambdas: differences with AWS lambdas prevents me from using algnhsa

Hi! :wave:

I’m trying to refactor a golang lambda I’m currently running in production to use akrylysov/algnhsa to be able to test more easily, since it is not possible to run go lambdas in Netlify Dev yet.

algnhsa is overall working but I’m having issues setting headers. To replicate, deploy this function to both, Netlify Functions and AWS Lambda + API Gateway and curl it:

package main

import (
	"net/http"

	"github.com/akrylysov/algnhsa"
)

func handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("location", "https://en.wikipedia.org")
	w.WriteHeader(http.StatusMovedPermanently)
}

func main() {
	http.HandleFunc("/", handler)
	algnhsa.ListenAndServe(http.DefaultServeMux, nil)
}

You will notice the location header is there for the AWS Lamda but it is not for the Netlify Function.

I’ve deployed the very same code above to both for you to test:

https://vigorous-galileo-f758df.netlify.com/.netlify/functions/test
https://iopemvdzz4.execute-api.us-east-1.amazonaws.com/default/

To clarify, if I use the regular lambda handler signature that takes events.APIGatewayProxyRequest and returns an events.APIGatewayProxyResponse, it does work (it is what I have in production right now). Adding algnhsa as a compat layer works on AWS Lambda but strips the location header in Netlify Functions.

So it makes me think there’s some other layer in Netlify Functions that might be out of sync with the AWS interface .

1 Like

I ran some more tests and indeed it seems somehow the interface for Netlify Functions doesn’t match that of AWS Lambda.

The MultiValueHeaders field of the events.APIGatewayProxyResponse gets ignored in Netlify Functions:

package main

import (
	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

func handler(request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
	return &events.APIGatewayProxyResponse{
		StatusCode: 301,
		MultiValueHeaders: map[string][]string{
			"Location": []string{"https://en.wikipedia.org"},
		},
	}, nil
}

func main() {
	lambda.Start(handler)
}

The code above works as expected in AWS Lambda but, again, lacks the location header on Netlify Functions.

Since algnhsa uses MultiValueHeaders, as seen here, it doesn’t work with Netlify Functions.

It is my understanding that Netlify Functions actually run in AWS Lambda. How does it work? Aren’t the binaries uploaded as they are? Are the aws-lambda-go dependencies hijacked during deploy? :thinking:

1 Like

Ok so probably related to this: Multiple `Set-Cookie` headers cause netlify lambda to throw an error

1 Like

hi @stefanmaric, thank you sooo much for digging in to this. You’re correct. We don’t use API gateway, we use our own, and it seems we don’t yet have support for MultiValueHeaders. I’ve gone ahead and filed an issue about this for our team to look at. I don’t anticipate a fast turnaround, but we’ll let you know when we have more info.

Hope it gets some traction. :crossed_fingers:

My team has a few new experimental projects kicking off and this is kinda holding us back because we need to be able to test locally (and I don’t see go support coming to netlify-dev anytime soon). Of course we might as well run it directly on AWS Lambda, but would rather skip all the boilerplate and setup and take advantage of the CI/CD flow we already use for our frontends.

Did yo umake any progress on this? I have another issue out to figure out how to run Go functions locally for dev. Are you able to run them locally for site to make calls to? If so, how?

I’ve answered your question around the local testing story and how to build go functions in the other thread you opened.

We don’t have an answer on the MultiValueHeaders issue yet. It’s not a small project to implement since we aren’t actually using the AWS API Gateway, we’re using their invoke api from our own in-house proxy.

1 Like

Netlify recently updated their AWS API Gateway and multiValueHeaders are now supported! :tada:

3 Likes

I wrote a blog post about how you can use the standard Go net/http package with Netlify: How to Use Netlify to Deploy a Free Go Web Application · The Ethically-Trained Programmer It makes it easier to test locally without needing to simulate Lambda.

1 Like