Google Cloud Functions are Google’s answer to AWS Lambda. They recently announced the public beta of Cloud Functions, coupled with an “always free” tier that includes 2 million invocations of Cloud Functions.
Google Cloud Functions are a pretty neat way of hosting your Webhook endpoints at Google-standard reliability and well, free of cost. Read on to find out how to do it!
The Execution Environment
Cloud functions are hosted by a Node.js execution environment, running Node v6.x. This means you have to write your logic as a Javascript function exported from a Node.js module. Dependencies are handled using npm.
Node.js is only supported execution environment, at least currently.
Your Node.js module files have to be zipped up and uploaded to a Google Storage
Bucket for deployment. However, the gcloud
command-line can make this a simple
1-step process.
Calling the Function
Your cloud function can be invoked directly as an HTTP(S) endpoint, without any further configuration. You don’t need to configure any other component to do this, unlike AWS’s API Gateway configuration needed for AWS Lambda.
Our First Function
So let’s try it. You’ll need a Google Cloud account of course, and you need to
enable Cloud Functions for your project from this
page.
We’ll call the project my-project
in the snippets.
Step 1: First, we need a Bucket. Since apparently Cloud
Functions are available only on the us-central1
zone currently, this means the
bucket has to be either in that region or multi-regional. Let’s just assume you
have one such, called my-bucket
.
Step 2: We need to write a Node.js module. The simplest thing we can do is
to create a directory, and create a file in it named index.js
, with this
content:
Step 3: Deploy it using the command-line gcloud
tool, like this:
And that’s it! Our webhook is ready. Let’s test it out:
A Useful Webhook
Let’s set up a real-life webhook now.
The OpsDash server monitoring service supports webhooks that are notified when servers go down, or trip alert thresholds. Plivo has APIs that can be used to send SMS texts to your phone. Let’s connect both so you can receive OpsDash notifications on your phone.
Let’s update our index.js to this:
You’ll of course, need to have a Plivo account, and substitute the “authid” and “authtoken” values, as well as phone numbers in the snippet above.
OpsDash webhooks send a JSON object as the request body, and this object contains the property “status”, which is a human-readable alert status, like “1 down, 1 warning”. See the docs for more info.
The code above now contains a dependency – the request
module. You can do the
usual npm install --save request
dance, or just create a package.json
with
the contents:
Finally, let’s add our Webhook into OpsDash:
and tell OpsDash that we want notifications about all sources to go here:
And that’s it! You’ll now be able to receive OpsDash alerts as SMSes on your phone:
Further Reading
- Google Cloud Functions can also be triggered by other means, and do more cool things. Check out the docs for more info.
- Plivo and Twilio are popular options for integrating voice and SMS capabilities into your product.
- OpsDash can monitor your servers, databases and services and tell you when things go wonky.
New Here?
OpsDash is a server monitoring, service monitoring, and database monitoring solution for monitoring MySQL, PostgreSQL, MongoDB, memcache, Redis, Apache, Nginx, Elasticsearch and more. It provides intelligent, customizable dashboards and spam-free alerting via email, HipChat, Slack, PagerDuty and Webhooks. Send in your custom metrics with StatsD and Graphite interfaces built into each agent.