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.
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.
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.
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.
To make your function accessible through a custom API endpoint, you use an OpenAPI specification. Here's a minimal example (openapi.yaml):
- The
/calculatepath defines a POST endpoint. - The
x-google-backendfield connects the endpoint to your deployed Cloud Function. ReplaceREGIONandPROJECT_IDwith your actual values.
Before deploying your function, it's a good idea to test it locally. This helps you catch errors early.
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.
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.
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.
Once your function works locally, you are ready to deploy it to GCP and connect it to API Gateway.
Deploy your function using the following command:
This command uploads your code and makes it available as an HTTP endpoint.
calculate_taxis the name of your Cloud Function on GCP.--entry-pointspecifies the Python function to run in your code.--runtimesets the runtime environment.--trigger-httpmakes the function accessible via HTTP.--memoryand--timeoutcontrol resources.
Create an API config from your OpenAPI file:
Then, create the API Gateway:
Replace PROJECT_ID and REGION with your actual values.
After deployment, you can find your API Gateway URL with:
Look for the defaultHostname field in the output. Your endpoint will look like:
You can test your API using curl:
Expected Output:
This confirms your function is live and working in the cloud.
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!
