Deploy a Cloud Function

This guide shows you how to deploy a Cloud Function from source code.

The deployment process takes your source code and configuration settings and builds a runnable image that Cloud Functions manages automatically in order to handle requests to your function.

Deployment basics

Users deploying Cloud Functions must have the Cloud Functions Developer IAM role or a role that includes the same permissions. See also Additional configuration for deployment.

Select a tab for instructions to deploy a function using the gcloud CLI or the Google Cloud console.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Use the gcloud functions deploy command to deploy a function:

    gcloud functions deploy YOUR_FUNCTION_NAME \
    [--gen2] \
    --region=YOUR_REGION \
    --runtime=YOUR_RUNTIME \
    --source=YOUR_SOURCE_LOCATION \
    --entry-point=YOUR_CODE_ENTRYPOINT \
    TRIGGER_FLAGS
    

    The first argument, YOUR_FUNCTION_NAME, is a name for your deployed function. The function name must start with a letter followed by up to 62 letters, numbers, hyphens, or underscores, and must end with a letter or a number.

    • The --gen2 flag specifies that you want to deploy to Cloud Functions (2nd gen). This flag is needed for the initial deployment of a 2nd gen function. See Cloud Functions version comparison for a comparison of Cloud Functions (1st gen) and Cloud Functions (2nd gen).

    • The --region flag specifies the region in which to deploy your function. See Locations for a list of regions supported by Cloud Functions.

    • The --runtime flag specifies which language runtime your function uses. Cloud Functions supports several runtimes - see Runtimes for more information.

    • The --source flag specifies the location of your function source code. See the following sections for details:

    • The --entry-point flag specifies the entry point to your function in your source code. This is the code that will be executed when your function runs. The value of this flag must be a function name or fully-qualified class name that exists in your source code. See Function entry point for more information.

    • To specify the trigger for your function, additional flags (represented as TRIGGER_FLAGS above) are required, depending on the trigger you want to use:

    Trigger flags Trigger description
    --trigger-http Trigger the function with an HTTP(S) request. See HTTP triggers for more information.
    --trigger-topic=YOUR_PUBSUB_TOPIC Trigger the function when a message is published to the specified Pub/Sub topic. See Pub/Sub triggers for more information.
    --trigger-bucket=YOUR_STORAGE_BUCKET Trigger the function when an object is created or overwritten in the specified Cloud Storage bucket. See Cloud Storage triggers for more information.
    --trigger-event-filters=EVENTARC_EVENT_FILTERS (2nd gen only) Trigger the function with Eventarc when an event occurs matching the specified filters. Requires the --gen2 flag to be specified. See Eventarc triggers for more information and additional options.
    --trigger-event=EVENT_TYPE
    [--trigger-resource=RESOURCE]
    (1st gen only) Trigger the function when the specified event occurs. Specifying a resource is required for some event types. See Triggers supported in Cloud Functions (1st gen) for more information.

    You can optionally specify additional configuration, networking, and security options when you deploy a function.

    For a complete reference on the deployment command and its flags, see the gcloud functions deploy documentation.

    For some example deployment commands, see Command-line examples.

