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.
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.
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.
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.
Every Cloud Function needs a main function. This is the function that Google Cloud calls when the function is triggered.
requestcontains information about the HTTP request, such as the data sent by the user and the request method.
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. Withsilent=True, it returnsNoneinstead 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
floatto 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.
Now, let's calculate the tax and the total amount.
- We multiply the amount by
0.07to get a 7% tax. round(..., 2)makes sure the result has two decimal places, like money.
Next, we create a response dictionary with our calculated values.
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.200is the HTTP status code meaning the request was successful.- If an exception occurs, we return a
400Bad 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.
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:
Let's see how everything works as a whole:
- A user sends a POST request to
/calculatewith a JSON body like{"amount": 100}. - API Gateway receives the request and passes it to the Cloud Function.
- The Cloud Function reads the amount, calculates the tax and total, and returns the result.
- If the input is invalid (for example, a non-numeric amount), the Cloud Function returns a
400error with a JSON error message. - 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.
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!
