

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

# 使用 Terraform 在 Amazon Managed Grafana 上自動化 Amazon MWAA 自訂指標的擷取和視覺化
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics"></a>

*Faisal Abdullah 和 Satya Vajrapu，Amazon Web Services*

## 總結
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-summary"></a>

此模式討論如何使用 Amazon Managed Grafana 建立和監控由 Amazon Managed Workflows for Apache Airflow (Amazon MWAA) 擷取的自訂指標。Amazon MWAA 可做為工作流程的協調器，採用以 Python 編寫指令碼的定向無環圖形 (DAGs)。此模式著重於監控自訂指標，包括過去一小時內執行DAGs 總數、每小時傳遞和失敗DAGs 計數，以及這些程序的平均持續時間。此分析顯示 Amazon Managed Grafana 如何與 Amazon MWAA 整合，以全面監控和洞察此環境中工作流程的協調。

## 先決條件和限制
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶 ，具有建立和管理下列項目的必要使用者許可 AWS 服務：
  + AWS Identity and Access Management (IAM) 角色和政策
  + AWS Lambda
  + Amazon Managed Grafana
  + Amazon Managed Workflows for Apache Airflow (Amazon MWAA)
  + Amazon Simple Storage Service (Amazon S3)
  + Amazon Timestream
+ 存取 Shell 環境，該環境可以是本機機器或 上的終端機[AWS CloudShell](https://docs.aws.amazon.com/cloudshell/latest/userguide/welcome.html)。
+ 安裝 Git 並安裝和設定最新版本 AWS Command Line Interface (AWS CLI) 的 shell 環境。如需詳細資訊，請參閱 AWS CLI 文件中的[安裝或更新至最新版本的 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。
+ 已安裝下列 Terraform 版本：`required_version = ">= 1.6.1, < 2.0.0"`您可以使用 [tfswitch](https://tfswitch.warrensbox.com/) 在不同的 Terraform 版本之間切換。
+ 在 中 AWS IAM Identity Center 為您的 設定身分來源 AWS 帳戶。如需詳細資訊，請參閱 [IAM Identity Center 文件中的在 IAM Identity Center 中確認您的身分來源](https://docs.aws.amazon.com/singlesignon/latest/userguide/prereq-identity-sources.html)。您可以選擇預設值 IAM Identity Center 目錄、Active Directory 或外部身分提供者 (IdP)，例如 Okta。如需詳細資訊，請參閱[相關資源](#automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-resources)。

**限制**
+ 有些 AWS 服務 完全無法使用 AWS 區域。如需區域可用性，請參閱[AWS 服務 依區域](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然後選擇服務的連結。

**產品版本**
+ Terraform `required_version = ">= 1.6.1, < 2.0.0"`
+ Amazon Managed Grafana 9.4 版或更新版本。此模式已在 9.4 版上測試。

## Architecture
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-architecture"></a>

下列架構圖會反白顯示解決方案 AWS 服務 中使用的 。

![自動化 Amazon MWAA 自訂指標擷取的工作流程。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/3458d0a9-aee1-428a-bf2f-c357bb531c64/images/b43ed8d2-94ac-4438-913b-81c7eba8f3e0.png)


上述圖表會逐步執行下列工作流程：

1. Amazon MWAA 內的自訂指標來自環境中執行DAGs。指標會以 CSV 檔案格式上傳至 Amazon S3 儲存貯體。下列 DAGs 使用 Amazon MWAA 的資料庫查詢功能：
   + `run-example-dag` – 此 DAG 包含定義一或多個任務的範例 Python 程式碼。它會每 7 分鐘執行一次，並列印日期。列印日期之後，DAG 會包含要在特定期間內休眠或暫停執行的任務。
   + `other-sample-dag` – 此 DAG 每 10 分鐘執行一次，並列印日期。列印日期之後，DAG 會包含要在特定期間內休眠或暫停執行的任務。
   + `data-extract` – 此 DAG 每小時執行一次，並查詢 Amazon MWAA 資料庫並收集指標。收集指標之後，此 DAG 會將它們寫入 Amazon S3 儲存貯體，以供進一步處理和分析。

1. 為了簡化資料處理，Lambda 函數會在 Amazon S3 事件觸發時執行，這有助於將指標載入 Timestream。

1. Timestream 整合為 Amazon Managed Grafana 內的資料來源，其中存放了來自 Amazon MWAA 的所有自訂指標。

1. 使用者可以查詢資料並建構自訂儀表板，以視覺化關鍵效能指標，並深入了解 Amazon MWAA 內工作流程的協調。

## 工具
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-tools"></a>

**AWS 服務**
+ [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html) 可協助您集中管理所有 AWS 帳戶 和雲端應用程式的單一登入 (SSO) 存取。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。在此模式中， 會 AWS Lambda 執行 Python 程式碼以回應 Amazon S3 事件，並自動管理運算資源。
+ [Amazon Managed Grafana](https://docs.aws.amazon.com/grafana/latest/userguide/what-is-Amazon-Managed-Service-Grafana.html) 是一項全受管資料視覺化服務，可用來查詢、關聯和視覺化指標、日誌和追蹤，並發出警示。此模式使用 Amazon Managed Grafana 來建立指標視覺化和提醒的儀表板。
+ [Amazon Managed Workflows for Apache Airflow (Amazon MWAA)](https://docs.aws.amazon.com/mwaa/latest/userguide/what-is-mwaa.html) 是 Apache Airflow 的受管協同運作服務，可用來大規模設定和操作雲端中的資料管道。[Apache Airflow](https://airflow.apache.org/) 是一種開放原始碼工具，用於以程式設計方式撰寫、排程和監控稱為工作流程的程序和任務序列。在此模式中，範例 DAGs和指標擷取器 DAG 會部署在 Amazon MWAA 中。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。在此模式中，Amazon S3 用於儲存 CSV 格式DAGs、指令碼和自訂指標。
+ [Amazon Timestream for LiveAnalytics](https://docs.aws.amazon.com/timestream/latest/developerguide/what-is-timestream.html) 是一種快速、可擴展、全受管的專用時間序列資料庫，可讓您輕鬆地每天存放和分析數兆個時間序列資料點。適用於 LiveAnalytics 的 Timestream 也與資料收集、視覺化和機器學習的常用服務整合。在此模式中，它會用來擷取產生的 Amazon MWAA 自訂指標。

**其他工具**
+ [HashiCorp Terraform](https://www.terraform.io/docs) 是一種基礎設施即程式碼 (IaC) 工具，可協助您使用程式碼來佈建和管理雲端基礎設施和資源。此模式使用 Terraform 模組自動佈建 中的基礎設施 AWS。

**程式碼儲存庫**

此模式的程式碼可在 GitHub 的 [visualize-amazon-mwaa-custom-metrics-grafana](https://github.com/aws-samples/visualize-amazon-mwaa-custom-metrics-grafana) 儲存庫中取得。`stacks/Infra` 資料夾包含下列項目：
+ 所有 AWS 資源的 Terraform 組態檔案
+ `grafana` 資料夾中的 Grafana 儀表板 .json 檔案
+ `mwaa/dags` 資料夾中的 Amazon Managed Workflows for Apache Airflow DAGs 
+ Lambda 程式碼可剖析 .csv 檔案，並將指標存放在 `src` 資料夾中的 Timestream 資料庫
+ `templates` 資料夾中的 IAM 政策 .json 檔案

## 最佳實務
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-best-practices"></a>

Terraform 必須儲存受管基礎設施和組態的狀態，以便將實際資源映射到您的組態。根據預設，Terraform 會在本機將狀態儲存在名為 的檔案中`terraform.tfstate`。請務必確保 Terraform 狀態檔案的安全性和完整性，因為它會維護基礎設施的目前狀態。如需詳細資訊，請參閱 Terraform 文件中的[遠端狀態](https://developer.hashicorp.com/terraform/language/state/remote)。

## 史詩
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-epics"></a>

### 使用 Terraform 部署基礎設施
<a name="deploy-the-infrastructure-using-terraform"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 基礎設施。 | 若要部署解決方案基礎設施，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html) | AWS DevOps | 

### 驗證部署的基礎設施資源
<a name="validate-the-deployed-infrastructure-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證 Amazon MWAA 環境。 | 若要驗證 Amazon MWAA 環境，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html) | AWS DevOps，資料工程師 | 
| 驗證 DAG 排程。 | 若要檢視每個 DAG 排程，請前往 **Airflow UI **中的**排程**索引標籤。<br />下列每個 DAGs都有預先設定的排程，在 Amazon MWAA 環境中執行並產生自訂指標：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html)<br />您也可以在執行欄下看到每個 DAG **的成功執行**。 | AWS DevOps 資料工程師 | 

### 設定 Amazon Managed Grafana 環境
<a name="configure-the-gra-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定對 Amazon Managed Grafana 工作區的存取。 | Terraform 指令碼建立了所需的 Amazon Managed Grafana 工作區、儀表板和指標頁面。若要設定存取權以便您可以檢視，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html) | AWS DevOps | 
| 安裝 Amazon Timestream 外掛程式。 | Amazon MWAA 自訂指標會載入 Timestream 資料庫。您可以使用 Timestream 外掛程式，透過 Amazon Managed Grafana 儀表板視覺化指標。<br />若要安裝 Timestream 外掛程式，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html)<br />如需詳細資訊，請參閱《Amazon Managed Grafana 文件》中的[使用外掛程式擴展工作區](https://docs.aws.amazon.com/grafana/latest/userguide/grafana-plugins.html#manage-plugins)。 | AWS DevOps，DevOps 工程師 | 

### 在 Amazon Managed Grafana 儀表板中視覺化自訂指標
<a name="visualize-the-custom-metrics-in-the-gra-dashboard"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 檢視 Amazon Managed Grafana 儀表板。 | 若要檢視擷取至 Amazon Managed Grafana 工作區的指標，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html)<br />儀表板指標頁面會顯示下列資訊：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html) | AWS DevOps | 
| 自訂 Amazon Managed Grafana 儀表板。 | 若要自訂儀表板以供日後進一步增強功能，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html)<br />或者，此儀表板的原始碼可在 [GitHub 儲存庫](https://github.com/aws-samples/visualize-amazon-mwaa-custom-metrics-grafana/blob/main/stacks/infra/grafana/dashboard.json)的 `stacks/infra/grafana` 資料夾中的 `dashboard.json` 檔案中取得。 | AWS DevOps | 

### 清除 AWS 資源
<a name="clean-up-aws-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 暫停 Amazon MWAA DAG 執行。 | 若要暫停 DAG 執行，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html) | AWS DevOps，資料工程師 | 
| 刪除 Amazon S3 儲存貯體中的物件。 | 若要刪除 Amazon S3 儲存貯體 **mwaa-events-bucket-\*** 和 **mwaa-metrics-bucket-\***，請遵循 Amazon S3 文件中刪除[儲存貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/delete-bucket.html)體中的 Amazon S3 主控台使用指示。 | AWS DevOps | 
| 銷毀 Terraform 建立的資源。 | 若要銷毀 Terraform 建立的資源和相關聯的本機 Terraform 狀態檔案，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics.html) | AWS DevOps | 

## 疑難排解
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| `null_resource.plugin_mgmt (local-exec): aws: error: argument operation: Invalid choice, valid choices are:` | 將 升級至 AWS CLI [最新版本](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。 | 
| 載入資料來源錯誤 - <br />`Fetch error: 404 Not Found Instantiating…` | 錯誤是間歇性的。等待幾分鐘，然後重新整理資料來源以檢視列出的 Timestream 資料來源。 | 

## 相關資源
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-resources"></a>

**AWS 文件**
+ [用於儀表板和視覺化的 Amazon Managed Grafana](https://docs.aws.amazon.com/prescriptive-guidance/latest/implementing-logging-monitoring-cloudwatch/amg-dashboarding-visualization.html)
+ [設定 Amazon Managed Grafana 使用 Okta](https://docs.aws.amazon.com/grafana/latest/userguide/AMG-SAML-providers-okta.html)
+ [AWS IAM Identity Center 搭配 Amazon Managed Grafana 工作區使用](https://docs.aws.amazon.com/grafana/latest/userguide/authentication-in-AMG-SSO.html)
+ [在 Amazon MWAA 上使用 DAG](https://docs.aws.amazon.com/mwaa/latest/userguide/working-dags.html)

**AWS 影片**
+ 使用 Amazon Managed Grafana 設定 IAM Identity Center 進行身分驗證，如下列[影片](https://www.youtube.com/watch?v=XX2Xcz-Ps9U)所示。




[https://www.youtube-nocookie.com/embed/XX2Xcz-Ps9U?controls=0](https://www.youtube-nocookie.com/embed/XX2Xcz-Ps9U?controls=0)
+ 如果無法使用 IAM Identity Center，您也可以使用 Okta 等外部身分提供者 (IdP) 整合 Amazon Managed Grafana 身分驗證，如下列[影片](https://www.youtube.com/watch?v=Z4JHxl2xpOg)所示。




[https://www.youtube-nocookie.com/embed/Z4JHxl2xpOg?controls=0](https://www.youtube-nocookie.com/embed/Z4JHxl2xpOg?controls=0)

## 其他資訊
<a name="automate-ingestion-and-visualization-of-amazon-mwaa-custom-metrics-additional"></a>

您可以為您的 Amazon MWAA 環境建立全面的監控和提醒解決方案，以主動管理和快速回應潛在問題或異常。Amazon Managed Grafana 包含下列功能：

**警示** – 您可以根據預先定義的閾值或條件，在 Amazon Managed Grafana 中設定警示。設定電子郵件通知，在特定指標超過或低於指定閾值時提醒相關利益相關者。如需詳細資訊，請參閱 Amazon Managed [Grafana 文件中的 Grafana 提醒](https://docs.aws.amazon.com/grafana/latest/userguide/alerts-overview.html)。

**整合** – 您可以將 Amazon Managed Grafana 與 OpsGenie、PagerDuty 或 Slack 等各種第三方工具整合，以增強通知功能。例如，您可以設定 Webhook 或與 APIs整合，以根據 Amazon Managed Grafana 中產生的警示觸發這些平台中的事件和通知。此外，此模式提供 [GitHub 儲存庫](https://github.com/aws-samples/visualize-amazon-mwaa-custom-metrics-grafana)來建立 AWS 資源。您可以進一步將此程式碼與您的基礎設施部署工作流程整合。