Console

  1. Go to the Cloud Functions overview page in the Google Cloud console:

    Go to Cloud Functions

    Make sure that the Google Cloud project to which you want to deploy Cloud Functions is selected.

  2. Click Create function.

  3. In the Environment field, select 1st gen or 2nd gen. See Cloud Functions version comparison for a comparison of Cloud Functions (1st gen) and Cloud Functions (2nd gen).

  4. In the Function name field, enter a name for your function. The function name must start with a letter followed by up to 62 letters, numbers, hyphens, or underscores, and must end with a letter or a number.

  5. In the Region field, select a region in which to deploy your function.

  6. In the Trigger section, follow the steps based on your selected environment:

    • 1st gen
      1. In the HTTPS Authentication section, select the appropriate option depending on whether you want to allow unauthenticated invocations of your function. By default, authentication is required. See Authenticating for invocation for more information.
      2. In the Trigger type field, select a trigger type. See Triggers supported in Cloud Functions (1st gen) for more details.
      3. Complete the additional fields for your selected trigger type.
      4. Click Save.
    • 2nd gen
      1. In the Trigger type field, select the trigger you want to use:
        • For an HTTPS trigger, select the appropriate option depending on whether you want to allow unauthenticated invocations of your function. Every 2nd gen function has an endpoint for HTTP(S) triggers. By default, authentication is required. See Authenticating for invocation for more information.
        • For an event-driven function, choose one of Cloud Pub/Sub, Cloud Storage, Cloud Firestore, or Other trigger. See Eventarc triggers for more information.
        • Once you've completed the required fields, click Save trigger.
  7. Optionally, expand the Runtime, build... section at the end of the page for additional configuration and security options.

  8. Click Next to move to the Code step.

  9. In the Runtime field, select the language runtime your function uses. Cloud Functions supports several runtimes - see Runtimes for more information.

  10. In the Entry point field, enter the entry point to your function in your source code. This is the code that will be executed when your function runs. The value of this flag must be a function name or fully-qualified class name that exists in your source code - see Function entry point for more information.

  11. In the Source code field, select the appropriate option for how you will supply the function source code. See the following sections for details:

  12. Click Deploy.

When deployment finishes successfully, functions appear with a green check mark in the Cloud Functions overview page in the Google Cloud console:

Deployed functions in the Cloud console

The initial deployment of a function may take several minutes, while the underlying infrastructure is provisioned. Redeploying an existing function is faster, and incoming traffic is automatically migrated to the newest version.

Deploy from your local machine

This section describes how to deploy a function from source code located on your local machine.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Follow the deployment instructions above using the gcloud functions deploy command.

    For the --source flag, specify a local file system path to the root directory of the function source code - see Source directory structure. The current working directory is used if this flag is omitted.

    You can also optionally use the --stage-bucket flag to specify a Cloud Storage bucket to upload your source code to as part of deployment.

    During upload of your source code, Cloud Functions excludes unnecessary files through the .gcloudignore file.

Console

  1. Follow the deployment instructions above using the Google Cloud console until you get to the Source code field.
  2. In the Source code field, select ZIP Upload.
  3. In the Destination bucket field, click Browse to select a Cloud Storage bucket to upload your source code to as part of deployment.
  4. In the ZIP file field, click Browse to select a ZIP file to upload from your local file system. Your function source files must be located at the root of the ZIP file - see Source directory structure.
  5. Click Deploy.

Deploy from Cloud Storage

This section describes how to deploy a function from source code located in a Cloud Storage bucket. The source code must be packaged as a ZIP file.

For Cloud Functions to read from a Cloud Storage bucket, ensure that the deploying agent has the storage.objects.get permission:

  • In Cloud Functions (1st gen), you must grant this permission to the account performing the deployment.
  • In Cloud Functions (2nd gen), the Cloud Functions service agent needs this permission. If the source bucket is in the same project as your function, it is granted automatically. If the bucket is in a different project, you must grant it to the service agent manually.

See Use IAM permissions in the Cloud Storage documentation for information about controlling access to buckets.

With this permission you can now deploy a function from Cloud Storage:

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Follow the deployment instructions above using the gcloud functions deploy command.

    For the --source flag, specify a Cloud Storage path, starting with gs://. The object at the path must be a ZIP file containing the function source code. Your function source files must be located at the root of the ZIP file - see Source directory structure.

Console

  1. Follow the deployment instructions above using the Google Cloud console until you get to the Source code field.
  2. In the Source code field, select ZIP from Cloud Storage.
  3. In the Cloud Storage location field, click Browse to select a ZIP file from Cloud Storage. Your function source files must be located at the root of the ZIP file - see Source directory structure.
  4. Click Deploy.

Deploy from a source repository

This section describes how to deploy a function from source code located in Cloud Source Repositories. Deploying from Cloud Source Repositories also lets you deploy code hosted in a GitHub or Bitbucket repository.

