Getting started with Google Cloud Functions

Tutorial 1 of 5

1. Introduction

This tutorial aims to provide a step-by-step guide on how to get started with Google Cloud Functions. By the end of this tutorial, you will be able to create, deploy, and trigger your first Google Cloud Function.

What you'll learn:

  • What are Google Cloud Functions
  • How to create your first Google Cloud Function
  • How to deploy and trigger your Google Cloud Function

Prerequisites:

  • Basic knowledge of JavaScript (Node.js)
  • A Google Cloud Platform account

2. Step-by-Step Guide

Google Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services.

Step 1: Setting up Google Cloud SDK

Before we start, make sure you have the Google Cloud SDK (Software Development Kit) installed on your machine. This will allow you to interact with Google Cloud using your terminal. You can download it here.

Step 2: Create a new Google Cloud Function

Create a new directory on your local machine and navigate to it using your terminal. Create two files: index.js and package.json.

Step 3: Write your Function

Open index.js and write the following function:

exports.helloWorld = (req, res) => {
  res.send('Hello, World!');
};

Step 4: Define Function Dependencies

Open package.json and define your function's dependencies:

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "@google-cloud/functions-framework": "^1.7.1"
  }
}

Step 5: Deploy your Function

Deploy the function to Google Cloud:

gcloud functions deploy helloWorld \
--runtime nodejs14 \
--trigger-http \
--allow-unauthenticated

Step 6: Trigger your Function

Once deployed, you can trigger your function using its URL:

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

Replace REGION and PROJECT_ID with your Google Cloud project's details.

3. Code Examples

Example 1: HTTP Triggered Function

This function will respond with 'Hello, World!' when triggered by an HTTP request.

exports.helloWorld = (req, res) => {
  // Send response
  res.send('Hello, World!');
};

Example 2: Background Function

This function will log 'Hello, World!' when triggered by a Pub/Sub event.

exports.helloPubSub = (event, context) => {
  const pubsubMessage = event.data;
  console.log('Hello, World!');
};

4. Summary

In this tutorial, you learnt what Google Cloud Functions are, and how to create, deploy, and trigger your first function. Next, you could explore other types of triggers, such as Cloud Storage and Firestore triggers.

Additional Resources:

5. Practice Exercises

Exercise 1:

Create an HTTP triggered function that responds with 'Hello, [your name]!'

Exercise 2:

Create a background function that logs 'Hello, World!' every time a new file is uploaded to a Google Cloud Storage bucket.

Tips for further practice:

  • Explore different types of triggers
  • Try integrating with other Google Cloud services