1 of 7

Google Cloud Functions

GDG Vienna, 26 April 2017

2 of 7

Google Cloud Functions?

Another serverless Google Cloud option

  • App Engine (standard & flex)
  • Firebase (BaaS)

Based on JavaScript / Node.js

Executed via Triggers

Super-Elastic pricing

3 of 7

Based on JavaScript / Node.js

Add dependencies through package.json

Deploy through a storage "bucket"�gcloud beta functions deploy <NAME> --stage-bucket <BUCKET_NAME> <TRIGGER>

Or through Cloud Source Repositories�gcloud beta functions deploy <NAME> \� --source-url https://source.developers.google.com/p/<PROJECT_ID>/r/<REPOSITORY_ID> \� --source-path <SOURCE> <TRIGGER> [<SOURCE_BRANCH>|<SOURCE_TAG>|<SOURCE_REVISION>]

4 of 7

Triggers

Cloud functions called through external events

  • HTTP request
  • Cloud Pub/Sub messages
  • Cloud Storage changes
  • Firebase events
  • Direct call from command client

5 of 7

Super-Elastic Pricing

Pay as you go

  • per function call ($0.0000004 per invocation)
  • per GB/GHz-second ($0.0000025 per GB-second)

Generous free tier

  • 2 million invocations
  • 400 000 GB-seconds

6 of 7

Sample Code: Hello World

/**� * Cloud Function.� *� * @param {object} event The Cloud Functions event.� * @param {function} The callback function.� */

exports.helloWorld = function helloWorld (event, callback) {� console.log(`My Cloud Function: ${event.data.message}`);� callback();�};

https://cloud.google.com/functions/docs/tutorials/http

7 of 7

Sample Code: Slack Slash Commands

exports.gSearch = function gSearch (req, res) {� return Promise.resolve()� .then(() => {� if (req.method !== 'POST') { /* error … throwing */ }� // Verify that this request came from Slack� verifyWebhook(req.body);� // Make the request to the Knowledge Graph Search API� return makeSearchRequest(req.body.text);� })� .then((response) => {� res.json(response);� })� // ...�};

https://cloud.google.com/functions/docs/tutorials/slack