Introduction

Welcome back! In the previous lesson, you learned how serverless computing works on Google Cloud Platform (GCP) using Cloud Functions and API Gateway. Now, you are ready to take the next step: defining, testing, and deploying your serverless applications using GCP's tools and configuration formats.

On GCP, you can manage your serverless resources by describing your application in configuration files, such as OpenAPI specifications. This approach allows you to define your API endpoints and connect them to your Cloud Functions, making it easier to build, test, and deploy serverless APIs.

In this lesson, you will learn how to:

  • Define your Cloud Function and API endpoint using GCP's configuration files
  • Test your function locally with sample data
  • Deploy your application to GCP and verify that it works

By the end of this lesson, you will be able to take a function and turn it into a working API using GCP's serverless tools.

Quick Recap: Cloud Functions and API Gateway

Before we dive in, let's quickly remind ourselves how Cloud Functions and API Gateway work together on GCP.

  • Cloud Function: This is your code that runs in the cloud. It receives an HTTP request, processes the input, and returns a response.
  • API Gateway: This is the entry point for your users. It lets you define HTTP endpoints (like POST or GET) and routes requests to your Cloud Function.

In the last lesson, you saw a basic Cloud Function handler:

The request parameter contains the data sent to your function (such as a JSON payload from an API request). You can read the request body, headers, and other information from this object.

With this in mind, let's see how to define and deploy this kind of function using GCP's tools.

Defining Your Application with OpenAPI and GCP Configuration

On GCP, you define your API endpoints and connect them to Cloud Functions using an OpenAPI specification file. This file describes your API in a way that GCP's API Gateway understands.

Let's build up a simple OpenAPI configuration step by step.

1. Define the Cloud Function

First, you need to create your Cloud Function code. Create a file called main.py with your function implementation:

This file structure is what GCP expects when you deploy your Cloud Function.

2. Connect API Gateway to Cloud Function

To make your function accessible through a custom API endpoint, you use an OpenAPI specification. Here's a minimal example (openapi.yaml):

  • The /calculate path defines a POST endpoint.
  • The x-google-backend field connects the endpoint to your deployed Cloud Function. Replace REGION and PROJECT_ID with your actual values.
Testing Locally with Sample Event Data

Before deploying your function, it's a good idea to test it locally. This helps you catch errors early.

1. Create a Sample Event

You can use a tool like curl or a local HTTP client to simulate an API request. For example, create a JSON file called event.json:

This file represents the data your API will receive.

2. Write the Cloud Function Handler

Here's a simple handler that reads the amount, calculates tax, and returns the result:

  • request.get_json(silent=True) reads the JSON body from the HTTP request.
  • The function calculates the tax and total, then returns a JSON response.
3. Test the Function

You can test your function locally using the Functions Framework. First, install it:

Then, run your function locally:

Now, in another terminal, send a test request:

Expected Output:

This shows that your function is working as expected before you deploy it.

Deploying Your Application: Deploy, Connect, and Verify

Once your function works locally, you are ready to deploy it to GCP and connect it to API Gateway.

1. Deploy the Function

Deploy your function using the following command:

This command uploads your code and makes it available as an HTTP endpoint.

  • calculate_tax is the name of your Cloud Function on GCP.
  • --entry-point specifies the Python function to run in your code.
  • --runtime sets the runtime environment.
  • --trigger-http makes the function accessible via HTTP.
  • --memory and --timeout control resources.
2. Deploy the API Gateway

Create an API config from your OpenAPI file:

Then, create the API Gateway:

Replace PROJECT_ID and REGION with your actual values.

3. Find Your API Endpoint

After deployment, you can find your API Gateway URL with:

Look for the defaultHostname field in the output. Your endpoint will look like:

4. Test the Deployed API

You can test your API using curl:

Expected Output:

This confirms your function is live and working in the cloud.

Summary and What's Next

In this lesson, you learned how to define, test, and deploy a serverless API on GCP. You saw how to:

  • Describe your Cloud Function and API endpoint using an OpenAPI specification
  • Test your function locally with sample event data
  • Deploy your application to GCP and verify that it works

You are now ready to practice these steps yourself. In the next exercises, you will get hands-on experience defining, testing, and deploying your own serverless APIs using GCP's serverless tools. Good luck, and have fun building!

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