Deploy Cloud Functions from the CLI
Intermediate10 min
Create and deploy serverless functions on Google Cloud Functions with HTTP and event-based triggers.
Prerequisites
- -gcloud CLI installed
- -GCP project with billing enabled
- -Cloud Functions API enabled
Steps
1
Enable the Cloud Functions API
Enables Cloud Functions and Cloud Build APIs.
$ gcloud services enable cloudfunctions.googleapis.com cloudbuild.googleapis.com
2
Create a function source file
Creates a minimal HTTP-triggered function.
$ mkdir my-function && echo 'exports.helloWorld = (req, res) => { res.send("Hello from Cloud Functions!"); };' > my-function/index.js
3
Deploy the function
Deploys a 2nd-gen Cloud Function with an HTTP trigger accessible without authentication.
$ gcloud functions deploy helloWorld --runtime nodejs20 --trigger-http --allow-unauthenticated --source ./my-function --region us-central1 --gen2
Always use --gen2 for new functions. Gen 2 runs on Cloud Run infrastructure with better performance.
4
Test the function
Invokes the function and prints the response.
$ gcloud functions call helloWorld --region us-central1 --gen2
5
View function logs
Displays the most recent 20 log entries for the function.
$ gcloud functions logs read helloWorld --region us-central1 --gen2 --limit 20
Full Script
FAQ
Discussion
Loading comments...