Before you can deploy code from Cloud Source Repositories, you must set up a repository and optionally connect it to your GitHub or Bitbucket repository.

For Cloud Functions to read from a source repository, the Cloud Functions service agent needs the Source Repository Reader (roles/source.reader) IAM role on the repository. If your source repository is in the same project as your function, this permission is included automatically. If the source repository is in a different project, you must manually grant the permission.

Then, you can deploy a function from the repository:

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Follow the deployment instructions above using the gcloud functions deploy command.

    For the --source flag, specify a source repository reference to the location of your function source code - see Source directory structure. The most basic reference for Cloud Source Repositories has the following format:

    https://source.developers.google.com/projects/PROJECT_ID/repos/REPOSITORY_NAME
    

    In the preceding path, PROJECT_ID is your Google Cloud project ID and REPOSITORY_NAME is the name of your source repository. With this path, source code from the root directory of the repository on the revision tagged master will be used.

    To deploy from a revision other than master, append one of the following to the path:

    • /revisions/REVISION_NAME
    • /moveable-aliases/MOVEABLE_ALIAS
    • /fixed-aliases/FIXED_ALIAS

    To deploy source code from a directory other than the repository root, specify a revision, moveable alias, or fixed alias and append /paths/PATH_TO_YOUR_SOURCE_DIRECTORY to the path.

Console

  1. Follow the deployment instructions above using the Google Cloud console until you get to the Source code field.
  2. In the Source code field, select Cloud Source repository.
  3. In the Repository field, enter the name of your source repository.
  4. In the Branch / tag field, select Branch or Tag depending on where you want to deploy from.
  5. Enter the branch or tag name where indicated.
  6. In the Directory with source code field, enter the path to the directory in your repository containing your function source code - see Source directory structure.
  7. Click Deploy.

Deploy from the Google Cloud console inline editor

This section describes how to write and deploy a function directly from the Google Cloud console using the provided inline editor.

  1. Follow the deployment instructions above using the Google Cloud console until you get to the Source code field.
  2. In the Source code field, select Inline Editor.
  3. Use the provided editor to create or edit source files as necessary - see Source directory structure.
    • The left pane lists your source files and lets you create, rename, and delete files.
    • The right pane is a text editor that lets you edit file contents.
  4. Click Deploy.

Command-line examples

This section shows deployment commands for some example deployment scenarios.

For details about different triggers supported by Cloud Functions, see Cloud Functions triggers.

HTTP function from local source code

Suppose you have an HTTP function as follows:

  • The function uses Node.js 16.
  • The source code is located in the current working directory (.).
  • The entry point in the code is named myHttpFunction.

To deploy the function to Cloud Functions (2nd gen) with the name my-http-function in the region us-central1, you use the following command:

gcloud functions deploy my-http-function \
  --gen2 \
  --region=us-central1 \
  --runtime=nodejs16 \
  --source=. \
  --entry-point=myHttpFunction \
  --trigger-http

Pub/Sub function from source code in Cloud Storage

Suppose you have an event-driven function as follows:

  • The function handles Pub/Sub message publish events.
  • The function uses Python 3.9.
  • The source code is located in Cloud Storage at the path. gs://my-bucket/my_function_source.zip.
  • The entry point in the code is named pubsub_handler.

To deploy the function to Cloud Functions (2nd gen) with the name my-pubsub-function in the region europe-west1, and have the function triggered by messages on the Pub/Sub topic my-topic, you 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

Cloud Storage function from local source code

Suppose you have an event-driven function as follows:

  • The function handles Cloud Storage object deletion events.
  • The function uses Java 11.
  • The source code is located locally at the path ./functions/storage-function.
  • The entry point in the code is named myproject.StorageFunction.

To deploy the function to Cloud Functions (2nd gen) with the name my-storage-function in the region asia-northeast1, and have the function triggered by events in the Cloud Storage bucket my-bucket, you 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"

Next steps