Le 15 septembre 2026, tous les environnements Cloud Composer 1 et Cloud Composer 2 version 2.0.x atteindront leur fin de vie prévue et vous ne pourrez plus les utiliser. Nous vous recommandons de planifier la migration vers Cloud Composer 3.
GCP_PROJECT_ID avec l'ID de projet d'un projet dans lequel se trouvent la VM et l'environnement qui exécute le DAG.
importdatetimeimportairflowfromairflow.providers.ssh.operators.sshimportSSHOperatorfromairflow.providers.google.cloud.hooks.compute_sshimportComputeEngineSSHHookGCE_INSTANCE='example-compute-instance'GCE_ZONE='us-central1-a'GCP_PROJECT_ID='example-project'withairflow.DAG('composer_compute_ssh_dag',start_date=datetime.datetime(2022,1,1))asdag:ssh_task=SSHOperator(task_id='composer_compute_ssh_task',ssh_hook=ComputeEngineSSHHook(instance_name=GCE_INSTANCE,zone=GCE_ZONE,project_id=GCP_PROJECT_ID,use_oslogin=True,use_iap_tunnel=False,use_internal_ip=True),command='echo This command is executed from a DAG',dag=dag)
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/08/29 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/08/29 (UTC)."],[[["\u003cp\u003eThis page provides instructions on how to connect to a Compute Engine VM from a Directed Acyclic Graph (DAG).\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eComputeEngineSSHHook\u003c/code\u003e is used within the \u003ccode\u003essh_hook\u003c/code\u003e parameter of the \u003ccode\u003eSSHOperator\u003c/code\u003e to establish the connection to the Compute Engine VM.\u003c/p\u003e\n"],["\u003cp\u003eAn example demonstrates using \u003ccode\u003eSSHOperator\u003c/code\u003e to execute a command on a Compute Engine VM instance.\u003c/p\u003e\n"],["\u003cp\u003eTo make it work, users must replace \u003ccode\u003eGCE_INSTANCE\u003c/code\u003e, \u003ccode\u003eGCE_ZONE\u003c/code\u003e, and \u003ccode\u003eGCP_PROJECT_ID\u003c/code\u003e with their respective values.\u003c/p\u003e\n"],["\u003cp\u003eThe content shown on this page is currently for Cloud Composer 2, and it is not yet revised for Cloud Composer 3.\u003c/p\u003e\n"]]],[],null,["# Connect to a Compute Engine VM with SSHOperator\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\n**Cloud Composer 3** \\| [Cloud Composer 2](/composer/docs/composer-2/connect-gce-vm-sshoperator \"View this page for Cloud Composer 2\") \\| [Cloud Composer 1](/composer/docs/composer-1/connect-gce-vm-sshoperator \"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 describes how to connect to a Compute Engine VM from a DAG.\n\nCreate a DAG that connects to a Compute Engine VM instance\n----------------------------------------------------------\n\nIn the `ssh_hook` parameter of `SSHOperator`, use\n[`ComputeEngineSSHHook`](https://airflow.apache.org/docs/apache-airflow-providers-google/stable/_api/airflow/providers/google/cloud/hooks/compute_ssh/index.html) with parameters that point to\nthe Compute Engine VM.\n\nThe following example demonstrates how to use\n[`SSHOperator`](https://airflow.apache.org/docs/apache-airflow-providers-ssh/stable/_api/airflow/providers/ssh/operators/ssh/index.html) to run a command on a\nCompute Engine VM instance.\n\nReplace the values:\n\n- `GCE_INSTANCE` with the name of the VM instance.\n- `GCE_ZONE` with the [Compute Engine zone](/compute/docs/regions-zones/regions-zones) where the VM is located.\n- `GCP_PROJECT_ID` with the [Project ID](/resource-manager/docs/creating-managing-projects)\n of a project where the VM and the environment that runs the DAG is located.\n\n import datetime\n\n import airflow\n from airflow.providers.ssh.operators.ssh import SSHOperator\n from airflow.providers.google.cloud.hooks.compute_ssh import ComputeEngineSSHHook\n\n GCE_INSTANCE = 'example-compute-instance'\n GCE_ZONE = 'us-central1-a'\n GCP_PROJECT_ID = 'example-project'\n\n with airflow.DAG(\n 'composer_compute_ssh_dag',\n start_date=datetime.datetime(2025, 1, 1)\n ) as dag:\n\n ssh_task = SSHOperator(\n task_id='composer_compute_ssh_task',\n ssh_hook=ComputeEngineSSHHook(\n instance_name=GCE_INSTANCE,\n zone=GCE_ZONE,\n project_id=GCP_PROJECT_ID,\n use_oslogin=True,\n use_iap_tunnel=False,\n use_internal_ip=True),\n command='echo This command is executed from a DAG',\n dag=dag)\n\nWhat's next\n-----------\n\n- [Add and update DAGs](/composer/docs/composer-3/manage-dags)\n- [Schedule and trigger DAGs](/composer/docs/composer-3/schedule-and-trigger-dags)\n- [Troubleshooting DAGs](/composer/docs/composer-3/troubleshooting-dags)"]]