Google Cloud Functions Framework
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.
This function can be called in multiple environments thanks to the Functions Framework:
Localhost
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.
How it works
When you run gcloud functions deploy
for Node 10, we…
- Upload the directory to the folder.
- Wrap the user code with the functions framework, which creates a HTTP server in a container image.
- Store that container for future function invocations.
- Spin up instances of the image according to demand and invoke the function.
Run any. Run anywhere. Integrate anything.
Next Steps
- Check out the Functions Framework on GitHub
- Read our Serverless @ Next ’19 blogpost
- Watch “Serverless on the Cloud”