Deploy a function

This guide shows you how to deploy a function from source code using the gcloud functions command. To learn how to deploy a function using the gcloud run command, see Deploy a Cloud Run function using the gcloud CLI.

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

Deployment basics

For an introduction on the type of functions you can deploy, see Write Cloud Run functions.

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

  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 \
       --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 name of the Cloud Run service that is created for your function will replace underscores with hyphens and uppercase letters will be converted to lowercase. For example, Function_1 will be given the name function-1 in Cloud Run.

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

    • The --runtime flag specifies which language runtime your function uses. See Runtime support for a list of supported runtime IDs.

    • The --source flag specifies the location of your function source code.

    • 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. For more information, see Function entry point.

    • 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.
      --trigger-topic=YOUR_PUBSUB_TOPIC Trigger the function when a message is published to the specified Pub/Sub topic.
      --trigger-bucket=YOUR_STORAGE_BUCKET Trigger the function when an object is created or overwritten in the specified Cloud Storage bucket.
      --trigger-event-filters=EVENTARC_EVENT_FILTERS Trigger the function with Eventarc when an event that matches the specified filters occurs.

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

      For more details about gcloud functions deploy configuration flags, refer to Cloud Run documentation.

When deployment finishes successfully, functions appear with a green check mark in the Cloud Run overview page in the Google 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.

HTTP endpoint URL

When you create a function with the gcloud functions command or the Cloud Functions v2 API, by default, the function has a cloudfunctions.net HTTP endpoint URL. If you take this function and deploy it on Cloud Run, your function also receives a run.app HTTP endpoint URL. However, functions created in Cloud Run won't have an cloudfunctions.net HTTP endpoint URL. A function's cloudfunctions.net URL and run.app URL behave in exactly the same way. They are interchangeable, and are used to trigger your function.

Terraform examples

For examples about how to deploy functions using Terraform, see the Terraform HTTP example and Terraform Pub/Sub example.

Learn how to deploy Cloud Run functions on Cloud Run

Deploying functions on Cloud Run is similar to the steps described in this document, but with some added advantages: