Introduction
There are several ways to deploy Cloud Functions, including:
- Using the GCP Console
- Using the gcloud CLI
- Using the Cloud Functions API
In this article, I will focus on deploying Cloud Functions using the gcloud command.
gcloud Command
You can use the gcloud functions deploy
command to deploy Cloud Functions.
$ gcloud functions deploy YOUR_FUNCTION_NAME \
[--gen2] \
--region=YOUR_REGION \
--runtime=YOUR_RUNTIME \
--source=YOUR_SOURCE_LOCATION \
--entry-point=YOUR_CODE_ENTRYPOINT
-
YOUR_FUNCTION_NAME
The name of the function to be deployed. Function names can use lowercase letters, numbers, hyphens, and underscores. -
--gen2
Specify to deploy to Cloud Functions (2nd Generation). If this flag is omitted, it will be deployed to Cloud Functions (1st Generation). -
--region
The region where the function will be deployed. -
--runtime
The language runtime used by the function. -
--source
The location of the function's source code. -
--entry-point
The entry point (function name) in the source code.
Command Examples
Here are some examples of how to use the command.
Deploy HTTP function from local source code
Assume that you have an HTTP function:
- It uses Node.js 16.
- The source code is in the current working directory (
.
). - The code's entry point is
myHttpFunction
.
To deploy the function to Cloud Functions (2nd Generation) with the name my-http-function
in the us-central1
region and trigger it with an HTTP trigger, use the following command:
$ gcloud functions deploy my-http-function \
--gen2 \
--region=us-central1 \
--runtime=nodejs16 \
--source=. \
--entry-point=myHttpFunction \
--trigger-http
Deploy Pub/Sub function from source code in Cloud Storage
Assume that you have an event-driven function:
- It handles Pub/Sub message publish events.
- It uses Python 3.9.
- The source code is in the Cloud Storage path
gs://my-bucket/my_function_source.zip
. - The code's entry point is
pubsub_handler
.
To deploy the function to Cloud Functions (2nd Generation) with the name my-pubsub-function
in the europe-west1
region and trigger it with a Pub/Sub topic my-topic
, use the following command:
$ gcloud functions deploy my-pubsub-function \
--gen2 \
--region=europe-west1 \
--runtime=python39 \
--source=gs://my-bucket/my_function_source.zip \
--entry-point=pubsub_handler \
--trigger-topic=my-topic
Deploy Cloud Storage function from local source code
Assume that you have an event-driven function:
- It handles Cloud Storage object deletion events.
- It uses Java 11.
- The source code is at the local path
./functions/storage-function
. - The code's entry point is
myproject.StorageFunction
.
To deploy the function to Cloud Functions (2nd Generation) with the name my-storage-function
in the asia-northeast1
region and trigger it with events in the my-bucket
Cloud Storage bucket, use the following command:
$ gcloud functions deploy my-storage-function \
--gen2 \
--region=asia-northeast1 \
--runtime=java11 \
--source=./functions/storage-function \
--entry-point=myproject.StorageFunction \
--trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
--trigger-event-filters="bucket=my-bucket"
References