How to Deploy a Cloud Function Using CLI on GCP
Use the
gcloud functions deploy command to deploy your cloud function from the command line. Specify the function name, runtime, trigger type, and source code location to complete the deployment.Syntax
The basic syntax to deploy a Google Cloud Function using the CLI is:
gcloud functions deploy FUNCTION_NAME: Names your function.--runtime RUNTIME: Sets the language runtime likenodejs18orpython39.--trigger-httpor--trigger-topic TOPIC: Defines how the function is triggered.--entry-point ENTRY_POINT: Specifies the function in your code to run.--source SOURCE_PATH: Points to your function's source code folder.
bash
gcloud functions deploy FUNCTION_NAME --runtime RUNTIME --trigger-http --entry-point ENTRY_POINT --source SOURCE_PATH
Example
This example deploys a simple HTTP-triggered Node.js function named helloWorld from the current directory.
bash
gcloud functions deploy helloWorld --runtime nodejs18 --trigger-http --entry-point helloWorld --source .
Output
Deploying function (may take a while)...done.
availableMemoryMb: 256
entryPoint: helloWorld
httpsTrigger:
url: https://REGION-PROJECT_ID.cloudfunctions.net/helloWorld
runtime: nodejs18
status: ACTIVE
Common Pitfalls
- Forgetting to specify the
--runtimecauses deployment failure. - Not setting the correct
--triggertype leads to unexpected behavior. - Incorrect
--entry-pointif your function name in code differs. - Deploying from the wrong
--sourcefolder misses your code changes. - Not having the Google Cloud SDK installed or authenticated before running commands.
bash
Wrong: gcloud functions deploy myFunc --trigger-http Right: gcloud functions deploy myFunc --runtime python39 --trigger-http --entry-point myFunc --source ./myfunc
Quick Reference
| Option | Description | Example |
|---|---|---|
| --runtime | Sets the language runtime | --runtime nodejs18 |
| --trigger-http | Triggers function via HTTP request | --trigger-http |
| --trigger-topic | Triggers function on Pub/Sub topic | --trigger-topic my-topic |
| --entry-point | Function name in source code | --entry-point helloWorld |
| --source | Path to source code folder | --source ./src |
Key Takeaways
Always specify the runtime with --runtime when deploying a cloud function.
Choose the correct trigger type like --trigger-http or --trigger-topic based on your use case.
Set the entry point to match your function's name in the source code.
Deploy from the correct source folder to include your latest code.
Make sure Google Cloud SDK is installed and you are authenticated before deploying.