Connect to a Compute Engine VM with SSHOperator

Cloud Composer 3 | Cloud Composer 2 | Cloud Composer 1

This page describes how to connect to a Compute Engine VM from a DAG.

Create a DAG that connects to a Compute Engine VM instance

Note: This approach is available only in Airflow 2.

In thessh_hook parameter ofSSHOperator, useComputeEngineSSHHook with parameters that point tothe Compute Engine VM.

The following example demonstrates how to useSSHOperator to run a command on aCompute Engine VM instance.

Replace the values:

  • GCE_INSTANCE with the name of the VM instance.
  • GCE_ZONE with theCompute Engine zone wherethe VM is located.
  • GCP_PROJECT_ID with theProject IDof a project where the VM and the environment that runs the DAG is located.

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(2025,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)

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.