Create a Cloud Function triggered via HTTP

Introduction

This project focuses on deploying an HTTP-triggered Cloud Function on Google Cloud. Cloud Functions is a serverless execution environment where you can run code without provisioning or managing servers.

Why Is It Useful?

  • Scalability: It automatically scales to handle incoming HTTP requests.
  • Cost-Effective: You only pay for the time your code runs. Under the free tier, low-volume use won’t incur charges.
  • Simplicity: You focus on your code; Google Cloud manages the infrastructure.

Prerequisites

Before you begin, ensure you have:

A Google Cloud Account (Free Tier, No Credits Required)Sign up at cloud.google.com. Choose a region that supports the Cloud Functions free usage.

Google Cloud SDK Installed (gcloud CLI)Download and install from Google Cloud SDK Install.

Proper Permissions You need Owner or Editor IAM role on your project to create and deploy Cloud Functions.

Cloud Functions API Enabled Confirm Cloud Functions API is active in your project. You can do this via the APIs & Services page or the CLI command below.

Step 1: Select or Create a Project

  • Console : Go to Google Cloud Console.
    In the top-left dropdown, choose an existing project or create a new one (e.g., “my-cloud-functions-project”).
  • CLI : gcloud config set project YOUR_PROJECT_ID

Step 2: Enable Cloud Functions API

  • Console : In the Console, navigate to APIs & Services > Library.
    Search for “Cloud Functions API”, select it, and click Enable.
  • CLI : gcloud services enable cloudfunctions.googleapis.com

Step 3: Create a Simple Function (Code + Deployment)

We’ll use a minimal example function in Node.js (although you can choose other runtimes).

A. Write/Review Your Function Code

Let’s call our function HelloHTTP. It returns a simple JSON response:

/**
* HTTP Cloud Function.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.helloHttp = (req, res) => {
res.status(200).send({ message: "Hello from Cloud Functions!" });
};

Save this as index.js. If your runtime is Node.js 18, also create a package.json if needed:

{
"name": "hello-http-function",
"version": "1.0.0",
"main": "index.js"
}

Deploy the Cloud Function

Console : Go to Cloud Functions (under “Serverless” or search “Cloud Functions” in the top search bar). Click Create Function. Function Name: helloHttp Region: Choose a region that qualifies for free tier usage (e.g., us-central1). Trigger type: Select HTTP. Runtime: Select Node.js 18 (or whichever you prefer). Entry point: helloHttp (match the exported function name). In the Source code section, you can choose “Inline editor” and paste the code from above (index.js and optional package.json) or “ZIP upload/Cloud Source Repositories.” Check Allow unauthenticated invocations if you want it publicly accessible. Click Deploy.


CLI : Inside a folder containing index.js (and package.json if used), run: gcloud functions deploy helloHttp \ --region=us-central1 \ --runtime=nodejs18 \ --trigger-http \ --allow-unauthenticated \ --entry-point=helloHttp

5. Verifying and Testing the Project

Check Deployment Status

  • In the Console, under Cloud Functions, verify the status is “Active” or “Deployed.”
  • Make sure helloHttp shows “ACTIVE” in the status column.Using the CLI, you can run : gcloud functions list

Invoke the Function

  • From the Console, click on the function name and look for the Trigger URL.
  • Paste the URL into a web browser or use a command like:
    bashCopierModifiercurl https://<REGION>-<PROJECT_ID>.cloudfunctions.net/helloHttpYou should see the JSON response : { "message": "Hello from Cloud Functions!" }

Logs & Monitoring

  • Ensure there are no errors and that your invocations are logged.Go to the function’s Logs in the Console or run : gcloud functions logs read helloHttp

Conclusion

In this project, we have:

  • Created a Cloud Function on Google Cloud that responds to HTTP requests.

  • Learned Console steps (GUI) and CLI commands (gcloud) side by side.

  • Verified that the function is live and responding without incurring extra costs.

What is Cloud Computing ?

Cloud computing delivers computing resources (servers, storage, databases, networking, and software) over the internet, allowing businesses to scale and pay only for what they use, eliminating the need for physical infrastructure.


  • AWS: The most popular cloud platform, offering scalable compute, storage, AI/ML, and networking services.
  • Azure: A strong enterprise cloud with hybrid capabilities and deep Microsoft product integration.
  • Google Cloud (GCP): Known for data analytics, machine learning, and open-source support.