Il 15 settembre 2026, tutti gli ambienti Cloud Composer 1 e Cloud Composer 2 versione 2.0.x raggiungeranno la fine del ciclo di vita pianificata e non potrai più utilizzarli. Ti consigliamo di pianificare la migrazione a Cloud Composer 3.
Questa pagina spiega come impostare le variabili di ambiente nel tuo ambiente Cloud Composer.
Puoi utilizzare le variabili di ambiente come alternativa alle variabili Airflow quando fornisci la configurazione di runtime per i tuoi DAG. Le variabili Airflow vengono impostate nel database Airflow, mentre le variabili di ambiente vengono impostate nel contenitore del worker Airflow.
Ad esempio, puoi utilizzare il nome di una variabile di ambiente così com'è in un comando eseguito da un BashOperator, oppure recuperare questo valore dall'oggetto os.environ nel DAG o utilizzare una variabile Airflow.
Le variabili di ambiente sono permanenti. Una volta specificata una variabile di ambiente, Airflow continua a utilizzarla finché non la rimuovi dall'ambiente.
Cloud Composer imposta le proprie variabili di ambiente riservate per il controllo degli ambienti.
Formato del nome
Le variabili di ambiente devono corrispondere all'espressione regolare[a-zA-Z_][a-zA-Z0-9_]*.
Opzioni di configurazione di Airflow
Non è possibile specificare variabili che modificano le opzioni di configurazione di Apache Airflow.
Le variabili non possono utilizzare il formato AIRFLOW__{SECTION}__{KEY} o corrispondere all'espressione regolare AIRFLOW__[A-Z0-9_]+__[A-Z0-9_]+. Ad esempio, non è possibile impostare AIRFLOW__WEBSERVER__DAG_DEFAULT_VIEW.
Nel parametro updateMask, specifica la maschera config.softwareConfig.envVariables per sostituire tutte le variabili esistenti con le variabili specificate. Le variabili non specificate vengono eliminate.
Nel corpo della richiesta, specifica le variabili e i relativi valori:
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-08-29 UTC."],[[["\u003cp\u003eEnvironment variables in Cloud Composer provide runtime configuration for DAGs, distinct from Airflow variables stored in the Airflow database.\u003c/p\u003e\n"],["\u003cp\u003eThese environment variables are persistent, remaining in effect until they are explicitly removed from the environment.\u003c/p\u003e\n"],["\u003cp\u003eCertain environment variable names are reserved by Cloud Composer and cannot be used for custom variables, and variables that configure Airflow itself are not allowed.\u003c/p\u003e\n"],["\u003cp\u003eEnvironment variables can be set for both new and existing environments using the Google Cloud console, gcloud command-line tool, API requests, or Terraform.\u003c/p\u003e\n"],["\u003cp\u003eWhen updating variables for existing environments via API, it is done through the \u003ccode\u003eenvironments.patch\u003c/code\u003e method, replacing all existing variables with the new variables specified in the request body.\u003c/p\u003e\n"]]],[],null,["# Set environment variables\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\n[Cloud Composer 3](/composer/docs/composer-3/set-environment-variables \"View this page for Cloud Composer 3\") \\| [Cloud Composer 2](/composer/docs/composer-2/set-environment-variables \"View this page for Cloud Composer 2\") \\| **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-1/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-1/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- `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-1/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 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 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-1/override-airflow-configurations)\n- [Update environments](/composer/docs/composer-1/update-environments)"]]