

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

# 使用 Amazon EFS 在 EC2 執行個體上建立 Amazon ECS 任務定義並掛載檔案系統
<a name="create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs"></a>

*Durga Prasad Cheepuri，Amazon Web Services*

## 總結
<a name="create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs-summary"></a>

此模式提供程式碼範例和步驟，以建立在 Amazon Web Services (AWS) Cloud 中 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上執行的 Amazon Elastic Container Service (Amazon ECS) 任務定義，同時使用 Amazon Elastic File System (Amazon EFS) 在這些 EC2 執行個體上掛載檔案系統。使用 Amazon EFS 的 Amazon ECS 任務會自動掛載您在任務定義中指定的檔案系統，並將這些檔案系統提供給 AWS 區域中所有可用區域中的任務容器。

為了滿足您的持久性儲存和共用儲存需求，您可以同時使用 Amazon ECS 和 Amazon EFS。例如，您可以使用 Amazon EFS，透過在不同可用區域中執行的作用中和待命 ECS 容器對來存放應用程式的持久性使用者資料和應用程式資料，以獲得高可用性。您也可以使用 Amazon EFS 來存放共用資料，以供 ECS 容器和分散式任務工作負載平行存取。

若要將 Amazon EFS 與 Amazon ECS 搭配使用，您可以將一或多個磁碟區定義新增至任務定義。磁碟區定義包含 Amazon EFS 檔案系統 ID、存取點 ID，以及傳輸中 AWS Identity and Access Management (IAM) 授權或 Transport Layer Security (TLS) 加密的組態。您可以使用任務定義中的容器定義來指定在容器執行時掛載的任務定義磁碟區。當使用 Amazon EFS 檔案系統的任務執行時，Amazon ECS 會確保檔案系統已掛載，並可供需要存取的容器使用。

## 先決條件和限制
<a name="create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 具有虛擬私有網路 (VPN) 端點或路由器的虛擬私有雲端 (VPC)
+ （建議） [Amazon ECS 容器代理程式 1.38.0 或更新版本](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-versions.html)，以與 Amazon EFS 存取點和 IAM 授權功能相容 （如需詳細資訊，請參閱 AWS 部落格文章 [New for Amazon EFS – IAM Authorization and Access Points](https://aws.amazon.com/blogs/aws/new-for-amazon-efs-iam-authorization-and-access-points/)。)

**限制**
+ 1.35.0 之前的 Amazon ECS 容器代理程式版本不支援使用 EC2 啟動類型的任務使用 Amazon EFS 檔案系統。

## Architecture
<a name="create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs-architecture"></a>

下圖顯示使用 Amazon ECS 在 ECS 容器中的 EC2 執行個體上建立任務定義和掛載 Amazon EFS 檔案系統的應用程式範例。

![](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/090a3f03-a4c6-47e3-b1ae-b0eb5c5b269c/images/343e0f1d-44ee-4ec2-8392-aeddc0e48b83.png)


該圖顯示以下工作流程：

1. 建立 Amazon EFS 檔案系統。

1. 使用容器建立任務定義。

1. 設定容器執行個體以掛載 Amazon EFS 檔案系統。任務定義參考磁碟區掛載，因此容器執行個體可以使用 Amazon EFS 檔案系統。ECS 任務可存取相同的 Amazon EFS 檔案系統，無論這些任務是在哪個容器執行個體上建立。

1. 使用任務定義的三個執行個體建立 Amazon ECS 服務。

**技術堆疊**
+ Amazon EC2
+ Amazon ECS
+ Amazon EFS

## 工具
<a name="create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs-tools"></a>
+ [Amazon EC2](https://docs.aws.amazon.com/ec2/?id=docs_gateway) – Amazon Elastic Compute Cloud (Amazon EC2) 在 AWS 雲端中提供可擴展的運算容量。您可以使用 Amazon EC2 視需要啟動任意數量或任意數量的虛擬伺服器，也可以向外擴展或向內擴展。
+ [Amazon ECS](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html) – Amazon Elastic Container Service (Amazon ECS) 是一種高度可擴展的快速容器管理服務，用於執行、停止和管理叢集上的容器。您可以在 AWS Fargate 管理的無伺服器基礎設施上執行任務和服務。或者，若要進一步控制您的基礎設施，您可以在您管理的 EC2 執行個體叢集上執行任務和服務。
+ [Amazon EFS](https://docs.aws.amazon.com/efs/latest/ug/whatisefs.html) – Amazon Elastic File System (Amazon EFS) 提供簡單、可擴展、全受管的彈性 NFS 檔案系統，可與 AWS 雲端服務和內部部署資源搭配使用。
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) – AWS 命令列界面 (AWS CLI) 是一種開放原始碼工具，可透過命令列 shell 中的命令與 AWS 服務互動。透過最少的組態，您可以從命令提示中執行 AWS CLI 命令，該命令會實作相當於瀏覽器型 AWS 管理主控台所提供功能的功能。

## 史詩
<a name="create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs-epics"></a>

### 建立 Amazon EFS 檔案系統
<a name="create-an-amazon-efs-file-system"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 使用 AWS 管理主控台建立 Amazon EFS 檔案系統。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs.html) | AWS DevOps | 

### 使用 Amazon EFS 檔案系統或 AWS CLI 建立 Amazon ECS 任務定義
<a name="create-an-amazon-ecs-task-definition-by-using-either-an-amazon-efs-file-system-or-the-aws-cli"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 使用 Amazon EFS 檔案系統建立任務定義。 | 使用以下組態，使用[新的 Amazon ECS 主控台](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-task-definition.html)或[傳統 Amazon ECS 主控台](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-task-definition-classic.html)建立任務定義：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs.html) | AWS DevOps | 
| 使用 AWS CLI 建立任務定義。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs.html) | AWS DevOps | 

## 相關資源
<a name="create-an-amazon-ecs-task-definition-and-mount-a-file-system-on-ec2-instances-using-amazon-efs-resources"></a>
+ [Amazon ECS 任務定義](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html)
+ [Amazon EFS 磁碟區](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/efs-volumes.html)

## 附件
<a name="attachments-090a3f03-a4c6-47e3-b1ae-b0eb5c5b269c"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/090a3f03-a4c6-47e3-b1ae-b0eb5c5b269c/attachments/attachment.zip)