Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Accessing Google Cloud APIs
from Google Cloud compute platforms
Wesley Chun - @wescpy
Developer Advocate, Google
Developer Advocate, Google Cloud
● Mission: enable current and future
developers everywhere to be
successful using Google Cloud and
other Google developer tools & APIs
● Focus: GCP serverless (App Engine,
Cloud Functions, Cloud Run); higher
education, Google Workspace, GCP
AI/ML APIs; multi-product use cases
● Content: speak to developers globally;
make videos, create code samples,
produce codelabs (free, self-paced,
hands-on tutorials), publish blog posts
About the speaker
Previous experience / background
● Software engineer & architect for 20+ years
○ Yahoo!, Sun, HP, Cisco, EMC, Xilinx
○ Original Yahoo!Mail engineer/SWE
● Technical trainer, teacher, instructor
○ Taught Math, Linux, Python since 1983
○ Private corporate trainer
○ Adjunct CS Faculty at local SV college
● Python community member
○ Popular Core Python series author
○ Python Software Foundation Fellow
● AB (Math/CS) & CMP (Music/Piano), UC
Berkeley and MSCS, UC Santa Barbara
● Adjunct Computer Science Faculty, Foothill
College (Silicon Valley)
01
Google & APIs
Our technologies for your apps
BUT
... wait, there’s more...
Accessing Google Cloud APIs
02
Accessing
Google APIs
What you need to do
The first word on Security
Authentication ("authn") vs authorization ("authz")
● authn: you are who you say you are
○ login & passwd
○ handprint authentication
○ retina scan
● authz: okay, you are who you say you are, but can you haz data?
○ OAuth2 - mostly authz, but some authn
○ Mostly about 3rd-party access to data
○ Users must give YOUR code access to THEIR data
○ Most of the time when you see "auth", it refers to authz
● Some refer to this as "consent" vs. "credentials…" which is which?
General steps
1. Go to Cloud Console
2. Login to Google/Gmail account
(Workspace domain may require admin approval)
3. Create project (per application)
4. Enable APIs to use
5. Enable billing (CC, Free Trial, etc.)
6. Download client library(ies)
7. Create & download credentials
8. Write code
9. Run code (may need to authorize)
Google APIs: how to use
Costs and pricing
● GCP: pay-per-use
● Google Workspace: subscription
● GCP Free Trial ($300/1Q, CC req'd)
● GCP "Always Free" tier
○ Most products have free tier
○ Daily or monthly quota
○ Must exceed to incur billing
● More on both programs at
cloud.google.com/free
Cloud/GCP console
console.cloud.google.com
● Hub of all developer activity
● Applications == projects
○ New project for new apps
○ Projects have a billing acct
● Manage billing accounts
○ Financial instrument required
○ Personal or corporate credit cards,
Free Trial, and education grants
● Access GCP product settings
● Manage users & security
● Manage APIs in devconsole
Collaborating & sharing
● Sharing: great way to see student work or have teams work on one project
● IAM (Identity & Access Mgmt): owner & editor most useful access levels
cloud.google.com/iam/docs/understanding-roles
● View application statistics
● En-/disable Google APIs
● Obtain application credentials
Using Google APIs
goo.gl/RbyTFD
API manager aka Developers Console (devconsole)
console.developers.google.com
Three different credentials types
● Simple: API keys (to access public data)
○ Simplest form of authorization: an API key; tied to a project
○ Allows access to public data
○ Do not put in code, lose, or upload to GitHub! (can be restricted however)
○ Supported by: Google Maps, (some) YouTube, (some) GCP, etc.
● Authorized: OAuth client IDs (to access data owned by [human] user)
○ Provides additional layer of security via OAuth2 (RFC 6749)
○ Owner must grant permission for your app to access their data
○ Access granularity determined by requested permissions (user scopes)
○ Supported by: Google Workspace, (some) YouTube, (some) GCP, etc.
● Authorized: service accounts (to access data owned by an app/robot user)
○ Provides additional layer of security via OAuth2 or JWT (RFC 7519)
○ Project owning data grants permission implicitly; requires public-private key-pair
○ Access granularity determined by Cloud IAM permissions granted to service account key-pair
○ Supported by: GCP, (some) Google Workspace, etc.
&
Google APIs client
libraries for many
languages; demos in
developers.google.com/api-
client-library
cloud.google.com/apis/docs
/cloud-client-libraries
Two different client library "styles"
● "Platform-level" client libraries (lower level)
○ Supports multiple products as a "lowest-common denominator"
○ Manage API service endpoints (setup & use)
○ Manage authorization (API keys, OAuth client IDs, service accounts)
○ Google Workspace, Google Analytics, YouTube, Google Ads APIs, etc.
○ Install: developers.google.com/api-client-library
● "Product-level" client libraries (higher level)
○ Custom client libraries made specifically for each product
○ Managing API service endpoints & security mostly taken care of
○ Only need to create a "client" to use API services
○ Install (Cloud/GCP & Firebase): cloud.google.com/apis/docs/cloud-client-libraries
○ Install (Maps): developers.google.com/places/web-service/client-library
● Some Google APIs families support both, e.g., Cloud
Google Cloud SDK and API client libraries
Google Cloud SDK: various tools to help you
use GCP services, platforms, tools, and APIs:
1. API client libraries
2. Command-line tools (gcloud, gsutil, etc.)
3. Cloud Shell and Cloud Editor
4. Emulators for local development & testing
cloud.google.com/sdk
cloud.google.com/apis/docs
OAuth2 or
API key
HTTP-based REST APIs 1
HTTP
2
Google APIs request-response workflow
● Application makes request
● Request received by service
● Process data, return response
● Results sent to application
(typical client-server model)
03
Calling Cloud APIs
from serverless
App Engine, Cloud Functions, Cloud Run
cloud.google.com/hosting-options#hosting-options
Google Cloud compute option spectrum
Compute
Engine
Kubernetes
Engine (GKE)
Cloud Run
on Anthos
Cloud Run
(fully-mgd)
App Engine
(Flexible)
App Engine
(Standard)
Cloud
Functions
Serverless common use cases App Engine Cloud Run
Cloud
Functions
Web services
Web app hosting/custom domains ✓ ✓
HTTP services ✓ ✓ ✓
Container hosting ✓
APIs
Web & mobile backends ✓ ✓
Internal/intranet apps ✓ ✓
Large-scale data processing ✓ ✓
Automation
Workflow & orchestration ✓ ✓
Event-driven automation ✓ ✓
GitOps: Git push-to-deploy (CD-only) ✓
Common use cases
Accessing Cloud APIs from serverless
Usage step (Cloud
Translation example) Python Node.js
Check Cloud API
documentation
cloud.google.com/translate/docs
/reference/libraries/v3/python
cloud.google.com/translate/docs
/reference/libraries/v3/nodejs
[local] Install API client
library
$ pip install google-cloud-translate $ npm install @google-cloud/translate
[cloud] Add to package
requirements file
flask
google-cloud-translate
"dependencies": {
"@google-cloud/translate": "^6.3.1",
"express": "^4.17.1",
},
Import library in code from google.cloud import translate
const {TranslationServiceClient} =
require('@google-cloud/translate');
Create an "API client"
client =
translate.TranslationServiceClient()
const client = new
TranslationServiceClient();
Call the Cloud API
rsp =
client.translate_text(request=data)
const [rsp] = await
client.translateText(data);
Flexibility in options
Cloud
Function
s
App
Engine
Cloud
Run
local
server
Cloud
Translation
My "Google Translate" MVP
goo.gle/2Y0ph5q
● "Nebulous" sample web app
○ Flask/Python app
○ Python 2 & 3 compatible
○ Uses Cloud Translation API
● Deployable to on-prem server
● Also GCP serverless compute
○ App Engine
○ Cloud Functions
○ Cloud Run
● With only config changes
● No changes to app code
Nebulous files
● Application files (Python)
● Configuration files
● Administrative files
○ Information files
○ Testing - CI/CD files
● Files differ per deployment
○ Mainly in configuration
● Application files identical
● Administrative files not part
of deployments*
Node.js
(all Cloud
deployments)
● Node.js 10+; less "drama" vs. Python (2 & 3)
● Swap in Express.js as web framework for Flask
● credentials.json only for running locally
● app.yaml only for App Engine; use Buildpacks for Cloud Run vs. Docker
● Same gcloud deploy commands as Python for each platform
Mini "My Google Translate" MVP
from flask import Flask, render_template, request
import google.auth
from google.cloud import translate
app = Flask(__name__)
_, PROJECT_ID = google.auth.default()
TRANSLATE = translate.TranslationServiceClient()
PARENT = 'projects/{}'.format(PROJECT_ID)
SOURCE, TARGET = ('en', 'English'), ('es', 'Spanish')
## translate() goes here
if __name__ == '__main__':
import os
app.run(debug=True, threaded=True, host='0.0.0.0',
port=int(os.environ.get('PORT', 8080)))
Mini "My Google Translate" MVP
const express = require('express');
const nunjucks = require('nunjucks');
const {TranslationServiceClient} = require('@google-cloud/translate');
const app = express();
app.use(express.urlencoded({extended: true}));
nunjucks.configure('templates', {autoescape: true, express: app});
const TRANSLATE = new TranslationServiceClient();
const PORT = process.env.PORT || 8080;
const SOURCE = ['en', 'English'];
const TARGET = ['es', 'Spanish'];
let parent;
TRANSLATE.getProjectId().then(result => parent = `projects/${result}`);
if (!process.env.FUNCTION_TARGET)
app.listen(PORT, () => console.log(`Listening on port ${PORT}`));
// translate() goes here
app.all('/', translate);
module.exports = {app};
Mini "My Google Translate" MVP (cont.)
@app.route('/', methods=['GET', 'POST'])
def translate(gcf_request=None):
local_request = gcf_request if gcf_request else request # GCF or not?
text = translated = None # reset all variables (GET)
if local_request.method == 'POST': # form submission (POST)
text = local_request.form['text'].strip()
if text:
data = {'contents': [text], 'parent': PARENT,
'target_language_code': TARGET[0]}
try:
rsp = TRANSLATE.translate_text(request=data)
except TypeError: # older call/backwards-compat
rsp = TRANSLATE.translate_text(**data)
translated = rsp.translations[0].translated_text
context = { # template context
'orig': {'text': text, 'lc': SOURCE},
'trans': {'text': translated, 'lc': TARGET},
}
return render_template('index.html', **context)
Mini "My Google Translate" MVP (cont.)
async function translate(req, rsp) {
let text = null;
let translated = null;
if (req.method === 'POST') {
text = req.body.text.trim();
if (text) {
const data = {
contents: [text],
parent: parent,
targetLanguageCode: TARGET[0]
};
const [response] = await TRANSLATE.translateText(data);
translated = response.translations[0].translatedText;
}
}
const context = {
orig: {text: text, lc: SOURCE},
trans: {text: translated, lc: TARGET}
};
rsp.render('index.html', context);
}
Supported app deployments
● Local (or hosted) Flask or Express.js server
○ Python 2.7, Python 3.7+, Node.js 10+
● App Engine
○ Python 2.7, Python 3.7+, Node.js 10, 12, 14, 16
● Cloud Functions
○ Python 3.7+, Node.js 10, 12, 14, 16
● Cloud Run (via Docker)
○ Python 2.7, Python 3.6+, Node.js 10+ (any)
● Cloud Run (via Cloud Buildpacks)
○ (automated) Python 3.9+, Node.js 14+
Deploying to serverless
● App Engine (requires app.yaml file)
○ (Python 2 only) Run pip install -t lib -r requirements.txt
○ Run gcloud app deploy
● Cloud Functions
○ Run gcloud functions deploy FUNCTION --runtime RUNTIME
--trigger-TRIGGER . . .
● Cloud Run (Cloud Buildpacks or Docker [requires Dockerfile])
○ Run gcloud run deploy SERVICE --platform managed --source . . .
○ Alternatively (register container then deploy vs. 1-step):
■ Run gcloud builds submit --tag gcr.io/PROJECT/IMG . . .
■ Run gcloud run deploy SERVICE --image gcr.io/PROJECT/IMG . . .
Application screenshots
● Overall objective: Help users learn serverless more comprehensively
● Specific goals
● Learn how to Access Cloud/Google APIs from serverless
● Identify differences between Cloud serverless compute platforms
● Demonstrate how to write one app deployable to all (it's possible!)
● Source code & codelabs (self-paced, hands-on tutorials); see "cloud" at:
● github.com/googlecodelabs/cloud-nebulous-serverless
● goo.gle/2Y0ph5q
App summary
04
More inspiration
Other examples & sample apps
Storage: listing buckets
from __future__ import print_function
from google.cloud import storage
GCS = storage.Client()
BUCKET = YOUR_BUCKET
# send bucket name & return fields to API, display results
print('n** Objects in bucket %r...' % BUCKET)
for f in GCS.list_blobs(BUCKET):
print(' %s (%s)' % (f['name'], f['size']))
Firestore: create & query for objects
from datetime import datetime
from google.cloud import firestore
def store_time(timestamp):
visits = fs_client.collection('visit')
visits.add({'timestamp': timestamp})
def fetch_times(limit):
visits = fs_client.collection('visit')
return visits.order_by(u'timestamp',
direction=firestore.Query.DESCENDING).limit(limit).stream()
fs_client = firestore.Client() # create Cloud Firestore client
print('** Adding another visit')
store_time(datetime.now()) # store a "visit" object
print('** Last 10 visits')
times = fetch_times(10) # fetch 10 most recent "visits"
for obj in times:
print('-', obj.to_dict()['timestamp'])
BigQuery: querying Shakespeare words
from google.cloud import bigquery
TITLE = "The most common words in all of Shakespeare's works"
QUERY = '''
SELECT LOWER(word) AS word, sum(word_count) AS count
FROM `bigquery-public-data.samples.shakespeare`
GROUP BY word ORDER BY count DESC LIMIT 10
'''
rsp = bigquery.Client().query(QUERY).result()
print('n*** Results for %r:n' % TITLE)
print('t'.join(col.name.upper() for col in rsp.schema)) # HEADERS
print('n'.join('t'.join(str(x) for x in row.values()) for row in rsp)) # DATA
Top 10 most common Shakespeare words
$ python bq_shake.py
*** Results for "The most common words in all of Shakespeare's works":
WORD COUNT
the 29801
and 27529
i 21029
to 20957
of 18514
a 15370
you 14010
my 12936
in 11722
that 11519
Simple sentiment & classification analysis
from google.cloud import language
TEXT = '''Google, headquartered in Mountain View, unveiled the new
Android phone at the Consumer Electronics Show. Sundar Pichai said
in his keynote that users love their new Android phones.'''
NL = language.LanguageServiceClient()
document = language.types.Document(content=TEXT,
type=language.enums.Document.Type.PLAIN_TEXT)
print('TEXT:', TEXT) # sentiment analysis
sent = NL.analyze_sentiment(document).document_sentiment
print('nSENTIMENT: score (%.2f), magnitude (%.2f)' % (sent.score, sent.magnitude))
print('nCATEGORIES:') # content classification
categories = NL.classify_text(document).categories
for cat in categories:
print('* %s (%.2f)' % (cat.name[1:], cat.confidence))
Simple sentiment & classification analysis
$ python nl_sent_simple.py
TEXT: Google, headquartered in Mountain View, unveiled the new Android
phone at the Consumer Electronics Show. Sundar Pichai said in
his keynote that users love their new Android phones.
SENTIMENT: score (0.20), magnitude (0.50)
CATEGORIES:
* Internet & Telecom (0.76)
* Computers & Electronics (0.64)
* News (0.56)
Custom intelligence in Gmail
Analyze Google Workspace (formerly G Suite) data with GCP
Gmail message processing with GCP
Gmail
Cloud
Pub/Sub
Cloud
Functions
Cloud
Vision
Workspace
(formerly G Suite)
GCP
Star
message
Message
notification
Trigger
function
Extract
images
Categorize
images
Inbox augmented with Cloud Function
● Gmail API: sets up notification forwarding to Cloud Pub/Sub
● developers.google.com/gmail/api/guides/push
● Pub/Sub: triggers logic hosted by Cloud Functions
● cloud.google.com/functions/docs/calling/pubsub
● Cloud Functions: "orchestrator" accessing GCP (and Google Workspace/G Suite) APIs
● Combine all of the above to add custom intelligence to Gmail
● Deep dive code blog post
● cloud.google.com/blog/products/application-development/
adding-custom-intelligence-to-gmail-with-serverless-on-gcp
● Application source code
● github.com/GoogleCloudPlatform/cloud-functions-gmail-nodejs
App summary
Big data analysis to slide presentation
Access GCP tools from Google Workspace (formerly G Suite)
Store big data results
Visualize big data results
Ingest data from Sheets
Link to chart in Sheets
Supercharge Workspace (G Suite) with GCP
Workspace (G Suite) GCP
BigQuery
Apps Script
Slides Sheets
Application
request
Big data
analytics
App summary
● Leverage GCP and build the "final mile" with Google Workspace (formerly G Suite)
● Driven by Google Apps Script
● Google BigQuery for data analysis
● Google Sheets for visualization
● Google Slides for presentable results
● "Glued" together w/Google Workspace (formerly G Suite) serverless
● Build this app (codelab): g.co/codelabs/bigquery-sheets-slides
● Video and blog post: bit.ly/2OcptaG
● Application source code: github.com/googlecodelabs/bigquery-sheets-slides
● Presented at Google Cloud NEXT (Jul 2018 [DEV229] & Apr 2019 [DEV212])
● cloud.withgoogle.com/next18/sf/sessions/session/156878
● cloud.withgoogle.com/next/sf/sessions?session=DEV212
05
Summary
Other resources & wrap-up
Session Summary
● Google provides more than just apps
○ We're more than search, YouTube, Android, Chrome, and Gmail
○ Much of our tech available to developers through our APIs
● Quick tour of Google (Cloud) APIs & developer tools
○ APIs for storage, networking, security, data & machine learning
○ Serverless: frees developers from infrastructure
■ So you can focus on building solutions
● Same client library user-experience across Cloud APIs
● Accessible in same way across all Cloud compute platforms
Other Google APIs & platforms
● Google Workspace (G Suite) (code Gmail, Drive, Docs, Sheets, Slides!)
○ developers.google.com/gsuite
● Firebase (mobile development platform and RT DB plus ML-Kit)
○ firebase.google.com and firebase.google.com/docs/ml-kit
● Google Data Studio (data visualization, dashboards, etc.)
○ datastudio.google.com/overview
○ goo.gle/datastudio-course
● Actions on Google/Assistant/DialogFlow (voice apps)
○ developers.google.com/actions
● YouTube (Data, Analytics, and Livestreaming APIs)
○ developers.google.com/youtube
● Google Maps (Maps, Routes, and Places APIs)
○ developers.google.com/maps
● Flutter (native apps [Android, iOS, web] w/1 code base[!])
○ flutter.dev
Thank you! Questions?
Wesley Chun (@wescpy)
Sample app: goo.gl/2Y0ph5q
Progress bars: goo.gl/69EJVw

More Related Content

Accessing Google Cloud APIs

  • 1. Accessing Google Cloud APIs from Google Cloud compute platforms Wesley Chun - @wescpy Developer Advocate, Google Developer Advocate, Google Cloud ● Mission: enable current and future developers everywhere to be successful using Google Cloud and other Google developer tools & APIs ● Focus: GCP serverless (App Engine, Cloud Functions, Cloud Run); higher education, Google Workspace, GCP AI/ML APIs; multi-product use cases ● Content: speak to developers globally; make videos, create code samples, produce codelabs (free, self-paced, hands-on tutorials), publish blog posts About the speaker Previous experience / background ● Software engineer & architect for 20+ years ○ Yahoo!, Sun, HP, Cisco, EMC, Xilinx ○ Original Yahoo!Mail engineer/SWE ● Technical trainer, teacher, instructor ○ Taught Math, Linux, Python since 1983 ○ Private corporate trainer ○ Adjunct CS Faculty at local SV college ● Python community member ○ Popular Core Python series author ○ Python Software Foundation Fellow ● AB (Math/CS) & CMP (Music/Piano), UC Berkeley and MSCS, UC Santa Barbara ● Adjunct Computer Science Faculty, Foothill College (Silicon Valley)
  • 2. 01 Google & APIs Our technologies for your apps
  • 5. 02 Accessing Google APIs What you need to do The first word on Security Authentication ("authn") vs authorization ("authz") ● authn: you are who you say you are ○ login & passwd ○ handprint authentication ○ retina scan ● authz: okay, you are who you say you are, but can you haz data? ○ OAuth2 - mostly authz, but some authn ○ Mostly about 3rd-party access to data ○ Users must give YOUR code access to THEIR data ○ Most of the time when you see "auth", it refers to authz ● Some refer to this as "consent" vs. "credentials…" which is which?
  • 6. General steps 1. Go to Cloud Console 2. Login to Google/Gmail account (Workspace domain may require admin approval) 3. Create project (per application) 4. Enable APIs to use 5. Enable billing (CC, Free Trial, etc.) 6. Download client library(ies) 7. Create & download credentials 8. Write code 9. Run code (may need to authorize) Google APIs: how to use Costs and pricing ● GCP: pay-per-use ● Google Workspace: subscription ● GCP Free Trial ($300/1Q, CC req'd) ● GCP "Always Free" tier ○ Most products have free tier ○ Daily or monthly quota ○ Must exceed to incur billing ● More on both programs at cloud.google.com/free Cloud/GCP console console.cloud.google.com ● Hub of all developer activity ● Applications == projects ○ New project for new apps ○ Projects have a billing acct ● Manage billing accounts ○ Financial instrument required ○ Personal or corporate credit cards, Free Trial, and education grants ● Access GCP product settings ● Manage users & security ● Manage APIs in devconsole
  • 7. Collaborating & sharing ● Sharing: great way to see student work or have teams work on one project ● IAM (Identity & Access Mgmt): owner & editor most useful access levels cloud.google.com/iam/docs/understanding-roles ● View application statistics ● En-/disable Google APIs ● Obtain application credentials Using Google APIs goo.gl/RbyTFD API manager aka Developers Console (devconsole) console.developers.google.com
  • 8. Three different credentials types ● Simple: API keys (to access public data) ○ Simplest form of authorization: an API key; tied to a project ○ Allows access to public data ○ Do not put in code, lose, or upload to GitHub! (can be restricted however) ○ Supported by: Google Maps, (some) YouTube, (some) GCP, etc. ● Authorized: OAuth client IDs (to access data owned by [human] user) ○ Provides additional layer of security via OAuth2 (RFC 6749) ○ Owner must grant permission for your app to access their data ○ Access granularity determined by requested permissions (user scopes) ○ Supported by: Google Workspace, (some) YouTube, (some) GCP, etc. ● Authorized: service accounts (to access data owned by an app/robot user) ○ Provides additional layer of security via OAuth2 or JWT (RFC 7519) ○ Project owning data grants permission implicitly; requires public-private key-pair ○ Access granularity determined by Cloud IAM permissions granted to service account key-pair ○ Supported by: GCP, (some) Google Workspace, etc.
  • 9. & Google APIs client libraries for many languages; demos in developers.google.com/api- client-library cloud.google.com/apis/docs /cloud-client-libraries Two different client library "styles" ● "Platform-level" client libraries (lower level) ○ Supports multiple products as a "lowest-common denominator" ○ Manage API service endpoints (setup & use) ○ Manage authorization (API keys, OAuth client IDs, service accounts) ○ Google Workspace, Google Analytics, YouTube, Google Ads APIs, etc. ○ Install: developers.google.com/api-client-library ● "Product-level" client libraries (higher level) ○ Custom client libraries made specifically for each product ○ Managing API service endpoints & security mostly taken care of ○ Only need to create a "client" to use API services ○ Install (Cloud/GCP & Firebase): cloud.google.com/apis/docs/cloud-client-libraries ○ Install (Maps): developers.google.com/places/web-service/client-library ● Some Google APIs families support both, e.g., Cloud
  • 10. Google Cloud SDK and API client libraries Google Cloud SDK: various tools to help you use GCP services, platforms, tools, and APIs: 1. API client libraries 2. Command-line tools (gcloud, gsutil, etc.) 3. Cloud Shell and Cloud Editor 4. Emulators for local development & testing cloud.google.com/sdk cloud.google.com/apis/docs OAuth2 or API key HTTP-based REST APIs 1 HTTP 2 Google APIs request-response workflow ● Application makes request ● Request received by service ● Process data, return response ● Results sent to application (typical client-server model)
  • 11. 03 Calling Cloud APIs from serverless App Engine, Cloud Functions, Cloud Run cloud.google.com/hosting-options#hosting-options Google Cloud compute option spectrum Compute Engine Kubernetes Engine (GKE) Cloud Run on Anthos Cloud Run (fully-mgd) App Engine (Flexible) App Engine (Standard) Cloud Functions
  • 12. Serverless common use cases App Engine Cloud Run Cloud Functions Web services Web app hosting/custom domains ✓ ✓ HTTP services ✓ ✓ ✓ Container hosting ✓ APIs Web & mobile backends ✓ ✓ Internal/intranet apps ✓ ✓ Large-scale data processing ✓ ✓ Automation Workflow & orchestration ✓ ✓ Event-driven automation ✓ ✓ GitOps: Git push-to-deploy (CD-only) ✓ Common use cases Accessing Cloud APIs from serverless Usage step (Cloud Translation example) Python Node.js Check Cloud API documentation cloud.google.com/translate/docs /reference/libraries/v3/python cloud.google.com/translate/docs /reference/libraries/v3/nodejs [local] Install API client library $ pip install google-cloud-translate $ npm install @google-cloud/translate [cloud] Add to package requirements file flask google-cloud-translate "dependencies": { "@google-cloud/translate": "^6.3.1", "express": "^4.17.1", }, Import library in code from google.cloud import translate const {TranslationServiceClient} = require('@google-cloud/translate'); Create an "API client" client = translate.TranslationServiceClient() const client = new TranslationServiceClient(); Call the Cloud API rsp = client.translate_text(request=data) const [rsp] = await client.translateText(data);
  • 13. Flexibility in options Cloud Function s App Engine Cloud Run local server Cloud Translation My "Google Translate" MVP goo.gle/2Y0ph5q ● "Nebulous" sample web app ○ Flask/Python app ○ Python 2 & 3 compatible ○ Uses Cloud Translation API ● Deployable to on-prem server ● Also GCP serverless compute ○ App Engine ○ Cloud Functions ○ Cloud Run ● With only config changes ● No changes to app code Nebulous files ● Application files (Python) ● Configuration files ● Administrative files ○ Information files ○ Testing - CI/CD files ● Files differ per deployment ○ Mainly in configuration ● Application files identical ● Administrative files not part of deployments*
  • 14. Node.js (all Cloud deployments) ● Node.js 10+; less "drama" vs. Python (2 & 3) ● Swap in Express.js as web framework for Flask ● credentials.json only for running locally ● app.yaml only for App Engine; use Buildpacks for Cloud Run vs. Docker ● Same gcloud deploy commands as Python for each platform Mini "My Google Translate" MVP from flask import Flask, render_template, request import google.auth from google.cloud import translate app = Flask(__name__) _, PROJECT_ID = google.auth.default() TRANSLATE = translate.TranslationServiceClient() PARENT = 'projects/{}'.format(PROJECT_ID) SOURCE, TARGET = ('en', 'English'), ('es', 'Spanish') ## translate() goes here if __name__ == '__main__': import os app.run(debug=True, threaded=True, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
  • 15. Mini "My Google Translate" MVP const express = require('express'); const nunjucks = require('nunjucks'); const {TranslationServiceClient} = require('@google-cloud/translate'); const app = express(); app.use(express.urlencoded({extended: true})); nunjucks.configure('templates', {autoescape: true, express: app}); const TRANSLATE = new TranslationServiceClient(); const PORT = process.env.PORT || 8080; const SOURCE = ['en', 'English']; const TARGET = ['es', 'Spanish']; let parent; TRANSLATE.getProjectId().then(result => parent = `projects/${result}`); if (!process.env.FUNCTION_TARGET) app.listen(PORT, () => console.log(`Listening on port ${PORT}`)); // translate() goes here app.all('/', translate); module.exports = {app}; Mini "My Google Translate" MVP (cont.) @app.route('/', methods=['GET', 'POST']) def translate(gcf_request=None): local_request = gcf_request if gcf_request else request # GCF or not? text = translated = None # reset all variables (GET) if local_request.method == 'POST': # form submission (POST) text = local_request.form['text'].strip() if text: data = {'contents': [text], 'parent': PARENT, 'target_language_code': TARGET[0]} try: rsp = TRANSLATE.translate_text(request=data) except TypeError: # older call/backwards-compat rsp = TRANSLATE.translate_text(**data) translated = rsp.translations[0].translated_text context = { # template context 'orig': {'text': text, 'lc': SOURCE}, 'trans': {'text': translated, 'lc': TARGET}, } return render_template('index.html', **context)
  • 16. Mini "My Google Translate" MVP (cont.) async function translate(req, rsp) { let text = null; let translated = null; if (req.method === 'POST') { text = req.body.text.trim(); if (text) { const data = { contents: [text], parent: parent, targetLanguageCode: TARGET[0] }; const [response] = await TRANSLATE.translateText(data); translated = response.translations[0].translatedText; } } const context = { orig: {text: text, lc: SOURCE}, trans: {text: translated, lc: TARGET} }; rsp.render('index.html', context); } Supported app deployments ● Local (or hosted) Flask or Express.js server ○ Python 2.7, Python 3.7+, Node.js 10+ ● App Engine ○ Python 2.7, Python 3.7+, Node.js 10, 12, 14, 16 ● Cloud Functions ○ Python 3.7+, Node.js 10, 12, 14, 16 ● Cloud Run (via Docker) ○ Python 2.7, Python 3.6+, Node.js 10+ (any) ● Cloud Run (via Cloud Buildpacks) ○ (automated) Python 3.9+, Node.js 14+
  • 17. Deploying to serverless ● App Engine (requires app.yaml file) ○ (Python 2 only) Run pip install -t lib -r requirements.txt ○ Run gcloud app deploy ● Cloud Functions ○ Run gcloud functions deploy FUNCTION --runtime RUNTIME --trigger-TRIGGER . . . ● Cloud Run (Cloud Buildpacks or Docker [requires Dockerfile]) ○ Run gcloud run deploy SERVICE --platform managed --source . . . ○ Alternatively (register container then deploy vs. 1-step): ■ Run gcloud builds submit --tag gcr.io/PROJECT/IMG . . . ■ Run gcloud run deploy SERVICE --image gcr.io/PROJECT/IMG . . . Application screenshots
  • 18. ● Overall objective: Help users learn serverless more comprehensively ● Specific goals ● Learn how to Access Cloud/Google APIs from serverless ● Identify differences between Cloud serverless compute platforms ● Demonstrate how to write one app deployable to all (it's possible!) ● Source code & codelabs (self-paced, hands-on tutorials); see "cloud" at: ● github.com/googlecodelabs/cloud-nebulous-serverless ● goo.gle/2Y0ph5q App summary 04 More inspiration Other examples & sample apps
  • 19. Storage: listing buckets from __future__ import print_function from google.cloud import storage GCS = storage.Client() BUCKET = YOUR_BUCKET # send bucket name & return fields to API, display results print('n** Objects in bucket %r...' % BUCKET) for f in GCS.list_blobs(BUCKET): print(' %s (%s)' % (f['name'], f['size'])) Firestore: create & query for objects from datetime import datetime from google.cloud import firestore def store_time(timestamp): visits = fs_client.collection('visit') visits.add({'timestamp': timestamp}) def fetch_times(limit): visits = fs_client.collection('visit') return visits.order_by(u'timestamp', direction=firestore.Query.DESCENDING).limit(limit).stream() fs_client = firestore.Client() # create Cloud Firestore client print('** Adding another visit') store_time(datetime.now()) # store a "visit" object print('** Last 10 visits') times = fetch_times(10) # fetch 10 most recent "visits" for obj in times: print('-', obj.to_dict()['timestamp'])
  • 20. BigQuery: querying Shakespeare words from google.cloud import bigquery TITLE = "The most common words in all of Shakespeare's works" QUERY = ''' SELECT LOWER(word) AS word, sum(word_count) AS count FROM `bigquery-public-data.samples.shakespeare` GROUP BY word ORDER BY count DESC LIMIT 10 ''' rsp = bigquery.Client().query(QUERY).result() print('n*** Results for %r:n' % TITLE) print('t'.join(col.name.upper() for col in rsp.schema)) # HEADERS print('n'.join('t'.join(str(x) for x in row.values()) for row in rsp)) # DATA Top 10 most common Shakespeare words $ python bq_shake.py *** Results for "The most common words in all of Shakespeare's works": WORD COUNT the 29801 and 27529 i 21029 to 20957 of 18514 a 15370 you 14010 my 12936 in 11722 that 11519
  • 21. Simple sentiment & classification analysis from google.cloud import language TEXT = '''Google, headquartered in Mountain View, unveiled the new Android phone at the Consumer Electronics Show. Sundar Pichai said in his keynote that users love their new Android phones.''' NL = language.LanguageServiceClient() document = language.types.Document(content=TEXT, type=language.enums.Document.Type.PLAIN_TEXT) print('TEXT:', TEXT) # sentiment analysis sent = NL.analyze_sentiment(document).document_sentiment print('nSENTIMENT: score (%.2f), magnitude (%.2f)' % (sent.score, sent.magnitude)) print('nCATEGORIES:') # content classification categories = NL.classify_text(document).categories for cat in categories: print('* %s (%.2f)' % (cat.name[1:], cat.confidence)) Simple sentiment & classification analysis $ python nl_sent_simple.py TEXT: Google, headquartered in Mountain View, unveiled the new Android phone at the Consumer Electronics Show. Sundar Pichai said in his keynote that users love their new Android phones. SENTIMENT: score (0.20), magnitude (0.50) CATEGORIES: * Internet & Telecom (0.76) * Computers & Electronics (0.64) * News (0.56)
  • 22. Custom intelligence in Gmail Analyze Google Workspace (formerly G Suite) data with GCP
  • 23. Gmail message processing with GCP Gmail Cloud Pub/Sub Cloud Functions Cloud Vision Workspace (formerly G Suite) GCP Star message Message notification Trigger function Extract images Categorize images Inbox augmented with Cloud Function
  • 24. ● Gmail API: sets up notification forwarding to Cloud Pub/Sub ● developers.google.com/gmail/api/guides/push ● Pub/Sub: triggers logic hosted by Cloud Functions ● cloud.google.com/functions/docs/calling/pubsub ● Cloud Functions: "orchestrator" accessing GCP (and Google Workspace/G Suite) APIs ● Combine all of the above to add custom intelligence to Gmail ● Deep dive code blog post ● cloud.google.com/blog/products/application-development/ adding-custom-intelligence-to-gmail-with-serverless-on-gcp ● Application source code ● github.com/GoogleCloudPlatform/cloud-functions-gmail-nodejs App summary Big data analysis to slide presentation Access GCP tools from Google Workspace (formerly G Suite)
  • 25. Store big data results
  • 26. Visualize big data results Ingest data from Sheets
  • 27. Link to chart in Sheets
  • 28. Supercharge Workspace (G Suite) with GCP Workspace (G Suite) GCP BigQuery Apps Script Slides Sheets Application request Big data analytics App summary ● Leverage GCP and build the "final mile" with Google Workspace (formerly G Suite) ● Driven by Google Apps Script ● Google BigQuery for data analysis ● Google Sheets for visualization ● Google Slides for presentable results ● "Glued" together w/Google Workspace (formerly G Suite) serverless ● Build this app (codelab): g.co/codelabs/bigquery-sheets-slides ● Video and blog post: bit.ly/2OcptaG ● Application source code: github.com/googlecodelabs/bigquery-sheets-slides ● Presented at Google Cloud NEXT (Jul 2018 [DEV229] & Apr 2019 [DEV212]) ● cloud.withgoogle.com/next18/sf/sessions/session/156878 ● cloud.withgoogle.com/next/sf/sessions?session=DEV212
  • 29. 05 Summary Other resources & wrap-up Session Summary ● Google provides more than just apps ○ We're more than search, YouTube, Android, Chrome, and Gmail ○ Much of our tech available to developers through our APIs ● Quick tour of Google (Cloud) APIs & developer tools ○ APIs for storage, networking, security, data & machine learning ○ Serverless: frees developers from infrastructure ■ So you can focus on building solutions ● Same client library user-experience across Cloud APIs ● Accessible in same way across all Cloud compute platforms
  • 30. Other Google APIs & platforms ● Google Workspace (G Suite) (code Gmail, Drive, Docs, Sheets, Slides!) ○ developers.google.com/gsuite ● Firebase (mobile development platform and RT DB plus ML-Kit) ○ firebase.google.com and firebase.google.com/docs/ml-kit ● Google Data Studio (data visualization, dashboards, etc.) ○ datastudio.google.com/overview ○ goo.gle/datastudio-course ● Actions on Google/Assistant/DialogFlow (voice apps) ○ developers.google.com/actions ● YouTube (Data, Analytics, and Livestreaming APIs) ○ developers.google.com/youtube ● Google Maps (Maps, Routes, and Places APIs) ○ developers.google.com/maps ● Flutter (native apps [Android, iOS, web] w/1 code base[!]) ○ flutter.dev Thank you! Questions? Wesley Chun (@wescpy) Sample app: goo.gl/2Y0ph5q Progress bars: goo.gl/69EJVw