

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

# 使用 Firelens 日誌路由器為 Amazon ECS 建立自訂日誌剖析器
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router"></a>

*Varun Sharma，Amazon Web Services*

## 總結
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-summary"></a>

Firelens 是 Amazon Elastic Container Service (Amazon ECS) 和 AWS Fargate 的日誌路由器。您可以使用 Firelens 將容器日誌從 Amazon ECS 路由到 Amazon CloudWatch 和其他目的地 （例如 [Splunk](https://www.splunk.com/) 或 [Sumo Logic](https://www.sumologic.com/))。Firelens 使用 [Fluentd](https://www.fluentd.org/) 或 [Fluent Bit](https://fluentbit.io/) 作為記錄代理程式，這表示您可以使用 [Amazon ECS 任務定義參數](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html)來路由日誌。

透過選擇在來源層級剖析日誌，您可以分析記錄資料並執行查詢，以更有效率且有效地回應操作問題。由於不同的應用程式有不同的記錄模式，因此您需要使用自訂剖析器來建構日誌，並在最終目的地進行搜尋。

此模式使用 Firelens 日誌路由器搭配自訂剖析器，從在 Amazon ECS 上執行的範例 Spring Boot 應用程式將日誌推送至 CloudWatch。然後，您可以使用 Amazon CloudWatch Logs Insights 根據自訂剖析器產生的自訂欄位來篩選日誌。

## 先決條件和限制
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-prereqs"></a>

**先決條件**
+ 作用中的 Amazon Web Services (AWS) 帳戶。
+ AWS Command Line Interface (AWS CLI)，安裝在本機機器上並進行設定。
+ 在本機電腦上安裝和設定 Docker。
+ Amazon Elastic Container Registry (Amazon ECR) 上現有的 Spring Boot 型容器化應用程式。 

## Architecture
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-architecture"></a>

![\[使用 Firelens 日誌路由器，從在 Amazon ECS 上執行的應用程式將日誌推送至 CloudWatch。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/e82b4992-c4e0-4af5-b87e-cb0b1c1ed8c9/images/ef60e087-965a-40e9-9f80-35edbda2befe.png)


**技術堆疊**
+ CloudWatch
+ Amazon ECR
+ Amazon ECS
+ Fargate
+ Docker
+ Fluent Bit

## 工具
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-tools"></a>
+ [Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) – Amazon Elastic Container Registry (Amazon ECR) 是一種 AWS 受管容器映像登錄服務，安全、可擴展且可靠。
+ [Amazon ECS](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html) – Amazon Elastic Container Service (Amazon ECS) 是一種高度可擴展的快速容器管理服務，可讓您輕鬆地執行、停止和管理叢集上的容器。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) – IAM 是一種 Web 服務，可安全地控制對 AWS 服務的存取。
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) – AWS Command Line Interface (AWS CLI) 是一種開放原始碼工具，可讓您使用命令列 shell 中的命令與 AWS 服務互動。
+ [Docker](https://www.docker.com/) – Docker 是開發、運送和執行應用程式的開放平台。

**Code**

下列檔案會附加至此模式：
+ `customFluentBit.zip` – 包含要新增自訂剖析和組態的檔案。
+ `firelens_policy.json` – 包含用來建立 IAM 政策的政策文件。
+ `Task.json` – 包含 Amazon ECS 的範例任務定義。

## 史詩
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-epics"></a>

### 建立自訂 Fluent Bit 映像
<a name="create-a-custom-fluent-bit-image"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 Amazon ECR 儲存庫。 | 登入 AWS 管理主控台，開啟 Amazon ECR 主控台，並建立名為 的儲存庫`fluentbit_custom`。如需詳細資訊，請參閱 [Amazon ECR 文件中的建立儲存庫](https://docs.aws.amazon.com/AmazonECR/latest/userguide/repository-create.html)。 | 系統管理員、開發人員 | 
| 解壓縮 customFluentBit.zip 套件。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html) |  | 
| 建立自訂 Docker 映像。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html)如需詳細資訊，請參閱 Amazon ECR 文件中的[推送 Docker 映像](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html)。  | 系統管理員、開發人員 | 

### 設定 Amazon ECS 叢集
<a name="set-up-the-amazon-ecs-cluster"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 Amazon ECS 叢集 | 遵循 Amazon ECS 文件中建立叢集的*僅限聯網範本*[一節中的指示，建立 Amazon ECS 叢集](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create_cluster.html)。請務必選擇**建立 VPC**，為您的 Amazon ECS 叢集建立新的虛擬私有雲端 (VPC)。 | 系統管理員、開發人員 | 

### 設定 Amazon ECS 任務
<a name="set-up-the-amazon-ecs-task"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
|  設定 Amazon ECS 任務執行 IAM 角色。 | 使用 `AmazonECSTaskExecutionRolePolicy`受管政策建立 Amazon ECS 任務執行 IAM 角色。如需詳細資訊，請參閱 [Amazon ECS 文件中的 Amazon ECS 任務執行 IAM 角色](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html)。請務必記錄 IAM 角色的 Amazon Resource Name (ARN)。 | 系統管理員、開發人員 | 
|  將 IAM 政策連接至 Amazon ECS 任務執行 IAM 角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html) | 系統管理員、開發人員 | 
| 設定 Amazon ECS 任務定義。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html)如需詳細資訊，請參閱 Amazon ECS 文件中的[建立任務定義](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-task-definition.html)。 | 系統管理員、開發人員 | 

### 執行 Amazon ECS 任務
<a name="run-the-amazon-ecs-task"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行 Amazon ECS 任務。 | 在 Amazon ECS 主控台上，選擇**叢集**，選擇您先前建立的叢集，然後執行獨立任務。如需詳細資訊，請參閱 Amazon ECS 文件中的[執行獨立任務](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_run_task.html)。 | 系統管理員、開發人員 | 

### 驗證 CloudWatch 日誌
<a name="verify-the-cloudwatch-logs"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證日誌。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html) | 系統管理員、開發人員 | 

## 相關資源
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-resources"></a>
+ [Amazon ECS 的 Docker 基本概念](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-basics.html) 
+ [AWS Fargate 上的 Amazon ECS](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/AWS_Fargate.html) 
+ [設定基本服務參數](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/basic-service-params.html) 

## 附件
<a name="attachments-e82b4992-c4e0-4af5-b87e-cb0b1c1ed8c9"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/e82b4992-c4e0-4af5-b87e-cb0b1c1ed8c9/attachments/attachment.zip)