[[["容易理解","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-03 (世界標準時間)。"],[[["\u003cp\u003eThis document outlines how to manage environment variables within Cloud Composer environments, which can be used as an alternative to Airflow variables for runtime configurations.\u003c/p\u003e\n"],["\u003cp\u003eEnvironment variables are persistent and are stored within the Airflow worker's container, and are set outside of the Airflow database.\u003c/p\u003e\n"],["\u003cp\u003eThere are specific rules for naming variables and restrictions on using variables to alter Airflow's configuration.\u003c/p\u003e\n"],["\u003cp\u003eCloud Composer reserves a set of variable names that cannot be used, as they are reserved for internal processes, and these should not be referred to in your DAGs.\u003c/p\u003e\n"],["\u003cp\u003eEnvironment variables can be set when creating a new environment or modified in existing environments through the Google Cloud console, gcloud commands, the API, or Terraform.\u003c/p\u003e\n"]]],[],null,["# Set environment variables\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\n**Cloud Composer 3** \\| [Cloud Composer 2](/composer/docs/composer-2/set-environment-variables \"View this page for Cloud Composer 2\") \\| [Cloud Composer 1](/composer/docs/composer-1/set-environment-variables \"View this page for Cloud Composer 1\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis page explains how to set environment variables in your\nCloud Composer environment.\n\nYou can use environment variables as an alternative to\n[Airflow variables](https://airflow.apache.org/docs/apache-airflow/stable/core-concepts/variables.html) when providing runtime configuration\nfor your DAGs. Airflow variables are set in the Airflow database, while\nenvironment variables are set in the Airflow worker's container.\n\nAs an example, you can use an environment variable's name as is in a command\nthat is run by a BashOperator, or get this value from the `os.environ` object\nin the DAG, or use an Airflow variable instead.\n\nEnvironment variables are persistent. Once you specify an environment variable,\nAirflow keeps using it until you remove the variable from your environment.\n| **Important:** It's not possible to use environment variables to configure Airflow. [Override Airflow configuration options](/composer/docs/composer-3/override-airflow-configurations) instead.\n\nCloud Composer sets its own reserved environment variables for controlling\nenvironments.\n\nName format\n-----------\n\nEnvironment variables must match the regular expression\n`[a-zA-Z_][a-zA-Z0-9_]*`.\n\nAirflow configuration options\n-----------------------------\n\nIt is not possible to specify variables that change Apache Airflow\nconfiguration options.\n\nThe variables cannot use the `AIRFLOW__{SECTION}__{KEY}` format or match the\nregular expression `AIRFLOW__[A-Z0-9_]+__[A-Z0-9_]+`. For example, it is not\npossible to set `AIRFLOW__WEBSERVER__DAG_DEFAULT_VIEW`.\n\nInstead, you can [override Airflow configuration options](/composer/docs/composer-3/override-airflow-configurations).\n\nReserved variables\n------------------\n\nThe following variable names are reserved by Cloud Composer. It is\nnot possible to create variables with these names.\n\n- `AIRFLOW_DATABASE_VERSION`\n- `AIRFLOW_HOME`\n- `AIRFLOW_SRC_DIR`\n- `AIRFLOW_WEBSERVER`\n- `AUTO_GKE`\n- `CLOUDSDK_METRICS_ENVIRONMENT`\n- `CLOUD_LOGGING_ONLY`\n- `COMPOSER_AGENT_BUILD_SERVICE_ACCOUNT`\n- `COMPOSER_ENVIRONMENT`\n- `COMPOSER_GKE_LOCATION`\n- `COMPOSER_GKE_NAME`\n- `COMPOSER_GKE_ZONE`\n- `COMPOSER_LOCATION`\n- `COMPOSER_OPERATION_UUID`\n- `COMPOSER_PYTHON_VERSION`\n- `COMPOSER_VERSION`\n- `CONTAINER_NAME`\n- `C_FORCE_ROOT`\n- `DAGS_FOLDER`\n- `GCE_METADATA_TIMEOUT`\n- `GCP_PROJECT`\n- `GCP_TENANT_PROJECT`\n- `GCSFUSE_EXTRACTED`\n- `GCS_BUCKET`\n- `GKE_CLUSTER_NAME`\n- `GKE_IN_TENANT`\n- `GOOGLE_APPLICATION_CREDENTIALS`\n\n- `GOOGLE_CLOUD_PROJECT`\n\n- `MAJOR_VERSION`\n\n- `MINOR_VERSION`\n\n- `PATH`\n\n- `PIP_DISABLE_PIP_VERSION_CHECK`\n\n- `PORT`\n\n- `PROJECT_ID`\n\n- `PYTHONPYCACHEPREFIX`\n\n- `PYTHONWARNINGS`\n\n- `REDIS_PASSWORD`\n\n- `REDIS_PORT`\n\n- `REDIS_USER`\n\n- `SQL_DATABASE`\n\n- `SQL_HOST`\n\n- `SQL_INSTANCE`\n\n- `SQL_PASSWORD`\n\n- `SQL_PROJECT`\n\n- `SQL_REGION`\n\n- `SQL_USER`\n\n| **Caution:** Cloud Composer uses these reserved names for variables that are used by its processes. Don't refer to reserved names in your DAGs. Values of these variables can change without notice.\n\nSet environment variables for new environments\n----------------------------------------------\n\nYou can specify environment variables when you create an environment. For more\ninformation, see [Create environments](/composer/docs/composer-3/create-environments#overrides-env-vars).\n\nSet environment variables for existing environments\n---------------------------------------------------\n\n### Console\n\nTo set environment variables for an existing environment:\n\n1. In Google Cloud console, go to the **Environments** page.\n\n [Go to Environments](https://console.cloud.google.com/composer/environments)\n2. In the list of environments, click the name of your environment.\n The **Environment details** page opens.\n\n3. Go to the **Environment variables** tab and click **Edit**.\n\n4. Add, change, or delete environment variables for your environment:\n\n - In the **Name** field, specify the variable name.\n - In the **Value** field, specify the variable value.\n - To add an extra variable, click **Add environment variable**.\n - To delete a variable, hold the pointer over a variable, then click **Delete item**.\n\n### gcloud\n\nFollowing arguments control environment variables:\n\n- `--update-env-variables` specifies a comma-separated list of environment\n variables to create or update.\n\n- `--remove-env-variables` specifies a comma-separated list of environment\n variables to delete.\n\n- `--clear-env-variables` deletes all existing environment variables.\n\n gcloud composer environments update \\\n \u003cvar translate=\"no\"\u003eENVIRONMENT_NAME\u003c/var\u003e \\\n --location \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e \\\n --update-env-variables=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nv\"\u003eNAME\u003c/span\u003e\u003c/var\u003e=\u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e\n\nReplace:\n\n- `ENVIRONMENT_NAME` with the name of the environment.\n- `LOCATION` with the region where the environment is located.\n- `NAME` with the name of the variable that you want to create or update.\n- `VALUE` with the value for the variable.\n- To add more than one variable, separate the definitions with commas: `NAME=VALUE,NAME=VALUE,...`.\n\nExample: \n\n gcloud composer environments update \\\n example-environment \\\n --location us-central1 \\\n --update-env-variables=EXAMPLE_VARIABLE=True,ANOTHER_VARIABLE=test\n\n### API\n\nConstruct an [`environments.patch`](/composer/docs/reference/rest/v1/projects.locations.environments/patch) API request.\n\nIn this request:\n\n1. In the `updateMask` parameter, specify the\n `config.softwareConfig.envVariables` mask to replace all existing\n variables with the specified variables. Variables that you don't specify\n are deleted.\n\n2. In the request body, specify variables and their values:\n\n {\n \"config\": {\n \"softwareConfig\": {\n \"envVariables\": {\n \"\u003cvar translate=\"no\"\u003eVAR_NAME\u003c/var\u003e\": \"\u003cvar translate=\"no\"\u003eVAR_VALUE\u003c/var\u003e\"\n }\n }\n }\n }\n\n Replace:\n - `VAR_NAME` with the name of the environment variable.\n - `VAR_VALUE` with the value of the environment variable.\n - To add more than one variable, add extra entries for variables to `envVariables`.\n\nExample: \n\n // PATCH https://composer.googleapis.com/v1/projects/example-project/\n // locations/us-central1/environments/example-environment?updateMask=\n // config.softwareConfig.envVariables\n {\n \"config\": {\n \"softwareConfig\": {\n \"envVariables\": {\n \"EXAMPLE_VARIABLE\": \"True\",\n \"ANOTHER_VARIABLE\": \"test\"\n }\n }\n }\n }\n\n### Terraform\n\nThe `env_variables` block in the `software_config` block specifies\nenvironment variables. \n\n resource \"google_composer_environment\" \"example\" {\n provider = google-beta\n name = \"\u003cvar translate=\"no\"\u003eENVIRONMENT_NAME\u003c/var\u003e\"\n region = \"\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e\"\n\n config {\n\n software_config {\n\n env_variables = {\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-na\"\u003eVAR_NAME\u003c/span\u003e\u003c/var\u003e = \"\u003cvar translate=\"no\"\u003eVAR_VALUE\u003c/var\u003e\"\n }\n\n }\n }\n }\n\nReplace:\n\n- `ENVIRONMENT_NAME` with the name of the environment.\n- `LOCATION` with the region where the environment is located.\n- `VAR_NAME` with the name of the environment variable.\n- `VAR_VALUE` with the value of the environment variable.\n- To add more than one variable, add extra entries for variables to `env_variables`.\n\nExample: \n\n resource \"google_composer_environment\" \"example\" {\n provider = google-beta\n name = \"example-environment\"\n region = \"us-central1\"\n\n config {\n\n software_config {\n\n env_variables = {\n EXAMPLE_VARIABLE = \"True\"\n ANOTHER_VARIABLE = \"test\"\n }\n }\n }\n }\n\nWhat's next\n-----------\n\n- [Override Airflow configuration options](/composer/docs/composer-3/override-airflow-configurations)\n- [Update environments](/composer/docs/composer-3/update-environments)"]]