Go Serverless: Part 1 Google Cloud Functions
Tags: go, serverless, googlecloud, googlecloudfunctions
Published: 2019-04-12 16:55:50

---
title: Go Serverless: Part 1 Google Cloud Functions
published: true
series: Go Serverless
tags: go, serverless, googlecloud, googlecloudfunctions
canonical_url: https://blog.merzlabs.com/posts/go-serverless-1/
---

# Running Serverless Functions with Golang

Go is the most popular language for cloud computing and serverless is becoming more popular for all kinds of workloads. Let´s have a look on how to run a simple Go function on the different cloud providers.

We will start with with [Google Cloud Functions](https://cloud.google.com/functions/docs/writing/http#writing_http_helloworld-go) and Firebase. AWS Lambda is the most popular serverless platform today. Some providers resell AWS Lambda with nicer/easier tooling and different pricing. We will have a look at [Zeit Now](https://zeit.co/now) and probably “bare-metal” [AWS Lambda](https://aws.amazon.com/blogs/compute/announcing-go-support-for-aws-lambda/) in this series.

For testing the different providers we will use my tiny project [Gistdirect](https://github.com/NiklasMerz/gistdirect) which is a simple HTTP function that redirects to URLs saved in a Github Gist. This project is simple to build and deploy. The Gistdirect function waits to be called with a path to redirect to the given URL.

# The beauty of Go (in functions)

I like to use Go for serverless because basically all platforms are expecting standard Go handler functions like this: `func Handler(w http.ResponseWriter, r *http.Request)`. This means you can build your functions reusable for all kinds of handlers like the standard `http` package or routers like `mux`. I don’t like building stuff that only works in a particular (cloud) environment. Gistdirect runs standalone as well thanks to a simple [`main` package](https://github.com/NiklasMerz/gistdirect/blob/master/main/main.go).

# Google Cloud Functions

[Since January 2019](https://cloud.google.com/blog/products/application-development/cloud-functions-go-1-11-is-now-a-supported-language) Google Cloud functions support Go and combined with Firebase hosting we can get Gistdirect running on its own domain pretty easily.

Obviously you need a Google account and sign up for Google Cloud (GCP). In the cloud console we create a new project. After creation we switch to the Cloud functions menu item and activate the API. After that we could click a button to create a new function, set the runtime to Go, set the environment variable to the Gist URL and paste or upload the code from [function.go](https://github.com/NiklasMerz/gistdirect/blob/master/function.go). But we will use the `gcloud` CLI tool which is more convenient for frequent updates and testing:

- Setup `gcloud` CLI: [Quick start](https://cloud.google.com/sdk/docs/quickstarts)
- Select a project: `gcloud config set project NAME`
- Get the Gistdirect project from Github: `git clone https://github.com/niklasmerz/gistdirect && cd gistdirect`
- Have a look at `function.go`: The function `Handler(..)` contains the redirect code and should be called as entry point.
- Deploy function for runtime Go 1.11 with the enviroment variable needed for Gistdirect and the entry function `Handler` CLI command: `gcloud functions deploy gistdirect --entry-point Handler --trigger-http --runtime go111 --set-env-vars "GIST_URL=https://gist.github.com/NiklasMerz/a9b5905f742b5863197a0af0465a39f6/raw/"`

If everything worked, we will get a result like this:

```
deploying function (may take a while - up to 2 minutes)...done.                                                                             
availableMemoryMb: 256
entryPoint: Handler
environmentVariables:
  GIST_URL: https://gist.github.com/NiklasMerz/a9b5905f742b5863197a0af0465a39f6/raw/
httpsTrigger:
  url: https://us-central1-PROJECT.cloudfunctions.net/gistdirect
labels:
  deployment-tool: cli-gcloud
name: projects/serverless-blog-236916/locations/us-central1/functions/gistdirect
runtime: go111
```

Now we can hit `https://us-central1-PROJECT.cloudfunctions.net/gistdirect/repo` in a browser and we will get redirected to the Gistdirect repo because the test [hostfile](https://gist.githubusercontent.com/NiklasMerz/a9b5905f742b5863197a0af0465a39f6/raw/) we set to the environment variable, has this redirect rule.

That’s it our serverless functions is running on GCP!

## Bonus get a nicer URL

The whole point of gistdirect to redirect from short URL to longer one. That’s why we want to point our own domain to the function. We can do that with Firebase.

- Go to the [Firebase console](https://firebase.google.com/)
- Go to “Hosting” in the menu
- Click “Get started” and install the Firebase CLI tool via NPM
- Just before we go on to setup our local project we can add our custom domain or sudomain to firebase. We follow the instructions to verify and add the domain. I had some trouble getting the DNS records right the first time, but it should work pretty straightforward.
- `cd` into the Gistdirect project directory
- Use `firebase login` only and **not** `firebase init`
- We can switch to our project with `firebase use PROJECTID`
- Have a look at `firebase.json`

```
{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      " **/node_modules/**"
    ],
    "rewrites": [
        {
            "source": "*",
            "function": "gistdirect"
        }
    ]
  }
}
```

- We define here that all our requests to our Firebase domain ([https://PROJECTID.firebaseapp.com/](https://PROJECTID.firebaseapp.com/)) or custom domain should get redirected to our Cloud Function. We just need to set the name of our function and call `firebase deploy` to set it up. Because Firebase does not support Go functions right now we need to take the longer route and set up our functions with GCP.
- You can find more information [here](https://firebase.google.com/docs/functions/).

**Attention** : The function and the Firebase project must live in the same region. My projects are set to `us-central1`. But you could try [other regions](https://firebase.google.com/docs/functions/locations).

**Now we are ready to go to use serverless functions with our domain**