Cloud Composer 1 is in the post-maintenance mode. Google
does not release any further updates to Cloud Composer 1, including new versions of Airflow, bugfixes, and security updates. We recommend planning
migration to Cloud Composer 2 .
Send feedback
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 | Cloud Composer 3
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 airflow.providers.google.cloud.hooks.compute_ssh import ComputeEngineSSHHook
GCE_INSTANCE = 'example - compute - instance '
GCE_ZONE = 'us - central1 - a '
GCP_PROJECT_ID = 'example - project '
with airflow . DAG (
'composer_compute_ssh_dag ',
start_date = datetime . datetime ( 2022 , 1 , 1 )
) as dag :
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
Send feedback
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License , and code samples are licensed under the Apache 2.0 License . For details, see the Google Developers Site Policies . Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-02 UTC.
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"Hard to understand"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"Incorrect information or sample code"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"Missing the information/samples I need"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
Need to tell us more?
{"lastModified": "Last updated 2024-10-02 UTC."}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-02 UTC."]]