

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Apache Airflow CLI 命令參考
<a name="airflow-cli-command-reference"></a>

本主題說明 Amazon Managed Workflows for Apache Airflow 上支援和不支援的 Apache Airflow CLI 命令。

**提示**  
REST API 比 CLI 更現代化，旨在與外部系統進行程式設計整合。REST 是與 Apache Airflow 互動的偏好方式。

**Contents**
+ [先決條件](#airflow-cli-command-prereqs)
  + [存取](#access-airflow-ui-prereqs-access)
  + [AWS CLI](#access-airflow-ui-prereqs-cli)
+ [有何變更？](#airflow-cli-command-changed)
+ [支援的 CLI 命令](#airflow-cli-commands)
  + [支援的命令](#airflow-cli-commands-supported)
  + [使用剖析 DAGs命令](#parsing-support)
+ [範本程式碼](#airflow-cli-command-examples)
  + [設定、取得或刪除 Apache Airflow v2 變數](#example-airflow-cli-commands-bash)
  + [觸發 DAG 時新增組態](#example-airflow-cli-commands-trigger)
  + [在對堡壘主機的 SSH 通道上執行 CLI 命令](#example-airflow-cli-commands-private)

## 先決條件
<a name="airflow-cli-command-prereqs"></a>

下一節說明使用此頁面上命令和指令碼所需的初步步驟。

### 存取
<a name="access-airflow-ui-prereqs-access"></a>
+ AWS 帳戶 在 AWS Identity and Access Management (IAM) 中存取 Amazon MWAA 許可政策[Apache Airflow UI 存取政策：AmazonMWAAWebServerAccess](access-policies.md#web-ui-access)。
+ AWS 帳戶 在 AWS Identity and Access Management (IAM) 中存取 Amazon MWAA 許可政策 [完整 API 和主控台存取政策：AmazonMWAAFullApiAccess](access-policies.md#full-access-policy)。

### AWS CLI
<a name="access-airflow-ui-prereqs-cli"></a>

 AWS Command Line Interface (AWS CLI) 是一種開放原始碼工具，您可以使用命令列 shell 中的 命令與 AWS 服務互動。若要完成此頁面上的步驟，您需要下列項目：
+ [AWS CLI – 安裝第 2 版](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)。
+ [AWS CLI – 使用 的快速組態`aws configure`](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)。

## 有何變更？
<a name="airflow-cli-command-changed"></a>
+ **v3：氣流架構**。Apache Airflow v3 引進突破性的架構變更，以提供更高的安全性和可擴展性，並使維護更容易。若要進一步了解，請參閱[升級至氣流 3](https://airflow.apache.org/docs/apache-airflow/stable/installation/upgrading_to_airflow3.html)。
+ **v2：Airflow CLI 命令結構**。Apache Airflow v2 CLI 經過組織，因此相關命令會分組為子命令，這表示如果您想要升級至 Apache Airflow v2，則需要更新 Apache Airflow v1 指令碼。例如，`unpause`在 Apache Airflow v1 `dags unpause`中為 Apache Airflow v2。若要進一步了解，請參閱 [2.0 中的 Airflow CLI 變更。](http://airflow.apache.org/docs/apache-airflow/2.0.2/upgrading-to-2.html#airflow-cli-changes-in-2-0)

## 支援的 CLI 命令
<a name="airflow-cli-commands"></a>

下一節列出 Amazon MWAA 上可用的 Apache Airflow CLI 命令。

### 支援的命令
<a name="airflow-cli-commands-supported"></a>

------
#### [ Apache Airflow v3 ]


| 次要版本 | 命令 | 
| --- | --- | 
|  3.0.6 版  |  [資產詳細資訊](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#details)  | 
|  3.0.6 版  |  [資產清單](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#list)  | 
|  3.0.6 版  |  [資產具體化](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#materialize)  | 
|  3.0.6 版  |  [回填建立](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#create)  | 
|  3.0.6 版  |  [cheat-sheet](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#cheat-sheet)  | 
|  3.0.6 版  |  [連線新增](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#add)  | 
|  3.0.6 版  |  [連線刪除](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete)  | 
|  3.0.6 版  |  [dags 刪除](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete_repeat1)  | 
|  3.0.6 版  |  [dags 清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat2)  | 
|  3.0.6 版  |  [dags list-jobs](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list-jobs)  | 
|  3.0.6 版  |  [dags list-import-errors](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#list-import-errors)  | 
|  3.0.6 版  |  [dags list-runs](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list-runs)  | 
|  3.0.6 版  |  [dags next-execution](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#next-execution)  | 
|  3.0.6 版  |  [dags 暫停](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#pause)  | 
|  3.0.6 版  |  [dags 報告](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#report)  | 
|  3.0.6 版  |  [dags 重新序列化](https://airflow.apache.org/docs/apache-airflow/2.4.3/cli-and-env-variables-ref.html#reserialize)  | 
|  3.0.6 版  |  [dags show](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#show)  | 
|  3.0.6 版  |  [dags 狀態](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#state)  | 
|  3.0.6 版  |  [dags 測試](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#test)  | 
|  3.0.6 版  |  [dags 觸發條件](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#trigger)  | 
|  3.0.6 版  |  [dags unpause](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#unpause)  | 
|  3.0.6 版  |  [db 清除](https://airflow.apache.org/docs/apache-airflow/2.4.3/cli-and-env-variables-ref.html#clean)  | 
|  3.0.6 版  |  [供應商行為](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#behaviours)  | 
|  3.0.6 版  |  [供應商取得](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#get_repeat2)  | 
|  3.0.6 版  |  [提供者勾點](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#hooks)  | 
|  3.0.6 版  |  [供應商連結](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#links)  | 
|  3.0.6 版  |  [供應商清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat4)  | 
|  3.0.6 版  |  [供應商通知](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#notifications)  | 
|  3.0.6 版  |  [提供者秘密](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#secrets)  | 
|  3.0.6 版  |  [提供者觸發器](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#triggerer)  | 
|  3.0.6 版  |  [提供者小工具](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#widgets)  | 
|  3.0.6 版  |  [角色 add-perms](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#add-perms)  | 
|  3.0.6 版  |  [角色 del-perms](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#del-perms)  | 
|  3.0.6 版  |  [角色建立](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#create)  | 
|  3.0.6 版  |  [角色清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat5)  | 
|  3.0.6 版  |  [任務清除](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#clear)  | 
|  3.0.6 版  |  [任務故障裝置](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#failed-deps)  | 
|  3.0.6 版  |  [任務清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat6)  | 
|  3.0.6 版  |  [任務轉譯](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#render)  | 
|  3.0.6 版  |  [任務狀態](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#state_repeat1)  | 
|  3.0.6 版  |  [任務 states-for-dag-run](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#states-for-dag-run)  | 
|  3.0.6 版  |  [任務測試](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#test_repeat1)  | 
|  3.0.6 版  |  [變數刪除](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete_repeat4)  | 
|  3.0.6 版  |  [變數取得](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#get_repeat3)  | 
|  3.0.6 版  |  [變數集](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#set_repeat1)  | 
|  3.0.6 版  |  [變數清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat8)  | 
|  3.0.6 版  |  [version](http://airflow.apache.org/docs/apache-airflow/1.10.12/cli-ref.html#version)  | 

------
#### [ Apache Airflow v2 ]


| 次要版本 | 命令 | 
| --- | --- | 
|  v2.0\$1  |  [cheat-sheet](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#cheat-sheet)  | 
|  v2.0\$1  |  [連線新增](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#add)  | 
|  v2.0\$1  |  [連線刪除](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete)  | 
|  v2.2\$1 ([備註](#parsing-support))  |  [dags 回填](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#backfill)  | 
|  v2.0\$1  |  [dags 刪除](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete_repeat1)  | 
|  v2.2\$1 ([備註](#parsing-support))  |  [dags 清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat2)  | 
|  v2.0\$1  |  [dags list-jobs](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list-jobs)  | 
|  v2.6\$1  |  [dags list-import-errors](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#list-import-errors)  | 
|  v2.2\$1 ([備註](#parsing-support))  |  [dags list-runs](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list-runs)  | 
|  v2.2\$1 ([備註](#parsing-support))  |  [dags next-execution](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#next-execution)  | 
|  v2.0\$1  |  [dags 暫停](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#pause)  | 
|  v2.0\$1  |  [dags 報告](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#report)  | 
|  v2.4\$1  |  [dags 重新序列化](https://airflow.apache.org/docs/apache-airflow/2.4.3/cli-and-env-variables-ref.html#reserialize)  | 
|  v2.0\$1  |  [dags show](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#show)  | 
|  v2.0\$1  |  [dags 狀態](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#state)  | 
|  v2.0\$1  |  [dags 測試](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#test)  | 
|  v2.0\$1  |  [dags 觸發條件](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#trigger)  | 
|  v2.0\$1  |  [dags 取消暫停](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#unpause)  | 
|  v2.4\$1  |  [db 清除](https://airflow.apache.org/docs/apache-airflow/2.4.3/cli-and-env-variables-ref.html#clean)  | 
|  v2.0\$1  |  [提供者行為](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#behaviours)  | 
|  v2.0\$1  |  [供應商取得](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#get_repeat2)  | 
|  v2.0\$1  |  [提供者勾點](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#hooks)  | 
|  v2.0\$1  |  [供應商連結](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#links)  | 
|  v2.0\$1  |  [提供者清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat4)  | 
|  v2.8\$1  |  [供應商通知](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#notifications)  | 
|  v2.6\$1  |  [提供者秘密](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#secrets)  | 
|  v2.7\$1  |  [提供者觸發器](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#triggerer)  | 
|  v2.0\$1  |  [提供者小工具](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#widgets)  | 
|  v2.6\$1  |  [角色 add-perms](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#add-perms)  | 
|  v2.6\$1  |  [角色 del-perms](https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#del-perms)  | 
|  v2.6\$1  |  [角色建立](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#create)  | 
|  v2.0\$1  |  [角色清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat5)  | 
|  v2.0\$1  |  [任務清除](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#clear)  | 
|  v2.0\$1  |  [任務故障裝置](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#failed-deps)  | 
|  v2.0\$1  |  [任務清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat6)  | 
|  v2.0\$1  |  [任務轉譯](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#render)  | 
|  v2.0\$1  |  [任務執行](https://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#run)  | 
|  v2.0\$1  |  [任務狀態](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#state_repeat1)  | 
|  v2.0\$1  |  [任務 states-for-dag-run](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#states-for-dag-run)  | 
|  v2.0\$1  |  [任務測試](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#test_repeat1)  | 
|  v2.0\$1  |  [變數刪除](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#delete_repeat4)  | 
|  v2.0\$1  |  [變數取得](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#get_repeat3)  | 
|  v2.0\$1  |  [變數集](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#set_repeat1)  | 
|  v2.0\$1  |  [變數清單](http://airflow.apache.org/docs/apache-airflow/2.2.2/cli-and-env-variables-ref.html#list_repeat8)  | 
|  v2.0\$1  |  [version](http://airflow.apache.org/docs/apache-airflow/1.10.12/cli-ref.html#version)  | 

------

### 使用剖析 DAGs命令
<a name="parsing-support"></a>

如果您的環境執行 Apache Airflow v2.0.2，如果 DAGs 使用依賴透過 安裝的套件的外掛程式，則剖析 DAG 的 CLI 命令將會失敗`requirements.txt`：

**Apache Airflow 2.0.2 版**
+ `dags backfill`
+ `dags list`
+ `dags list-runs`
+ `dags next-execution`

如果您的 DAGs 不使用依賴透過 安裝的套件的外掛程式，您可以使用這些 CLI 命令`requirements.txt`。

## 範本程式碼
<a name="airflow-cli-command-examples"></a>

下一節包含使用 Apache Airflow CLI 的不同方法範例。

### 設定、取得或刪除 Apache Airflow v2 變數
<a name="example-airflow-cli-commands-bash"></a>

您可以使用下列範例程式碼來設定、取得或刪除格式為 的變數`<script> <mwaa env name> get | set | delete <variable> <variable value> </variable> </variable>`。

```
[ $# -eq 0 ] && echo "Usage: $0 MWAA environment name " && exit

if [[ $2 == "" ]]; then
    dag="variables list"

elif  [ $2 == "get" ] ||  [ $2 == "delete" ] ||  [ $2 == "set" ]; then
    dag="variables $2 $3 $4 $5"

else
    echo "Not a valid command"
    exit 1
fi

CLI_JSON=$(aws mwaa --region $AWS_REGION create-cli-token --name $1) \
    && CLI_TOKEN=$(echo $CLI_JSON | jq -r '.CliToken') \
    && WEB_SERVER_HOSTNAME=$(echo $CLI_JSON | jq -r '.WebServerHostname') \
    && CLI_RESULTS=$(curl --request POST "https://$WEB_SERVER_HOSTNAME/aws_mwaa/cli" \
    --header "Authorization: Bearer $CLI_TOKEN" \
    --header "Content-Type: text/plain" \
    --data-raw "$dag" ) \
    && echo "Output:" \
    && echo $CLI_RESULTS | jq -r '.stdout' | base64 --decode \
    && echo "Errors:" \
    && echo $CLI_RESULTS | jq -r '.stderr' | base64 --decode
```

### 觸發 DAG 時新增組態
<a name="example-airflow-cli-commands-trigger"></a>

您可以在觸發 DAG 時使用下列範例程式碼搭配 Apache Airflow v2 來新增組態，例如 `airflow trigger_dag 'dag_name' —conf '{"key":"value"}'`。

```
import boto3
import json
import requests 
import base64
				
  mwaa_env_name = 'YOUR_ENVIRONMENT_NAME'
  dag_name = 'YOUR_DAG_NAME'
  key = "YOUR_KEY"
  value = "YOUR_VALUE"
  conf = "{\"" + key + "\":\"" + value + "\"}"
				
  client = boto3.client('mwaa')
				
  mwaa_cli_token = client.create_cli_token(
    Name=mwaa_env_name
  )
				
  mwaa_auth_token = 'Bearer ' + mwaa_cli_token['CliToken']
  mwaa_webserver_hostname = 'https://{0}/aws_mwaa/cli'.format(mwaa_cli_token['WebServerHostname'])
  raw_data = "trigger_dag {0} -c '{1}'".format(dag_name, conf)
				
  mwaa_response = requests.post(
    mwaa_webserver_hostname,
    headers={
      'Authorization': mwaa_auth_token,
      'Content-Type': 'text/plain'
    },
    data=raw_data
  )
				
  mwaa_std_err_message = base64.b64decode(mwaa_response.json()['stderr']).decode('utf8')
  mwaa_std_out_message = base64.b64decode(mwaa_response.json()['stdout']).decode('utf8')
				
  print(mwaa_response.status_code)
  print(mwaa_std_err_message)
  print(mwaa_std_out_message)
```

### 在對堡壘主機的 SSH 通道上執行 CLI 命令
<a name="example-airflow-cli-commands-private"></a>

使用以下範例，使用 SSH 通道代理對 Linux 堡壘主機執行 Airflow CLI 命令。

**使用 curl**

1. 

   ```
   ssh -D 8080 -f -C -q -N YOUR_USER@YOUR_BASTION_HOST
   ```

1. 

   ```
   curl -x socks5h://0:8080 --request POST https://YOUR_HOST_NAME/aws_mwaa/cli --header YOUR_HEADERS --data-raw YOUR_CLI_COMMAND
   ```