GRANT: Hey cloud developers.
My name is Grant.
VINNY: And my name is Vinny.
GRANT: And we're here to get you started
using Google Cloud Functions.
Google Cloud Functions makes it easy to run and scale
your code in the cloud that listens and responds to events.
VINNY: The Functions Framework lets
you write lightweight functions that run in many environments,
including Cloud Functions, your local development
machine, Cloud Run and Cloud Run on GKE,
and Knative-based environments.
The library is open source on GitHub.
And the Functions Framework is used in the production
by Google Cloud Functions.
So Grant, what is the Functions Framework?
GRANT: The Functions Framework defines an interface
for building modular services.
This means that rather than emulating
how your function will run in production,
you can see exactly how your code will perform when testing
it on your local machine.
The Functions Framework is also Knativ-compatible,
supporting Knative eventing, which
makes it possible to port your functions between Knative
environments.
Specifically, the Functions Framework
defines a port for specifying HTTP functions,
a target name for the function, and then lastly, a signature
type, either an HTTP or event, for the type of event.
VINNY: The Functions Framework can be used in two ways,
either as a CLI, a Command Line Script, that you
run in your terminal to start the function server,
useful for local testing, or as a library, a dependency
for your application.
This is useful for staging a production Google Cloud
Functions environment on your machine.
You can specify an explicit dependency
on a version of the Functions framework.
And if you don't specify a version,
Google Cloud Functions will automatically choose the latest
Functions Framework.
Now let's look at that open-source framework for Node.
It's used in production for Google Cloud Functions running
Node 10.
Within the package, the module uses Express.
Express is a fast, unopinionated, minimalistic web
framework for Node.js that makes developing services a breeze.
It's a project of the Node.js Foundation,
and it's used by the Functions framework.
GRANT: Let's get started with the Functions
framework for Node.js.
Let's first run the command npm install @google cloud/functions
framework.
This will create a package.json in your current directory.
Create an index.js file with the following contents.
So what are we doing here?
Well, we're exporting a function called Hello, World.
The function accepts two parameters, req,
for the express request object, and res,
for the express response object.
Next, we're sending the string, Hello, World, in the response.
To start the server, run the following command--
npx @google cloud/functions framework.
Open localhost in your browser, and see Hello, World.
Awesome.
VINNY: So you your function running on your computer,
but how do you get it running in production, like in a real URL?
GRANT: Good point, Vinny.
Since the Functions Framework is the same library that
runs Google Cloud Functions, we can deploy our same Node
application with no modifications.
So let's deploy the code to Google Cloud Functions.
Using gcloud in our directory with our function,
we can call gcloud functions deploy,
specifying our function name--
trigger with HTTP-- and using Node 10.
After a bit, we'll get a live URL.
If I copy the URL and paste it into my web browser,
I'll invoke my function.
But where did our console.log statements go?
Vinny?
VINNY: Well, on my machine, I'll see the logs in the terminal
where I ran the function.
But in the cloud, I'll see them on Stackdriver.
This is because Google Cloud Functions
has an integration with Stackdriver where
logs written to standard out or standard error
will appear directly in Cloud Console.
GRANT: And that's like Google Cloud Functions Framework.
To learn more about the Functions Framework,
check it out on GitHub.
We have so many useful guides on how
to do things like how to deploy the Cloud Run using TypeScript,
using the Node Debugger, et cetera.
The link to the GitHub repo is in the description below.
VINNY: And be sure to subscribe right here on the Google Cloud
Platform channel on YouTube to get notified of new videos
about Cloud Functions we'll see you here next time.