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.
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.
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-05 (世界標準時間)。"],[[["\u003cp\u003eThis document outlines how to deploy a Cloud Run function (1st gen) from source code, which involves building a runnable image managed by Cloud Run functions.\u003c/p\u003e\n"],["\u003cp\u003eDeployment requires the Cloud Run functions Developer IAM role, and you can deploy functions using either the gcloud CLI or the Google Cloud console.\u003c/p\u003e\n"],["\u003cp\u003eWhen deploying with gcloud, you'll use the \u003ccode\u003egcloud functions deploy\u003c/code\u003e command, specifying details like function name, region, runtime, source location, entry point, and trigger flags.\u003c/p\u003e\n"],["\u003cp\u003eUsing the Google Cloud console, you can deploy functions by selecting the 1st gen environment, configuring the trigger type, runtime, entry point, and specifying the source code location, with options to upload from local, Cloud Storage, or use the inline editor.\u003c/p\u003e\n"],["\u003cp\u003eThe source code for your Cloud Run function can be located on your local machine, in a Cloud Storage bucket, or written directly in the inline editor of the Google Cloud Console, and it must be structured in a certain way to allow the function to be accessed.\u003c/p\u003e\n"]]],[],null,["# Deploy a Cloud Run function (1st gen)\n=====================================\n\nThis guide shows you how to deploy a legacy 1st gen Cloud Run function\nfrom source code. If you are creating a new function, see the\n[Console Quickstart](/run/docs/quickstarts/functions/deploy-functions-console)\non Cloud Run.\n\nThe deployment process takes your source code and configuration settings and\n[builds a runnable image](/functions/1stgendocs/building) that Cloud Run functions\nmanages automatically in order to handle requests to your function.\n\nDeployment basics\n-----------------\n\nUsers deploying Cloud Run functions must have the\n[Cloud Run functions Developer](/functions/docs/reference/iam/roles#cloudfunctions.developer)\nIAM role or a role that includes the same permissions. See also\n[Additional configuration for deployment](/functions/docs/reference/iam/roles#additional-configuration).\n\nDeploy a function using the gcloud CLI as follows:\n\n1. Use the [`gcloud functions deploy`](/sdk/gcloud/reference/functions/deploy)\n command to deploy a function:\n\n ```sh\n gcloud functions deploy YOUR_FUNCTION_NAME \\\n [--gen2] \\\n --region=YOUR_REGION \\\n --runtime=YOUR_RUNTIME \\\n --source=YOUR_SOURCE_LOCATION \\\n --entry-point=YOUR_CODE_ENTRYPOINT \\\n TRIGGER_FLAGS\n ```\n\n The first argument, \u003cvar translate=\"no\"\u003eYOUR_FUNCTION_NAME\u003c/var\u003e, is a name for\n your deployed function. The function name must start with a letter\n followed by up to 62 letters, numbers, hyphens, or underscores, and must end with a letter or a number.\n - The [`--gen2`](/sdk/gcloud/reference/functions/deploy#--gen2) flag\n specifies that you want to deploy to Cloud Run functions. As of September 2024,\n this becomes the default. To deploy to 1st gen, use [`--no-gen2`](/sdk/gcloud/reference/functions/deploy#--gen2).\n\n - The [`--region`](/sdk/gcloud/reference/functions/deploy#--region) flag\n specifies the region in which to deploy your function. See\n [Locations](/functions/1stgendocs/locations) for a list of regions supported by\n Cloud Run functions.\n\n - The [`--runtime`](/sdk/gcloud/reference/functions/deploy#--runtime) flag\n specifies which language runtime your function uses. Cloud Run functions\n supports several runtimes - see\n [Runtimes](/functions/docs/concepts/execution-environment#runtimes) for more information.\n\n - The [`--source`](/sdk/gcloud/reference/functions/deploy#--source) flag\n specifies the location of your function source code. See the following\n sections for details:\n\n - [Deploy from your local machine](#from-local-machine)\n - [Deploy from Cloud Storage](#from-cloud-storage)\n - The [`--entry-point`](/sdk/gcloud/reference/functions/deploy#--entry-point)\n flag specifies the entry point to your function in your source code. This is\n the code that will be executed when your function runs. The value of this\n flag must be a function name or fully-qualified class name that exists in\n your source code. See\n [Function entry point](/functions/1stgendocs/writing#entry-point) for\n more information.\n\n - To specify the [trigger](/functions/1stgendocs/calling) for your\n function, additional flags (represented as\n \u003cvar translate=\"no\"\u003eTRIGGER_FLAGS\u003c/var\u003e above) are required, depending on\n the trigger you want to use:\n\n You can optionally specify additional\n [configuration](/functions/1stgendocs/configuring),\n [networking](/functions/1stgendocs/networking/network-settings), and\n [security](/functions/1stgendocs/securing) options when you deploy a function.\n\n For a complete reference on the deployment command and its flags, see the\n [`gcloud functions deploy`](/sdk/gcloud/reference/functions/deploy)\n documentation.\n\n For some example deployment commands, see\n [Command-line examples](#cli-examples).\n\nDeploy from your local machine\n------------------------------\n\nThis section describes how to use gcloud CLI to deploy a function\nfrom source code located on your local machine.\n\n1. In the Google Cloud console, activate Cloud Shell.\n\n [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n\n\n At the bottom of the Google Cloud console, a\n [Cloud Shell](/shell/docs/how-cloud-shell-works)\n session starts and displays a command-line prompt. Cloud Shell is a shell environment\n with the Google Cloud CLI\n already installed and with values already set for\n your current project. It can take a few seconds for the session to initialize.\n2. Follow the [deployment instructions above](#basics) using the\n `gcloud functions deploy` command.\n\n For the `--source` flag, specify a local file system path to the root directory\n of the function source code - see\n [Source directory structure](/functions/1stgendocs/writing#directory-structure).\n The current working directory is used if this flag is omitted.\n\n You can also optionally use the\n [`--stage-bucket`](/sdk/gcloud/reference/functions/deploy#--stage-bucket) flag\n to specify a Cloud Storage bucket to upload your source code to as part of\n deployment.\n\n During upload of your source code, Cloud Run functions excludes unnecessary\n files through the [`.gcloudignore`](/sdk/gcloud/reference/topic/gcloudignore)\n file.\n\nDeploy from Cloud Storage\n-------------------------\n\nThis section describes how to use gcloud CLI to deploy a function\nfrom source code located in a Cloud Storage bucket. The source code must be\npackaged as a ZIP file.\n\nFor Cloud Run functions to read from a Cloud Storage bucket, you must grant the\n`storage.objects.get` permission to the account performing the deployment.\n\nSee [Use IAM permissions](/storage/docs/access-control/using-iam-permissions)\nin the Cloud Storage documentation for information about controlling access\nto buckets.\n\nWith this permission you can now deploy a function from Cloud Storage:\n\n1. In the Google Cloud console, activate Cloud Shell.\n\n [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n\n\n At the bottom of the Google Cloud console, a\n [Cloud Shell](/shell/docs/how-cloud-shell-works)\n session starts and displays a command-line prompt. Cloud Shell is a shell environment\n with the Google Cloud CLI\n already installed and with values already set for\n your current project. It can take a few seconds for the session to initialize.\n2. Follow the [deployment instructions above](#basics) using the\n `gcloud functions deploy` command.\n\n For the `--source` flag, specify a Cloud Storage path, starting with\n `gs://`. The object at the path must be a ZIP file containing the function\n source code. Your function source files must be located at the root of the\n ZIP file - see\n [Source directory structure](/functions/1stgendocs/writing#directory-structure).\n\nCommand-line examples\n---------------------\n\nThis section shows deployment commands for some example deployment scenarios.\n\nFor details about different triggers supported by Cloud Run functions, see\n[Cloud Run functions triggers](/functions/1stgendocs/calling).\n\n### HTTP function from local source code\n\nSuppose you have an HTTP function as follows:\n\n- The function uses Node.js 20.\n- The source code is located in the current working directory (`.`).\n- The entry point in the code is named `myHttpFunction`.\n\nTo deploy the function to Cloud Run functions with the name\n`my-http-function` in the region `us-central1`, you use the following command: \n\n gcloud functions deploy my-http-function \\\n --no-gen2 \\\n --region=us-central1 \\\n --runtime=nodejs20 \\\n --source=. \\\n --entry-point=myHttpFunction \\\n --trigger-http\n\n### Pub/Sub function from source code in Cloud Storage\n\nSuppose you have an event-driven function as follows:\n\n- The function handles Pub/Sub message publish events.\n- The function uses Python 3.12.\n- The source code is located in Cloud Storage at the path `gs://my-bucket/my_function_source.zip`.\n- The entry point in the code is named `pubsub_handler`.\n\nTo deploy the function to Cloud Run functions with the name\n`my-pubsub-function` in the region `europe-west1`, and have the function\ntriggered by messages on the Pub/Sub topic `my-topic`, you use the\nfollowing command: \n\n gcloud functions deploy my-pubsub-function \\\n --no-gen2 \\\n --region=europe-west1 \\\n --runtime=python312 \\\n --source=gs://my-bucket/my_function_source.zip \\\n --entry-point=pubsub_handler \\\n --trigger-topic=my-topic\n\n### Cloud Storage function from local source code\n\nSuppose you have an event-driven function as follows:\n\n- The function handles Cloud Storage object deletion events.\n- The function uses Java 17.\n- The source code is located locally at the path `./functions/storage-function`.\n- The entry point in the code is named `myproject.StorageFunction`.\n\nTo deploy the function to Cloud Run functions with the name\n`my-storage-function` in the region `asia-northeast1`, and have the function\ntriggered by events in the Cloud Storage bucket `my-bucket`, you use the\nfollowing command: \n\n gcloud functions deploy my-storage-function \\\n --no-gen2 \\\n --region=asia-northeast1 \\\n --runtime=java17 \\\n --source=./functions/storage-function \\\n --entry-point=myproject.StorageFunction \\\n --trigger-resource=gs://my-bucket \\\n --trigger-event=google.storage.object.delete\n\nNext steps\n----------\n\n- See details about [Cloud Run functions triggers](/functions/1stgendocs/calling).\n- Learn about the [Cloud Run functions build process](/functions/1stgendocs/building).\n- Explore additional Cloud Run functions [configuration options](/functions/1stgendocs/configuring).\n- Learn about [securing Cloud Run functions](/functions/1stgendocs/securing)."]]