0
0
GcpHow-ToBeginner · 3 min read

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 like nodejs18 or python39.
  • --trigger-http or --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 --runtime causes deployment failure.
  • Not setting the correct --trigger type leads to unexpected behavior.
  • Incorrect --entry-point if your function name in code differs.
  • Deploying from the wrong --source folder 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

OptionDescriptionExample
--runtimeSets the language runtime--runtime nodejs18
--trigger-httpTriggers function via HTTP request--trigger-http
--trigger-topicTriggers function on Pub/Sub topic--trigger-topic my-topic
--entry-pointFunction name in source code--entry-point helloWorld
--sourcePath 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.