[MUSIC PLAYING]
GRANT TIMMERMAN: So my name's Grant Timmerman.
I'm a developer , platform engineer in Google Cloud
Platform.
Previously, I led open source for G Suite APIs.
And in particular, I love Node, GitHub.
And I play the alto sax in my spare time.
So the proposed agenda for today is, first,
what is functions as a service?
What is the difference between functions and microservices
running Google Cloud functions?
And then this new thing, the Functions framework--
you might have heard of it.
It's an open source platform that powers Google
Cloud Functions for Node.
And then a couple of quick demos.
So first, when you're thinking about serverless,
we're talking about Google Cloud Functions
and being able to develop functions on-the-fly.
So what is functions as a service?
Well, so functions as a service is on-demand infrastructure
for executing your code.
As Kevin said, it's where you can just solely
focus on your application, rather
than having to worry about the infrastructure that powers it.
So traditionally, you have servers where you
have to maintain your hardware.
You have maintenance, spare parts.
You have staff, DevOps.
You have server rooms.
You have to worry about electricity cooling.
And you might have some idle time with these servers.
And you might have to patch your servers
and patch your hardware.
And you also have to overprovision,
if you have traffic spikes, in case it's Black Friday
or you need to make sure that your load is kept.
And so this can cost a lot of money and cost
a lot of time, something that you don't want to do.
Well, so why do we really think about servers?
And why can't we just focus on our core application,
what we're good at?
Why can't we let Google do the hard part?
Well, so that's where our functions as a service
comes in.
So it's part of the serverless movement.
And it's where your logic is written by the application
developer, runs in a managed stateless computing container,
and you have special triggers, like HTTP, Pub/Sub topics,
or dual cloud storage upload that is
able to trigger your function.
It's ephemeral.
So that means your function invocation,
it won't have global state.
And it's also managed by a third party.
So serverless-- Google Cloud Functions
is a serverless environment to build and connect services
with code.
Basically, an event is emitted, a function is triggered,
and your code is executed.
You can trigger HTTP functions from all sorts of environments,
also from Drive, Calendar, Gmail, and Maps.
Or you can have your background functions
trigger from Google Cloud Storage, Stackdriver, Cloud
Pub/Sub, Firebase, et cetera.
Fourth is that it's managed.
So your cloud function might be running in one of Google's data
centers, one of these racks.
You don't really need to worry about where it's being run.
So Deep Kapadia stated that Google Cloud Functions
is really useful for "The New York Times."
So "Google Cloud serverless platform
allows us to focus on building product features without having
to worry about provisioning, scaling,
and configuration management for the underlying infrastructure.
The tools and abstractions have been a game changer,
allowing teams to iterate and scale with ease."
Now onto, what's the difference between monoliths
and microservices?
Well, so traditionally with a monolith,
you have a single application.
It's one giant app.
You deploy it all at once.
And fundamentally, there's different pieces of logic.
You might have a couple databases, a couple application
pieces of logic.
And the idea of microservices is to really break away
this monolithic application and configure your points of entry,
which might be anything from Google Cloud storage uploads,
HTTP triggers, Pub/Sub events, and be able to trigger these
independently.
So writing functions in Google Cloud Functions--
well, so for writing functions, you
can specify a request URL, which might be something like this,
and provide a body.
So you can just provide JSON, which could be Grant.
And then the response, you'll just get 200, OK.
And maybe you'll get a response body of "Hello, Grant!"
Cloud Function supports these runtimes, so Node 6, 8,
and 10, Python 3.7, Go 1.11 and 1.12 alpha, as well as Java 8.
Behind the scenes, we have an HTTP server endpoint.
We provide a fully qualified domain, an SSL certificate,
autoscaling, as your users hit your function more often.
And we also integrate with Stackdriver logging
and Stackdriver error reporting.
You can deploy from the Cloud Console UI,
or from the command line.
Onto languages and the CLI.
Well, so for Node, a Google Cloud Function
will look something like this.
It's your typical node where you can export a variable.
This is just "Hello World."
And we're powering this through Express 4.
And you can simply just res.status, 200,
and send the message.
Python is pretty similar.
It uses Flask.
And you can pass the args, if you want,
and just return a string, "Hello World".
And for Go, we use the native HTTP client.
And you can send your response natively in Go.
You can deploy Google Cloud Functions from the CLI
and do everything from calling functions, listing
all your functions, deploying, deleting, and listing
all the event types.
There's some advanced options for the CLI
that allows you to specify different runtimes, different
memory allocations, up to 2 gigabytes, a number of retries,
in case your function fails.
And you can also specify that this function
will run as a service-account.
Timeouts, environment variables, triggers, et cetera.
And you can learn more at cloud.google.com/functions.
Now, I'm really excited to talk about this part.
This is something that I've been working
on with [? Slavic ?] and the Google Cloud Functions team.
This is the Functions Framework.
So what is the Functions Framework?
Well, it's really a new underlying backbone
for Google Cloud Functions.
It's an open source project.
It's under github.com/Googl eCloudPlatform/f
unctions-framework.
And it enables for local development of your code.
If you're wishing to not spend 30 to 60 seconds to wait
for your functions to deploy, you
can simply locally install the Functions Framework
and have the same environment that's
used with Google Cloud Functions,
at least for Node 10 right now.
And we also provide a transparent [INAUDIBLE]..
And so you can pin your Functions Framework
on a specific version, so that you know that your function is
going to run in the same environment,
even if you G Cloud Functions [INAUDIBLE]..
Inside the Functions Framework, what does it do?
Well, so it does a light transformation of your code.
It first loads the source code.
It creates a light HTTP server--
in this case, Express 4 for Node.
And it creates a route for your request.
There's a preset Docker file.
You can see it on the GitHub repo.
And we spin up container instances
in response to increased demand.
Cool.
Now I have a couple of demos that I would love to show you.
So first here, we have our Google Cloud Platform projects
where I have deployed a function called BTC.
A simple way to invoke a function
is just to call the URL.
And here, I actually have a function deployed
that's calling the Coinbase API and getting the current bitcoin
price at a certain date.
So in 2017, you could buy a single bitcoin for $2,100.
See in 2019, it's increased to $5,400.
Let's go try to run this locally, though.
So with the new Functions Framework,
we can simply start the Functions Framework.
And so this is using the local NPM package
and just serving a local host URL.
And I have the query parameter set up here,
and it's really exactly the same code.
So you can see in 2016, et cetera, and the price changes.
We can also, instead of powering our functions locally,
let's say we want to go and test against our deployed version.
Well, so rather than having to go to Chrome,
maybe we want to do this from the terminal.
We can just use gcloud and call the function,
and it'll execute and provide the data right then and there.
Let's say you have a Google Sheet,
and you want to use Google Cloud Functions to power
some of the data processing.
Well, I think Google Sheets is a very flexible UI.
And it's really powerful when you can combine Google Cloud
Functions and Google Sheets.
So here, I've created a custom function called BTC_CF.
It takes in a date.
And you can simply press Enter, and this
will call the Cloud Function, which we just deployed.
And it'll return the exact body response from that.
So the powerful part about Google Sheets
is that you can simply just drag and drop or click
the bottom right corner, and you can
see there's all these prices for all these dates.
And it's powered by Google Cloud Functions.
So how does this work, you might ask?
Well, so we can go to Tools and then Script Editor,
and we have some lightweight app script code.
Basically, we provide the date in this format.
And then we do a URL fetch, which is just
a fetch to this cloud endpoint.
And then we parse with JSON.
And we just want the amount, and so we just return that.
So that's really cool.
Let's see.
So those are three different ways
in which you can call your Cloud Function-- from the command
line, from a browser, and from Google Sheets.
So in summary, we showed what functions as a service
is, functions versus microservices,
writing Google Cloud Functions, the open source function
framework, and a demo of three ways in which you
can call Cloud Functions.
You can learn more about the Functions Framework
at git.io/gcf-ff.
Thank you, so much.
My name is Grant Timmerman.
I would love, if you have any questions, to tweet at me
@granttimmerman.
Thank you.
[MUSIC PLAYING]