Deploying from Your Local Machine

This page shows you how to deploy a Cloud Function from your local machine using the gcloud command-line tool. When using the command-line tool, Google Cloud Functions packages and uploads the contents of your function's directory to a Cloud Storage bucket for you and automatically excludes unnecessary files via the .gcloudignore file.

Alternatively, you can use the Cloud Functions interface in the GCP Console to upload a ZIP file you make yourself.

For a complete runnable example that includes downloadable sample code, see the Using the gcloud Command-Line Tool quickstart.

Deploy using the gcloud tool

Using the gcloud command-line tool, deploy your function from the directory containing your function code with the gcloud functions deploy command:

gcloud functions deploy NAME --runtime RUNTIME TRIGGER [FLAGS...]
Argument Description
NAME The name of the Cloud Function you are deploying. This name can only contain letters, numbers, underscores, and hyphens. Unless you specify the --entry-point flag, your code must contain a function with the same name.
--runtime RUNTIME The name of the runtime you are using. For a complete list, see the gcloud reference. Note that you must include this flag the first time you deploy a function, but you can omit it on subsequent deployments.
TRIGGER The trigger type for this function (see Events and Triggers). The rules for specifying triggers when deploying a function are as follows:
  • If you don't specify a trigger when deploying an update to an existing function, it will keep its current trigger.
  • When deploying a new function, you must specify one of --trigger-http, --trigger-topic, or --trigger-bucket, or specify both --trigger-event AND --trigger-resource. You use the flag --trigger-http to deploy HTTP functions. You use the other flags to deploy background functions.
FLAGS... (Optional) Additional flags you can specify during deployment, such as --stage-bucket or --entry-point. For a full reference, see the gcloud functions deploy documentation.

Each runtime has specific file structuring requirements so Cloud Functions can find your function's definition.

HTTP function example

There are two types of Cloud Functions: HTTP functions and background functions. For an example that shows how to deploy a background function, see Background function example. The following example deploys an HTTP function written in Python and assigns it a trigger, by using the --trigger-http flag:

gcloud functions deploy helloworld --runtime python37 --trigger-http

See the arguments in this command:

Argument Description
helloworld The name of the Cloud Function you are deploying, in this case helloworld. The deployed Cloud Function will be registered under the name helloworld, and the source file must contain a function named helloworld.
--runtime python37 The runtime for this function, in this case python37.
--trigger-http The trigger type for this function, in this case an HTTP request (webhook).

The following example deploys the same function but under a different name:

gcloud functions deploy hello --entry-point helloworld --runtime python37 --trigger-http

See the arguments in this command:

Argument Description
hello The name of the Cloud Function you are deploying, in this case hello. The deployed Cloud Function will be registered under the name hello.
--entry-point helloworld The deployed Cloud Function will use a function named helloworld in the source file.
--runtime python37 The runtime for this function, in this case python37.
--trigger-http The trigger type for this function, in this case an HTTP request (webhook).

The --entry-point option is useful when the functions in your source code do not meet the naming requirements for Cloud Functions.

Background function example

The following example deploys a background function written in Node.js 8 that will be triggered by Cloud Storage:

gcloud functions deploy helloGCSGeneric --runtime nodejs8 --trigger-resource TRIGGER_BUCKET_NAME --trigger-event google.storage.object.finalize

See the arguments in this command:

Argument Description
helloGCSGeneric The name of the Cloud Function you are deploying, in this case helloGCSGeneric. The deployed Cloud Function will be registered under the name helloGCSGeneric, and the source file must contain a function named helloGCSGeneric.
--runtime nodejs8 The runtime for this function, in this case nodejs8.
--trigger-resource The trigger resource for this function. The trigger resource specifies the resource for which the trigger event is being observed. In this case, the resource is the name (TRIGGER_BUCKET_NAME) of the Cloud Storage bucket that triggers the function.
--trigger-event The trigger event for this function, which specifies which action should trigger the function. In this case, the event is google.storage.object.finalize.

Using the flags --trigger-resource MY_RESOURCE and --trigger-event MY_EVENT is the most explicit way to specify a trigger for a background function. However, gcloud provides shorthands for Cloud Pub/Sub and Cloud Storage:

  • When deploying functions that are assigned Cloud Pub/Sub triggers, you can use the flag --trigger-topic MY_TOPIC. This flag is a shorthand for --trigger-resource MY_TOPIC --trigger-event google.pubsub.topic.publish.

  • When deploying functions that are assigned Cloud Storage triggers, you can use the flag --trigger-bucket MY_STORAGE_BUCKET to trigger function execution whenever files in the specified bucket change.

Next steps

Esta página foi útil? Conte sua opinião sobre:

Enviar comentários sobre…

Cloud Functions Documentation