Building Serverless Architecture with Cloud Functions

Tutorial 4 of 5

Building Serverless Architecture with Cloud Functions

1. Introduction

In this tutorial, we will learn how to build a serverless architecture using Cloud Functions. This involves writing, deploying, and managing Cloud Functions, allowing you to focus on your application logic and leaving the infrastructure management to the cloud provider.

By the end of this guide, you will understand:

  • What cloud functions are and how they work
  • How to write and deploy cloud functions
  • Best practices for managing and using cloud functions

Prerequisites:
- Basic understanding of cloud computing
- Basic knowledge of JavaScript (Node.js) or Python
- An account on a cloud platform offering Cloud Functions such as Google Cloud, AWS, or Azure

2. Step-by-Step Guide

Let's start with the basics. Cloud Functions are a part of the serverless offerings from various cloud providers. They let you run your application backend without managing any servers.

Writing a Cloud Function

First, we need to write our cloud function. Here's a basic example of a cloud function written in Node.js.

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

This is a simple HTTP function that responds to HTTP requests with the message 'Hello, World!'. req is the HTTP request that was received. This request object includes details about the request, including headers, parameters, and body. res is the HTTP response that will be sent.

Deploying a Cloud Function

Deploying this function will differ based on your cloud provider. For Google Cloud, you can use the following command:

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

This command tells Google Cloud to deploy a function named helloWorld, using the Node.js 10 runtime, and to trigger the function whenever an HTTP request is received. The --allow-unauthenticated flag allows any user to trigger the function.

3. Code Examples

Example 1: HTTP Function

Below is a more advanced HTTP function that accepts POST requests and returns a greeting to a specified name.

exports.helloUser = (req, res) => {
  if (req.method !== "POST") {
    res.status(405).send('Method Not Allowed');
    return;
  }

  const name = req.body.name || 'World';

  res.send(`Hello, ${name}!`);
};

In this code, we first check if the request method is POST. If it's not, we return a 405 status code with the message 'Method Not Allowed'. If it is a POST request, we extract the name from the request body. If no name is provided, we default to 'World'. Finally, we send a greeting to the specified name.

Example 2: Background Function

Background functions are triggered by events from your cloud infrastructure. Here's an example of a background function that's triggered whenever a new file is uploaded to Google Cloud Storage.

exports.fileUploaded = (file, context, callback) => {
  console.log(`New file uploaded. Name: ${file.name}`);
  callback();
};

In this function, file is the file that was uploaded and context provides information about the event that triggered the function. The callback function should be called when your function is done.

4. Summary

In this tutorial, we learned about Cloud Functions and how they can be used to build serverless applications. We covered how to write and deploy HTTP and background functions. The next steps could be exploring other types of Cloud Functions and learning how to manage and monitor these functions.

5. Practice Exercises

  1. Write an HTTP cloud function that accepts GET requests and returns the current date and time.
  2. Write a background function that is triggered whenever a file is deleted from Google Cloud Storage and logs the name of the deleted file.
  3. Write an HTTP function that accepts POST requests with a 'text' parameter and returns the number of words in the text.

Tip: For more practice, try modifying these functions to suit your needs, or explore other triggers for background functions.