A First Netlify Function in Golang

I love all things serverless, and Netlify Functions is a neat and convenient way to access AWS Lambda. I do also love AWS Lambda but it’s so powerful and flexible that creating something like a webhook receiver can be hard going by the time you have all the permissions and API Gateway setup sorted out — for a simple webhook receiver, Netlify functions is a nice and easy way to get going. Best of all: it supports Golang but the docs are very JS-heavy so I am writing my notes here in case I need to refer back to them some day.

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 (


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() {

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:

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:

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!

Originally published at LornaJane.




Polyglot programmer, technology addict, open source fanatic and incurable blogger (see http://lornajane.net)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Tester Credibility

Create FTP server with Docker Swarm

Coding Period 1: Week 3

How to control your car with Alexa and Raspberry Pi

Code Smell 07 — Boolean Variables

Add Page Numbers to an existing PDF Document using Java

How to use range() function in python.

How to Start Reverse Engineering in 2021

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lorna Mitchell

Lorna Mitchell

Polyglot programmer, technology addict, open source fanatic and incurable blogger (see http://lornajane.net)

More from Medium

How to create simple WebAssembly 2D game using Go

How and why to use a function to configure New() in Golang

Gomo — 📐 Simple Golang multi modules tool.

[golang] How to use github for hosting self-updating binaries for your golang projects