A First Netlify Function in Golang
Write a function
The Netlify docs for golang are a great starting point. I am deploying this project manually, so I need to build the thing myself rather than letting the Netlify CI servers do that.
Here’s a very simple example with a handler declared in main()
and then a function that will greet you by name if you supply a name
parameter in the query (this code lives in src/first.go
):
package main import ( "context" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-lambda-go/lambda" ) func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) { params := request.QueryStringParameters name := "World" if params["name"] != "" { name = params["name"] } return &events.APIGatewayProxyResponse{ StatusCode: 200, Body: "Hi " + name, }, nil } func main() { lambda.Start(handler) }
Great, we wrote a function! Now what? Time to build the code before deploying it.
Build the binary
You can push code to GitHub and instruct the Netlify CI on how to use the source code to build what you want … in this case I’m just pushing one function as an example so I’ll do the building myself. I like to use a Makefile for this:
build: mkdir -p functions GOOS=linux GOARCH=amd64 go build -o functions/first ./src/first.go
Note that for go build
setting the appropriate config for Lambda is important here – on my Ubuntu machine, probably this would build correctly, but by being specific in my Makefile
I’m less likely to screw this up in future :)
When I run this by typing make
, it puts the compiled first
binary into the functions/
directory. This is important as we work up to deploying in the next step.
Deploy to Netlify
I’m doing a manual deploy because I’m too lazy to set up a pipeline for a one-off project, so I am using the netlify-cli
command line tool here. Before I do though, I’ll configure the settings in a netlify.toml
file in the root of my project. Mine looks like this:
[build] command = "make build" functions = "functions" publish = "./"
The important part here is the functions
setting – my build step above put the binaries into the functions/
directory, and that’s what Netlify will deploy. There’s more information in the .netlify.toml
docs too.
Now the deploy command! By default this has a really neat feature where you can deploy a draft and check it before promoting to production. For this project though, I’m just going to go straight for the live platform. You only live once, after all.
netlify deploy --prod
This will upload whatever I put in the functions/
folder and then come back to me with a “Live URL” that it deployed to.
Call the function
Now I can call the function! The URL is a bit funky: [Live URL
]/.netlify/functions/first – but Netlify make this easy to find through the web interface, look in the Site you are working on, choose functions from the top bar and when you click on the function name, you get detailed information including its “Endpoint” and also some logs (the output of fmt.Print*
goes to the logs here, very useful for debugging!).
I can append ?name=Jane
to see my function greet Jane rather than World, which is rather simple but neat. You can access a load of the request info AND the serverless function context from within the function so many more things are possible. I tend to think that the hard part is usually the moving parts and not the code, so hopefully this will help me next time I want to do this. If it helped you too, feel free to share more tips in the comments box!
Also published on Medium.