Deploy GCP Cloud Function with Http Trigger
Table of Contents
- Introduction
- Create Index.js Entry Point File
- Include Dependency Package for GCP Cloud Function
- Run at Local
- Deploy to GCP Cloud
- Test the HTTP Endpoint
- Conclusion
Introduction
This blog shows how to create a simple Node.js application and deploy it to GCP Cloud Functions as an HTTP trigger.
Create Index.js Entry Point File
Create an Index.js file and add the below code. It will get the dependency package from '@google-cloud/functions-framework', create the endpoint, and return the response as 'ok'.
const functions = require('@google-cloud/functions-framework');
functions.http('myHttpFunctionName', (req, res) => {
res.send('ok');
});
Include Dependency Package for GCP Cloud Function
Since we'll use the package for GCP HTTP Cloud Functions, we need to add this dependency package in the package.json file.
"dependencies": {
"@google-cloud/functions-framework": "^3.3.0"
}
Run at Local
Add this if you wish to test the GCP Cloud Function in the local environment. By default it will use port 8080. Type http://localhost:8080 so you can test the cloud function locally.
"scripts": {
"start": "npx functions-framework --target=myHttpFunctionName [--signature-type=http]",
}
Deploy to GCP Cloud
After testing the cloud function locally, when you're ready you can run the below command to deploy the code to GCP Cloud Functions.
gcloud functions deploy myHttpCloudFunctionName --gen2 --region=us-central1 --runtime=nodejs18 --entry-point=myHttpFunctionName --trigger-http --allow-unauthenticated
Test the HTTP Endpoint
If the deployment runs successfully, you should see the new HTTP Cloud Function as shown below. If you copy the URL and run it in a browser, you should see ok on the page.

Conclusion
Deploying a GCP Cloud Function with an HTTP trigger is straightforward. By setting up the entry point, adding the required dependency, and running the deploy command, you can quickly have a serverless HTTP endpoint running in the cloud.