Introduction: Serverless and Cloud Functions

Welcome to the first lesson of the "Building Serverless Applications" course. In this lesson, we will explore the basics of serverless computing and how Cloud Functions help you build applications without managing servers.

Serverless computing means you do not have to worry about setting up or maintaining servers. Instead, you write your code, and the cloud provider (like Google Cloud) runs it for you only when needed. This makes it easier to scale and can save money, since you only pay for what you use.

Cloud Functions is a popular serverless service. With Cloud Functions, you write small pieces of code called functions. These functions run in response to events, such as a user making a request to your application.

In this lesson, you will learn how to connect Cloud Functions to an API Gateway so you can build an API that responds to user requests. By the end, you will understand how a simple API can trigger a Cloud Function, process some data, and return a result.

Recall: APIs and API Gateway

Before we dive in, let's quickly remind ourselves what an API (Application Programming Interface) is and how API Gateway fits in.

An API (Application Programming Interface) is a way for different programs to talk to each other. For example, when you use a weather app, it might call an API to get the latest weather data.

API Gateway is a service from Google Cloud that helps you create and manage APIs. It acts as a "front door" for your application. When someone sends a request to your API, API Gateway receives it and can pass it on to a Cloud Function to process.

In this lesson, we will see how API Gateway and Cloud Functions work together to handle a simple calculation request.

Exploring the Cloud Function Code

Let's build up the Cloud Function step by step. This function will receive a request with an amount, calculate tax, and return the total. By default, Cloud Functions (Python) look for main.py and the specified entry-point.

Step 1: Importing Required Libraries

First, we need to import the json library. This helps us work with JSON data, which is a common format for sending information in APIs.

Step 2: Defining the Cloud Function

Every Cloud Function needs a main function. This is the function that Google Cloud calls when the function is triggered.

  • request contains information about the HTTP request, such as the data sent by the user and the request method.
Step 3: Reading the Input Data

We want to get the amount from the request. The data is sent as JSON in the body of the request.

  • request.get_json(silent=True) parses the request body as JSON. With silent=True, it returns None instead of raising an error if the body is not valid JSON.
  • We default to an empty dictionary when there is no body.
  • We convert the amount to a float to ensure it's numeric. If this conversion fails (for example, if amount is "abc"), an exception will be raised and handled by our error handling below.
Step 4: Calculating Tax and Total

Now, let's calculate the tax and the total amount.

  • We multiply the amount by 0.07 to get a 7% tax.
  • round(..., 2) makes sure the result has two decimal places, like money.
Step 5: Preparing the Response

Next, we create a response dictionary with our calculated values.

Step 6: Returning the Response (and Handling Errors)

Finally, we need to return the result as a JSON response. If anything goes wrong (for example, invalid input), we return a 400 error with details.

  • json.dumps(...) converts our Python dictionary to a JSON string.
  • 200 is the HTTP status code meaning the request was successful.
  • If an exception occurs, we return a 400 Bad Request with an error message so the client knows what went wrong.
  • {"Content-Type": "application/json"} tells the client that the response is in JSON format.
Complete Code Example

Here's the full Cloud Function code in main.py:

Full Example Output

If you send a request with an amount of 100, the output will look like this:

Putting It All Together

Let's see how everything works as a whole:

  1. A user sends a POST request to /calculate with a JSON body like {"amount": 100}.
  2. API Gateway receives the request and passes it to the Cloud Function.
  3. The Cloud Function reads the amount, calculates the tax and total, and returns the result.
  4. If the input is invalid (for example, a non-numeric amount), the Cloud Function returns a 400 error with a JSON error message.
  5. API Gateway sends the response back to the user.

Real-World Example

Imagine you are building a simple online store. When a customer enters the amount of their purchase, your API calculates the tax and total for them instantly, without you having to manage any servers.

Summary And What's Next

In this lesson, you learned about serverless computing and how Cloud Functions work with API Gateway to create simple APIs. We explored a Cloud Function that calculates tax, walked through the code step by step, and saw how it processes input, handles errors, and returns a response.

Next, you will get hands-on practice by working with Cloud Functions and API Gateway in the CodeSignal environment. You will write your own code, test it, and see how serverless applications work in real time. Let's get started!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal