Airflow Summit 2023
Join the Airflow community on September 19—21 during the Airflow Summit 2023 conference to learn more about Airflow and share your expertise. Call for papers is now open

Connect to a Compute Engine VM with SSHOperator

Stay organized with collections Save and categorize content based on your preferences.

Cloud Composer 1 | Cloud Composer 2

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

In the ssh_hook parameter of SSHOperator, use ComputeEngineSSHHook with parameters that point to the Compute Engine VM.

The following example demonstrates how to use SSHOperator to run a command on a Compute Engine VM instance.

Replace the values:

  • GCE_INSTANCE with the name of the VM instance.
  • GCE_ZONE with the Compute Engine zone where the VM is located.
  • GCP_PROJECT_ID with the Project ID of a project where the VM and the environment that runs the DAG is located.
import datetime

import airflow
from airflow.providers.ssh.operators.ssh import SSHOperator
from import ComputeEngineSSHHook

GCE_INSTANCE = 'example-compute-instance'
GCE_ZONE = 'us-central1-a'
GCP_PROJECT_ID = 'example-project'

with airflow.DAG(
        start_date=datetime.datetime(2022, 1, 1)
        ) as dag:

  ssh_task = SSHOperator(
      command='echo This command is executed from a DAG',

What's next