By now you may have heard of serverless functions, also called cloud functions. Often these are AWS lambda functions, but did you know Google Cloud has these serverless functions too? This post will guide you through creating your first Google Cloud Function and show you the basics of how to use it. But first, let’s explain why they’re important.

A cloud with the word function signifying google cloud functions

Why Use Google Cloud Functions?

With all the excitement around serverless functions, you may be wondering, “Why are these even important?” Well, it comes down to cost.

When you’re running things in the cloud, you pay only for the resources you use. You’re renting these resources from whatever cloud provider you have. To keep costs down, you want to rent as few resources as possible while still making sure you have enough to support whatever your software’s doing.

With Google Cloud Functions, you don’t have to reserve a full server to do your work. You rent the time that your function is actually executing. This means that instead of paying for the full 100% of the time the server would be running in the cloud, you only have to pay for the time that the Google Cloud function is actually executing a request. This drastically reduces your cost.

However, there’s a downside. Because you aren’t renting out a full server 100% of the time, you have what’s called a cold start. Your Google Cloud function isn’t loaded into memory, so the Google infrastructure has to load it. When this happens, the server takes a hit and makes the request wait a little bit. Depending on the architecture and what the function does, this wait can last for 400 to 500 milliseconds. Whether this wait time is acceptable and how you can lessen it is outside the scope of this article. But I want to make you aware of it.

Your First Google Cloud Function

For your first function, let’s create a simple “Hello World!”-type function. When you create a function in Google Cloud, this is what they give you if you keep the default steps. For these steps, I ‘m going to assume you have some basic Google Cloud knowledge and currently have a project set up where you can create this function. If not, follow this tutorial from Google.

For your project, use the menu on the left-hand side of the screen. Under the Compute section, select Cloud Functions. It looks something like this:

From there, you should have a screen that lists all of your Cloud Functions, which should be empty right now. On the top bar, click Create Function. It looks something like this:

Now let’s walk through the wizard.

  1. Change the name of your function to something that makes sense for you.
  2. Keep the memory allocated to the default 256 MB right now.
  3. Notice the URL. You’ll need that a little later.
  4. Keep the Allow unauthenticated invocations box checked. I’ll explain a little bit later what that means.
  5. You want the inline editor for right now.
  6. Keep the runtime as the default Node.js 8.
  7. The inline editor should have a “Hello world!”-type application already filled, so we won’t touch that.
  8. The Function to execute is also already set up correctly, so we won’t touch that.
  9. Click Create to deploy the cloud function. It may take a few minutes, but you should see something like this:

If you do, great! Let’s execute that URL you had before.

If you don’t remember it, click your function name, and then click the Trigger tab. The URL will be listed there. Run it inside a new web browser tab, and you should see “Hello World!”

Congratulations. You’ve created your first Google Cloud function!

Deploying With the gcloud Tool

While the last step used the inline editor to deploy, that isn’t useful in an actual production setting. Let’s demonstrate how you’d deploy the same thing using Google’s SDK tool gcloud.

First, you’ll need to set up a directory with two files: a package.json and index.js. The package.json file will be simple, with only a name attribute and a version attribute. Your main.js will have the same simple “Hello world!” code. They will look something like this:

Package.json:

{
  "name": "my-first-function",
  "version": "0.0.1"
}

index.js:

exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

Now that you have your two files, let’s deploy using the gcloud tool, like this:

> gcloud functions deploy my-first-function --trigger-http --runtime nodejs8 --entry-point=helloWorld

This should deploy the function and be used in the same way as the first one you did.

The gcloud tool will pick defaults, as you did in the UI editor. But there are a few command line arguments to explain.

  • The command line argument –trigger-http specifies what triggers the function. In your case, you’re using http.
  • You’ll need to pick a –runtime, which in this case should be the Node.js 8 environment again.
  • The last one, –entry-point, specifies the name of the export function to use.

There’s nothing too magical here, but you need to use them all.

Passing Data to Your Function

Now that you have a pipeline for your function, let’s figure out how you’d actually pass some data to it.

To understand what this function can do, let’s first peel back a layer and understand the environment this function is executing in.

When you use the Node.js environment for your cloud function (and there are a few others), you’re letting Google set you up in an environment with Node.js as the engine and express as the web server. This way you can concentrate on the what the function does.

So, all you’re doing is writing express.js functions that take the normal request and response object parameters. You can see those in your example. The first line in your example lets you use the query parameter message to change the message that comes back. Let’s try this out by executing out function and adding a message. To run the function with a parameter, take a URL that looks something like this and run it inside the browser:

http://<location>/my-first-function?message=BAM

If everything works out, you should see the message “BAM” as text in the browser. You could also construct a POST request where the body is the text you wanted to be displayed. Whatever an express.js function can do, these functions can do as well.

Where to Go Next

Now that you have a fairly simple pipeline to deploy our functions, what’s next?

This function can do any type of work that you’d normally do in an HTTP service. But functions aren’t just limited to HTTP calls. They can be triggered in a number of ways. They can be triggered from events inside the Google cloud environment or even from pub/sub messages. You can do a lot with functions.

Google Cloud Functions can also connect to other resources. For example, it can connect to Google Cloud SQL so you can store data inside a world-class SQL database.

Remember, your cloud function is stateless. Therefore, any data you store inside it will get blown away regularly. You have to store your data outside of the function itself.

Google Cloud Functions can be a great part of your architecture. They allow a different way of thinking about how to use cloud resources and how to take advantage of the newer paradigms, like the DevOps cultures, that are currently out there.

Conclusion

Google Cloud functions are great for when you want to deploy some code in the cloud without having to mess with a virtual machine in the cloud. Functions let you do what you’re good at: writing code that solves problems.

Hopefully this tutorial showed you a way to get your functions in the cloud and start to use this great feature from Google Cloud!

This post was written by Erik Lindblom. Erik has been a full stack developer for the last 13 years. During that time, he’s tried to understand everything that’s required to deliver high quality, valuable software. Today, that means using cloud services, microservices techniques, container technologies. Tomorrow? Well, he’s ready to find out.

Comments are closed.

THE TOP LIST OF BLOG

SUBSCRIBE TO OUR BLOG

Jump in with your own data. Free for 30 days.

Free Trial