

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

# 安裝 Python 相依性
<a name="working-dags-dependencies"></a>

Python 相依性是 Amazon Managed Workflows for Apache Airflow 環境上 Apache Airflow 版本 Apache Airflow 基本安裝中未包含的任何套件或分佈。本主題說明使用 Amazon S3 儲存貯體中的`requirements.txt`檔案在 Amazon MWAA 環境上安裝 Apache Airflow Python 相依性的步驟。

**Contents**
+ [先決條件](#working-dags-dependencies-prereqs)
+ [運作方式](#working-dags-dependencies-how)
+ [Python 相依性概觀](#working-dags-dependencies-overview)
  + [Python 相依性位置和大小限制](#working-dags-dependencies-quota)
+ [建立 requirements.txt 檔案](#working-dags-dependencies-test-create)
  + [步驟一：使用 Amazon MWAA CLI 公用程式測試 Python 相依性](#working-dags-dependencies-cli-utility)
  + [步驟二：建立 `requirements.txt`](#working-dags-dependencies-syntax-create)
+ [`requirements.txt` 上傳至 Amazon S3](#configuring-dag-dependencies-upload)
  + [使用 AWS CLI](#configuring-dag-dependencies-upload-cli)
  + [使用 Amazon S3 主控台](#configuring-dag-dependencies-upload-console)
+ [在您的環境上安裝 Python 相依性](#configuring-dag-dependencies-installing)
  + [在 Amazon MWAA 主控台`requirements.txt`上指定 的路徑 （第一次）](#configuring-dag-dependencies-first)
  + [在 Amazon MWAA 主控台上指定`requirements.txt`版本](#working-dags-dependencies-mwaaconsole-version)
+ [存取 的日誌 `requirements.txt`](#working-dags-dependencies-logs)
+ [後續步驟？](#working-dags-dependencies-next-up)

## 先決條件
<a name="working-dags-dependencies-prereqs"></a>

您將需要下列項目，才能完成此頁面上的步驟。
+ **許可** — 您的管理員 AWS 帳戶 必須已授予您環境的 [AmazonMWAAFullConsoleAccess](access-policies.md#console-full-access) 存取控制政策的存取權。此外，您的[執行角色](mwaa-create-role.md)必須允許您的 Amazon MWAA 環境，才能存取您的環境所使用的 AWS 資源。
+ **存取** — 如果您需要存取公有儲存庫，才能直接在 Web 伺服器上安裝相依性，您的環境必須設定**公有網路** Web 伺服器存取。如需詳細資訊，請參閱 [Apache Airflow 存取模式](configuring-networking.md)。
+ **Amazon S3 組態** — 用於在 中存放 DAGs、自訂外掛程式`plugins.zip`和 Python 相依性的 [Amazon S3 儲存貯](mwaa-s3-bucket.md)體`requirements.txt`必須設定為啟用*公開存取封鎖*和*版本控制*。

## 運作方式
<a name="working-dags-dependencies-how"></a>

在 Amazon MWAA 上，您可以將`requirements.txt`檔案上傳到 Amazon S3 儲存貯體，然後在每次更新檔案時，在 Amazon MWAA 主控台上指定檔案的版本，以安裝所有 Python 相依性。Amazon MWAA 會執行 `pip3 install -r requirements.txt` 在 Apache Airflow 排程器和每個工作者上安裝 Python 相依性。

若要在您的環境上執行 Python 相依性，您必須執行三項作業：

1. 在本機建立`requirements.txt`檔案。

1. 將本機 上傳至您的 Amazon S3 `requirements.txt`儲存貯體。

1. 在 Amazon MWAA 主控台的要求檔案欄位中指定此**檔案**的版本。

**注意**  
如果這是您第一次建立 並將其上傳至 Amazon S3 `requirements.txt`儲存貯體，您也需要在 Amazon MWAA 主控台上指定 檔案的路徑。您只需要完成此步驟一次。

## Python 相依性概觀
<a name="working-dags-dependencies-overview"></a>

您可以從 Python 套件索引 (PyPi.org), `.whl` PyPi

### Python 相依性位置和大小限制
<a name="working-dags-dependencies-quota"></a>

Apache Airflow 排程器和工作者會搜尋 `requirements.txt` 檔案中的套件，而套件會安裝在 的環境。 `/usr/local/airflow/.local/bin`
+ **大小限制**。我們建議`requirements.txt`檔案參考合併大小小於 1 GB 的程式庫。Amazon MWAA 需要安裝的程式庫越多，環境的*啟動*時間就越長。雖然 Amazon MWAA 不會明確限制已安裝程式庫的大小，但如果無法在十分鐘內安裝相依性，則 Fargate 服務將會逾時，並嘗試將環境復原至穩定狀態。

## 建立 requirements.txt 檔案
<a name="working-dags-dependencies-test-create"></a>

下列步驟說明我們建議在本機建立 requirements.txt 檔案的步驟。

### 步驟一：使用 Amazon MWAA CLI 公用程式測試 Python 相依性
<a name="working-dags-dependencies-cli-utility"></a>
+ 命令列界面 (CLI) 公用程式會在本機複寫 Amazon Managed Workflows for Apache Airflow 環境。
+ CLI 會在本機建置類似於 Amazon MWAA 生產映像的 Docker 容器映像。您可以使用它來執行本機 Apache Airflow 環境，以在部署到 Amazon MWAA 之前開發和測試 DAGs、自訂外掛程式和相依性。
+ 若要執行 CLI，請參閱 GitHub 上的 [aws-mwaa-docker-images](https://github.com/aws/amazon-mwaa-docker-images)。

### 步驟二：建立 `requirements.txt`
<a name="working-dags-dependencies-syntax-create"></a>

下一節說明如何從`requirements.txt`檔案中的 Python [套件索引指定 Python](https://pypi.org/) 相依性。

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

1. 在**本機測試**。在建立`requirements.txt`檔案之前，反覆新增其他程式庫，以尋找套件及其版本的正確組合。若要執行 Amazon MWAA CLI 公用程式，請參閱 GitHub 上的 [aws-mwaa-docker-images](https://github.com/aws/amazon-mwaa-docker-images)。

1. **檢閱 Apache Airflow 套件額外項目**。若要存取 Amazon MWAA 上為 Apache Airflow v3 安裝的套件清單，請參閱 GitHub 網站上的 [aws-mwaa-docker-images`requirements.txt`](https://github.com/aws/amazon-mwaa-docker-images/blob/main/requirements.txt)。

1. **新增限制條件陳述式**。在檔案頂端新增 Apache Airflow v3 環境的限制條件`requirements.txt`檔案。Apache Airflow 限制條件檔案會指定 Apache Airflow 發行時可用的提供者版本。

    在下列範例中，將 *\$1environment-version\$1* 取代為您環境的版本編號，並將 *\$1Python-version\$1* 取代為您環境相容的 Python 版本。

   如需有關與 Apache Airflow 環境相容的 Python 版本的資訊，請參閱 [Apache Airflow 版本](airflow-versions.md#airflow-versions-official)。

   ```
   --constraint "https://raw.githubusercontent.com/apache/airflow/constraints-{Airflow-version}/constraints-{Python-version}.txt"
   ```

    如果限制條件檔案判斷`xyz==1.0`套件與您環境中的其他套件不相容， `pip3 install` 將無法防止不相容的程式庫安裝到您的環境。如果任何套件的安裝失敗，您可以在 CloudWatch Logs 的對應日誌串流中存取每個 Apache Airflow 元件 （排程器、工作者和 Web 伺服器） 的錯誤日誌。如需日誌類型的詳細資訊，請參閱 [在 Amazon CloudWatch 中存取 Airflow 日誌](monitoring-airflow.md)。

1. **Apache Airflow 套件**。新增[套件額外項目](http://airflow.apache.org/docs/apache-airflow/2.5.1/extra-packages-ref.html)和版本 (`==`)。這有助於防止相同名稱但不同版本的套件安裝在您的環境中。

   ```
   apache-airflow[package-extra]==2.5.1
   ```

1. **Python 程式庫**。在 `requirements.txt` 檔案中新增套件名稱和版本 (`==`)。這有助於防止 [PyPi.org](https://pypi.org) 未來的重大更新自動套用。

   ```
   library == version
   ```  
**Example Boto3 和 psycopg2-binary**  

   此範例僅供示範之用。boto 和 psycopg2-binary 程式庫包含在 Apache Airflow v3 的基本安裝中，不需要在`requirements.txt`檔案中指定。

   ```
   boto3==1.17.54
   boto==2.49.0
   botocore==1.20.54
   psycopg2-binary==2.8.6
   ```

   如果指定的套件沒有版本，Amazon MWAA 會從 [PyPi.org](https://pypi.org) 安裝最新版本的套件。此版本可能與 中的其他套件衝突`requirements.txt`。

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

1. 在**本機測試**。在建立`requirements.txt`檔案之前，反覆新增其他程式庫，以尋找套件及其版本的正確組合。若要執行 Amazon MWAA CLI 公用程式，請參閱 GitHub 上的 [aws-mwaa-docker-images](https://github.com/aws/amazon-mwaa-docker-images)。

1. **檢閱 Apache Airflow 套件額外項目**。若要存取 Amazon MWAA 上為 Apache Airflow v2 安裝的套件清單，請存取 GitHub 網站上的 [aws-mwaa-docker-images`requirements.txt`](https://github.com/aws/amazon-mwaa-docker-images/blob/main/requirements.txt)。

1. **新增限制條件陳述式**。在檔案頂端新增 Apache Airflow v2 環境的限制條件`requirements.txt`檔案。Apache Airflow 限制條件檔案會指定 Apache Airflow 發行時可用的提供者版本。

    從 Apache Airflow 2.7.2 版開始，您的需求檔案必須包含 `--constraint`陳述式。如果您未提供限制條件，Amazon MWAA 會為您指定一個限制條件，以確保您的需求中列出的套件與您正在使用的 Apache Airflow 版本相容。

   在下列範例中，將 *\$1environment-version\$1* 取代為您環境的版本編號，並將 *\$1Python-version\$1* 取代為您環境相容的 Python 版本。

   如需有關與 Apache Airflow 環境相容的 Python 版本的資訊，請參閱 [Apache Airflow 版本](airflow-versions.md#airflow-versions-official)。

   ```
   --constraint "https://raw.githubusercontent.com/apache/airflow/constraints-{Airflow-version}/constraints-{Python-version}.txt"
   ```

   如果限制條件檔案判斷`xyz==1.0`套件與您環境中的其他套件不相容， `pip3 install` 將無法防止不相容的程式庫安裝到您的環境。如果任何套件的安裝失敗，您可以在 CloudWatch Logs 的對應日誌串流中存取每個 Apache Airflow 元件 （排程器、工作者和 Web 伺服器） 的錯誤日誌。如需日誌類型的詳細資訊，請參閱 [在 Amazon CloudWatch 中存取 Airflow 日誌](monitoring-airflow.md)。

1. **Apache Airflow 套件**。新增[套件額外項目](http://airflow.apache.org/docs/apache-airflow/2.5.1/extra-packages-ref.html)和版本 (`==`)。這有助於防止相同名稱但不同版本的套件安裝在您的環境中。

   ```
   apache-airflow[package-extra]==2.5.1
   ```

1. **Python 程式庫**。在 `requirements.txt` 檔案中新增套件名稱和版本 (`==`)。這有助於防止 [PyPi.org](https://pypi.org) 未來的重大更新自動套用。

   ```
   library == version
   ```  
**Example Boto3 和 psycopg2-binary**  

   此範例僅供示範之用。boto 和 psycopg2-binary 程式庫包含在 Apache Airflow v2 基本安裝中，不需要在`requirements.txt`檔案中指定。

   ```
   boto3==1.17.54
   boto==2.49.0
   botocore==1.20.54
   psycopg2-binary==2.8.6
   ```

   如果指定的套件沒有版本，Amazon MWAA 會從 [PyPi.org](https://pypi.org) 安裝最新版本的套件。此版本可能與 中的其他套件衝突`requirements.txt`。

------

## `requirements.txt` 上傳至 Amazon S3
<a name="configuring-dag-dependencies-upload"></a>

您可以使用 Amazon S3 主控台或 AWS Command Line Interface (AWS CLI) 將`requirements.txt`檔案上傳至 Amazon S3 儲存貯體。

### 使用 AWS CLI
<a name="configuring-dag-dependencies-upload-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)。

**使用 上傳 AWS CLI**

1. 使用下列命令列出所有 Amazon S3 儲存貯體。

   ```
   aws s3 ls
   ```

1. 使用下列命令列出您環境的 Amazon S3 儲存貯體中的檔案和資料夾。

   ```
   aws s3 ls s3://YOUR_S3_BUCKET_NAME
   ```

1. 下列命令會將`requirements.txt`檔案上傳至 Amazon S3 儲存貯體。

   ```
   aws s3 cp requirements.txt s3://amzn-s3-demo-bucket/requirements.txt
   ```

### 使用 Amazon S3 主控台
<a name="configuring-dag-dependencies-upload-console"></a>

Amazon S3 主控台是以 Web 為基礎的使用者介面，可用來建立和管理 Amazon S3 儲存貯體中的資源。

**使用 Amazon S3 主控台上傳**

1. 在 Amazon MWAA 主控台上開啟[環境](https://console.aws.amazon.com/mwaa/home#/environments)頁面。

1. 選擇環境。

1. 在 **S3 窗格中的 DAG 程式碼中選取 S3 儲存貯**體連結，以在主控台中開啟您的儲存貯體。 ** S3** 

1. 選擇**上傳**。

1. 選擇**新增檔案**。

1. 選取 的本機副本`requirements.txt`，然後選擇**上傳**。

## 在您的環境上安裝 Python 相依性
<a name="configuring-dag-dependencies-installing"></a>

本節說明如何透過指定 requirements.txt 檔案的路徑，以及在每次更新時指定 requirements.txt 檔案的版本，來安裝您上傳至 Amazon S3 儲存貯體的相依性。

### 在 Amazon MWAA 主控台`requirements.txt`上指定 的路徑 （第一次）
<a name="configuring-dag-dependencies-first"></a>

如果這是您第一次建立 並將其上傳至 Amazon S3 `requirements.txt`儲存貯體，您也需要在 Amazon MWAA 主控台上指定 檔案的路徑。您只需要完成此步驟一次。

1. 在 Amazon MWAA 主控台上開啟[環境](https://console.aws.amazon.com/mwaa/home#/environments)頁面。

1. 選擇環境。

1. 選擇**編輯**。

1. 在 **Amazon S3 窗格中的 DAG 程式碼**上，選擇**需求檔案 - 選用**欄位旁的**瀏覽 S3**。

1. 選取 Amazon S3 儲存貯體上的 `requirements.txt` 檔案。

1. 選擇 **Choose (選擇)**。

1. 選擇**下一步**，**更新環境**。

您可以在環境完成更新之後立即開始使用新套件。

### 在 Amazon MWAA 主控台上指定`requirements.txt`版本
<a name="working-dags-dependencies-mwaaconsole-version"></a>

每次在 Amazon S3 儲存貯`requirements.txt`體中上傳新版本的 時，您需要在 Amazon MWAA 主控台上指定 `requirements.txt` 檔案的版本。

1. 在 Amazon MWAA 主控台上開啟[環境](https://console.aws.amazon.com/mwaa/home#/environments)頁面。

1. 選擇環境。

1. 選擇**編輯**。

1. 在 **Amazon S3 窗格中的 DAG 程式碼**上，在下拉式清單中選擇`requirements.txt`版本。

1. 選擇**下一步**，**更新環境**。

您可以在環境完成更新之後立即開始使用新套件。

## 存取 的日誌 `requirements.txt`
<a name="working-dags-dependencies-logs"></a>

您可以檢視排程器的 Apache Airflow 日誌，以排程工作流程和剖析`dags`資料夾。下列步驟說明如何在 Amazon MWAA 主控台上開啟排程器的日誌群組，以及在 CloudWatch Logs 主控台上存取 Apache Airflow 日誌。

**存取 的日誌 `requirements.txt`**

1. 在 Amazon MWAA 主控台上開啟[環境](https://console.aws.amazon.com/mwaa/home#/environments)頁面。

1. 選擇環境。

1. 在**監控**窗格中選擇 **Airflow 排程器日誌群組**。

1. 在`requirements_install_ip`日誌**串流中選擇日誌**。

1. 請參閱 環境上安裝的套件清單`/usr/local/airflow/.local/bin`。例如：

   ```
   Collecting appdirs==1.4.4 (from -r /usr/local/airflow/.local/bin (line 1))
   Downloading https://files.pythonhosted.org/packages/3b/00/2344469e2084fb28kjdsfiuyweb47389789vxbmnbjhsdgf5463acd6cf5e3db69324/appdirs-1.4.4-py2.py3-none-any.whl  
   Collecting astroid==2.4.2 (from -r /usr/local/airflow/.local/bin (line 2))
   ```

1. 檢閱套件清單，以及是否有任何套件在安裝期間發生錯誤。如果發生錯誤，您會收到類似以下的錯誤：

   ```
   2021-03-05T14:34:42.731-07:00
   No matching distribution found for LibraryName==1.0.0 (from -r /usr/local/airflow/.local/bin (line 4))
   No matching distribution found for LibraryName==1.0.0 (from -r /usr/local/airflow/.local/bin (line 4))
   ```

## 後續步驟？
<a name="working-dags-dependencies-next-up"></a>

在 GitHub 上使用 [aws-mwaa-docker-images](https://github.com/aws/amazon-mwaa-docker-images)，在本機測試您的 DAGs、自訂外掛程式和 Python 相依性。