Access Airflow command-line interface

Cloud Composer 1 | Cloud Composer 2 | Cloud Composer 3

Apache Airflow has a command-line interface (CLI) that you can use to perform tasks such as triggering and managing DAGs, getting information about DAG runs and tasks, adding and deleting connections and users.

About CLI syntax versions

Airflow in Cloud Composer 1 provides two different syntaxes for CLI commands:

Supported Airflow CLI commands

For a full list of supported Airflow CLI commands, see gcloud composer environments run reference.

Before you begin

  • You must have enough permissions to use Google Cloud CLI with Cloud Composer and run Airflow CLI commands.

  • In Cloud Composer versions before 2.4.0, you need access to the control plane of your environment's cluster to run Airflow CLI commands.

Run Airflow CLI commands

To run Airflow CLI commands in your environments, use gcloud:

gcloud composer environments run ENVIRONMENT_NAME \
    --location LOCATION \
    SUBCOMMAND \
    -- SUBCOMMAND_ARGUMENTS

Replace:

  • ENVIRONMENT_NAME with the name of the environment.
  • LOCATION with the region where the environment is located.
  • SUBCOMMAND with one of the supported Airflow CLI commands.
  • SUBCOMMAND_ARGUMENTS with arguments for the Airflow CLI command.

Sub-command arguments separator

Separate the arguments for the specified Airflow CLI command with --:

Airflow 2

For Airflow 2 CLI syntax:

  • Specify compound CLI commands as a sub-command.
  • Specify any arguments for compound commands as sub-command arguments, after a -- separator.
gcloud composer environments run example-environment \
    dags list -- --output=json

Airflow 1

For Airflow 1.10 CLI syntax:

  • Specify CLI commands as a sub-command.
  • Specify any arguments after a -- separator.
gcloud composer environments run example-environment \
    list_dags -- --report

Default location

Most gcloud composer commands require a location. You can specify the location with the --location flag, or by setting the default location.

Example

For example, to trigger a DAG named sample_quickstart with the ID 5077 in your Cloud Composer environment:

Airflow 2

gcloud composer environments run example-environment \
    --location us-central1 dags trigger -- sample_quickstart \
    --run-id=5077

Airflow 1

gcloud composer environments run example-environment \
    --location us-central1 trigger_dag -- sample_quickstart \
    --run_id=5077

Running commands on a private IP environment

To run Airflow CLI commands on a Private IP environment, run them on a machine that can access the GKE cluster's control plane endpoint. Your options may vary depending on your private cluster configuration.

If public endpoint access is disabled in your environment's cluster, then it's not possible to use gcloud composer commands to run Airflow CLI. To be able to run Airflow CLI commands, perform the following steps:

  1. Create a VM in your VPC network
  2. Acquire cluster credentials. Run the following command: bash gcloud container clusters get-credentials CLUSTER_NAME \ --region REGION \ --project PROJECT \ --internal-ip
  • Use kubectl to run your Airflow command. For example:
kubectl exec deployment/airflow-scheduler -n COMPOSER_NAMESPACE \
  --container airflow-scheduler -- airflow dags list

Replace COMPOSER_NAMESPACE withe a namespace similar to: composer-2-0-28-airflow-2-3-394zxc12411. You can find your Cloud Composer in the workloads list or by using the kubectl get namespaces command.

If public endpoint access is enabled in your environment's cluster, you can also run Airflow CLI commands from a machine with an external IP address that is added to authorized networks. To enable access from your machine, add the external address of your machine to your environment's list of authorized networks.

Troubleshooting

No connectivity to the cluster control plane

When running gcloud composer environments run or kubectl commands, you might encounter the following error:

Get "https://<IP Address>/api?timeout=32s": dial tcp <IP Address>:443: i/o timeout"

Symptom: This error message indicates that there is no network connectivity from a computer where you run these commands.

Solution: Follow the guidelines presented in the Running commands on a private IP environment section or use the instructions available in the kubectl command times out section.