

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

# 為 Amazon ECS 建立 Fargate 暫時性儲存的加密金鑰
<a name="fargate-create-storage-key"></a>

建立客戶自管金鑰，加密儲存於 Fargate 暫時性儲存中的資料。

**注意**  
使用客戶自管金鑰的 Fargate 暫時性儲存加密，不適用於 Windows 任務叢集。  
使用客戶自管金鑰的 Fargate 暫時性儲存加密，不適用於早於 `1.4.0` 的 `platformVersions`。  
Fargate 會在僅供 Fargate 使用的暫時性儲存上保留空間，而且不會向您收取該空間的費用。資源配置能與非客戶自管金鑰的任務不同，但總空間保持不變。您可以在 `df` 等工具中檢視此變更。  
Fargate 暫時性儲存不支援多區域金鑰。  
Fargate 暫時性儲存不支援 KMS 金鑰別名。

若要建立客戶受管金鑰 (CMK) 來加密 Fargate 中的暫時性儲存 AWS KMS，請遵循下列步驟。

1. 導覽至 [https://console.aws.amazon.com/kms](https://console.aws.amazon.com/kms)。

1. 請遵循 [AWS Key Management Service Developer Guide](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 中的 [Creating Keys](https://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html) 的說明操作。

1. 建立 AWS KMS 金鑰時，請務必在金鑰政策中提供 Fargate 服務相關的 AWS KMS 操作許可。若要將客戶自管金鑰與 Amazon ECS 叢集資源搭配使用，必須在金鑰政策中允許下列 API 操作。
   + `kms:GenerateDataKeyWithoutPlainText` ‐ 呼叫 從提供的金鑰`GenerateDataKeyWithoutPlainText`產生加密的資料 AWS KMS 金鑰。
   + `kms:CreateGrant`：新增客戶自管金鑰的授權。授予控制對指定 AWS KMS 金鑰的存取，允許存取 Amazon ECS Fargate 所需的授予操作。如需詳細資訊，請參閱 [AWS Key Management Service Developer Guide](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 中的 [Using Grants](https://docs.aws.amazon.com/kms/latest/developerguide/grants.html)。此授權使 Amazon ECS Fargate 能執行以下操作：
     + 呼叫 `Decrypt` AWS KMS 以取得加密金鑰來解密暫時性儲存資料。
     + 設定淘汰主體，允許服務 `RetireGrant`。
   + `kms:DescribeKey` ‐ 提供客戶自管金鑰詳細資訊，使 Amazon ECS 能驗證金鑰是否對稱且已啟用。

   下列範例顯示您要套用至目標 AWS KMS 金鑰以進行加密的金鑰政策。若要使用下列政策範例，請將*使用者輸入預留位置*取代為您自己的資訊。一如往常，只設定您需要的許可，但您需要為至少一個使用者提供 AWS KMS 許可，以避免錯誤。

   ```
   {
         "Sid": "Allow generate data key access for Fargate tasks.",
         "Effect": "Allow",
         "Principal": { "Service":"fargate.amazonaws.com" },
         "Action": [
           "kms:GenerateDataKeyWithoutPlaintext"
         ],
         "Condition": {
           "StringEquals": {
             "kms:EncryptionContext:aws:ecs:clusterAccount": [
               "customerAccountId"
             ],
             "kms:EncryptionContext:aws:ecs:clusterName": [
                "clusterName"
             ]   
           }
         },
         "Resource": "*"
       },
       {
         "Sid": "Allow grant creation permission for Fargate tasks.",
         "Effect": "Allow",
         "Principal": { "Service":"fargate.amazonaws.com" },
         "Action": [
           "kms:CreateGrant"
         ],
         "Condition": {
           "StringEquals": {
             "kms:EncryptionContext:aws:ecs:clusterAccount": [
               "customerAccountId"
             ],
             "kms:EncryptionContext:aws:ecs:clusterName": [
                "clusterName"
             ]   
           },
          "ForAllValues:StringEquals": {
             "kms:GrantOperations": [
                "Decrypt"
             ]
          }
         },
         "Resource": "*"
       },
       {
         "Sid": "Allow describe key permission for cluster operator - CreateCluster and UpdateCluster.",
         "Effect": "Allow",
         "Principal": { "AWS":"arn:aws:iam::customerAccountId:role/customer-chosen-role" },
         "Action": [
           "kms:DescribeKey"
         ],
         "Resource": "*"
       }
   ```

   Fargate 任務使用 `aws:ecs:clusterAccount` 與 `aws:ecs:clusterName` 加密內容金鑰搭配該金鑰進行密碼編譯操作。客戶應新增這些許可，限制對特定帳戶和/或叢集的存取。在指定叢集時，請使用叢集名稱而非 ARN。

   如需詳細資訊，請參閱 [AWS KMS 開發人員指南](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html)中的[加密內容](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#encrypt_context)。

   建立或更新叢集時，您可以選擇使用條件索引鍵 `fargateEphemeralStorageKmsKeyId`。此條件索引鍵讓客戶可以更精細地控制 IAM 政策。`fargateEphemeralStorageKmsKeyId` 組態的更新僅在新服務部署上生效。

   以下是允許客戶僅將許可授予一組特定核准 AWS KMS 金鑰的範例。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Action": [
           "ecs:CreateCluster",
           "ecs:UpdateCluster"
         ],
         "Resource": "*",
         "Condition": {
           "StringEquals": {
             "ecs:fargate-ephemeral-storage-kms-key": "arn:aws:kms:us-west-2:111122223333:key/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
           }
         }
       }
     ]
   }
   ```

------

   接下來是拒絕嘗試移除已與叢集建立關聯的 AWS KMS 金鑰的範例。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": {
       "Effect": "Deny",
       "Action": [
           "ecs:CreateCluster",
           "ecs:UpdateCluster"
       ],
       "Resource": "*",
       "Condition": {
         "Null": {
           "ecs:fargate-ephemeral-storage-kms-key": "true"
         }
       }
     }
   }
   ```

------

   客戶可以使用 `describe-cluster`、 或 AWS CLI `describe-tasks``describe-services`命令，查看其未受管任務或服務任務是否使用 金鑰加密。

   如需詳細資訊，請參閱 [AWS KMS Developer Guide](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 中的 [Condition keys for AWS KMS](https://docs.aws.amazon.com/kms/latest/developerguide/policy-conditions.html)。

------
#### [ AWS 管理主控台 ]

1. 開啟主控台，網址為 [https://console.aws.amazon.com/ecs/v2](https://console.aws.amazon.com/ecs/v2)。

1. 請於左側導覽中選擇**叢集**，然後點選右上角的**建立叢集**，或選取現有叢集。若採用現有叢集，則選擇右上角的**更新叢集**。

1. 在工作流程的**加密**區段下，您可以選擇在**受管儲存**和 **Fargate 暫時性儲存**下選取 AWS KMS 金鑰。您也可以從這裡選擇**建立 AWS KMS 金鑰**。

1. 完成新叢集的建立後，請選擇**建立**；如果要更新現有叢集，請選擇**更新**。

------
#### [ AWS CLI ]

以下是使用 建立叢集和設定 Fargate 暫時性儲存的範例 AWS CLI （使用您自己的值取代*紅色*值）：

```
aws ecs create-cluster --cluster clusterName \
--configuration '{"managedStorageConfiguration":{"fargateEphemeralStorageKmsKeyId":"arn:aws:kms:us-west-2:012345678901:key/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"}}'
{
    "cluster": {
        "clusterArn": "arn:aws:ecs:us-west-2:012345678901:cluster/clusterName",
        "clusterName": "clusterName",
        "configuration": {
            "managedStorageConfiguration": {
                "fargateEphemeralStorageKmsKeyId": "arn:aws:kms:us-west-2:012345678901:key/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
            }
        },
        "status": "ACTIVE",
        "registeredContainerInstancesCount": 0,
        "runningTasksCount": 0,
        "pendingTasksCount": 0,
        "activeServicesCount": 0,
        "statistics": [],
        "tags": [],
        "settings": [],
        "capacityProviders": [],
        "defaultCapacityProviderStrategy": []
    },
    "clusterCount": 5
}
```

------
#### [ CloudFormation ]

以下是使用 CloudFormation （使用您自己的值取代*紅色*值） 建立叢集和設定 Fargate 暫時性儲存的範例範本：

```
AWSTemplateFormatVersion: 2010-09-09
Resources:
  MyCluster: 
    Type: AWS::ECS::Cluster
    Properties: 
      ClusterName: "clusterName" 
      Configuration:
        ManagedStorageConfiguration:
          FargateEphemeralStorageKmsKeyId: "arn:aws:kms:us-west-2:012345678901:key/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
```

------