Google Cloud Functions Framework

Grant Timmerman
Google Cloud - Community
3 min readApr 15, 2019

Cloud Functions is a managed service for serverless functions on Google Cloud Platform. Functions scale dynamically, charge per use and eliminate the toil of infrastructure management. So why would I want to run functions in different environments?

In this article, we will cover the basics of function portability using the functions framework. We’ll show you how you can use functions on Google Cloud Functions locally, from the gcloud CLI, a URL, and applications like Google Sheets.

What is the Functions Framework?

The Functions Framework allows you to:

  • Spin up a local development server for quick testing
  • Invoke a function in response to a request
  • Automatically unmarshal events conforming to the CloudEvents spec
  • Be portable between serverless platforms

Google Cloud Functions for Node 10 uses the Functions Framework for it’s underlying invoker module.

It’s open-source on GitHub and npm: @google-cloud/functions-framework

3 Demos

The below demos show calling a Cloud Function from localhost, the gcloud CLI, a URL, and Google Sheets.

We set COINBASE_TOKEN as an environment variable. Create your key here.

Code

In Node, export a function that makes a request to api.coinbase.com BTC in USD. We can optionally provide a specific date as a URL query parameter.

Node 10 with `COINBASE_TOKEN` env variable.

This function can be called in multiple environments thanks to the Functions Framework:

Localhost

Start the functions framework server
Go to localhost:8080

gcloud CLI

Deployed URL

Google Sheets

You can Cloud Functions from Google Sheets with a few lines of Apps Script glue.

In Google Sheets, you can type = and autocomplete your function.

Calling Cloud Functions via Google Sheets

How it works

When you run gcloud functions deploy for Node 10, we…

  1. Upload the directory to the folder.
  2. Wrap the user code with the functions framework, which creates a HTTP server in a container image.
  3. Store that container for future function invocations.
  4. Spin up instances of the image according to demand and invoke the function.

Run any. Run anywhere. Integrate anything.

I gave an intro to Cloud Functions at Next ’19. I recommend watching at 1.5x speed.

--

--