Deploy GCP Cloud Function with Http Trigger

2023/04/062 min read
bookmark this
Responsive image

Create Index.js Entry Point file

Create Index.js file andd add below code, it'll 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 @google-cloud/functions-framework for GCP Cloud Function

Since we'll use the package for GCP HTTP Cloud Function, so we need to add this dependency package at package.json file.

"dependencies": {
    "@google-cloud/functions-framework": "^3.3.0"
}

Run at local

Add this if wish to test the GCP Cloud Function at the local environment, by default will be using the port 8080, type http://8080 so you can the the cloud function locally.

"scripts": {
    "start": "npx functions-framework --target=myHttpFunctionName [--signature-type=http]",
}

Deploy to GCP Cloud

After test the cloud function at local, when ready you can run below command to deploy the code to the GCP Cloud Function.

gcloud functions deploy myHttpCloudFunctionName --gen2 --region=us-central1 --runtime=nodejs18 --entry-point=myHttpFunctionName --trigger-http --allow-unauthenticated

Test the HTTP end point

If the deploy run well, you should see the new HTTP Cloud Functions as below, and if you copy the URL to run at browser, should be see the ok at your page. Test Cloud Function Http End Point