Step by step guide to create Google Cloud Functions

Tutorial 3 of 5

1. Introduction

In this tutorial, we aim to guide you through the process of creating, deploying, and triggering Google Cloud Functions. Google Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions, you can write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services.

By the end of this tutorial, you will have learned how to write a Google Cloud Function, how to deploy it, and how to trigger it.

Prerequisites:
- Basic knowledge of JavaScript (Node.js)
- Google Cloud account
- Google Cloud SDK installed on your machine

2. Step-by-Step Guide

Step 1: Write the function

Before we start, make sure you have Node.js and npm (Node Package Manager) installed on your machine. We will write a simple function that takes a name as input and returns a greeting message.

exports.helloWorld = (req, res) => {

  // Get the name from the request
  let name = req.query.name || 'World';

  // Send the greeting
  res.send(`Hello ${name}!`);
};

In this code snippet, exports.helloWorld is our cloud function which takes two parameters, a request (req) and a response (res). It fetches a name from the query parameters of the request and sends a greeting message in response.

Step 2: Deploy the function

To deploy the function, you can use the gcloud command-line tool. Navigate to the directory containing your function and run the following command:

gcloud functions deploy helloWorld --runtime nodejs10 --trigger-http --allow-unauthenticated

This command deploys your function with the name helloWorld, sets the runtime environment as Node.js 10, triggers the function over HTTP, and allows unauthenticated requests for testing purposes.

Step 3: Trigger the function

After successful deployment, you can trigger the function using its URL. The URL usually has the following format:

https://REGION-PROJECT_ID.cloudfunctions.net/helloWorld

3. Code Examples

Let's take another example where we create and deploy a function that multiplies two numbers.

// Function to multiply two numbers
exports.multiply = (req, res) => {

  // Get the numbers from the request
  let num1 = Number(req.query.num1);
  let num2 = Number(req.query.num2);

  // Send the result
  res.send(`The product is ${num1 * num2}`);
};

Deploy the function using the gcloud command, exactly as we did in the previous example:

gcloud functions deploy multiply --runtime nodejs10 --trigger-http --allow-unauthenticated

The function can be triggered using the URL:

https://REGION-PROJECT_ID.cloudfunctions.net/multiply?num1=6&num2=7

The output will be The product is 42.

4. Summary

In this tutorial, we have covered how to write, deploy, and trigger Google Cloud Functions. You've learned how to create simple functions and deploy them to Google Cloud.

Next Steps:
- Explore more complex functions and deployment options
- Learn how to secure your functions
- Learn how to monitor and debug your functions

Additional Resources:
- Google Cloud Functions Documentation
- Node.js Documentation

5. Practice Exercises

  1. Create a function that takes two numbers and returns their sum.
  2. Create a function that takes a string and returns it in reverse order.
  3. Create a function that takes an array of numbers as input and returns the array sorted in ascending order.

Solutions can be found in the Google Cloud Functions documentation under the 'Writing and deploying functions' section. For further practice, try modifying these functions and observing the results.