如何疑難排解 Image Builder 中的加密 AMI 分發錯誤?

3 分的閱讀內容
0

當我嘗試將加密的 AMI 分佈到 EC2 Image Builder 中的另一個帳戶時,我收到錯誤訊息。如何對此問題進行疑難排解?

簡短描述

當您將加密的 Amazon Machine Image (AMI) 分佈到另一個帳戶時,下列情況可能會導致 EC2 映像產生器中發生分佈錯誤:

解決方案

使用 Amazon EBS 的預設 AWS 受管金鑰加密對分佈的 AMI 進行加密

您會收到以下錯誤:

Distribution failed with JobId 'XXXXXXXXXXXXXX', status = 'Failed' for ARN 'arn:aws:imagebuilder:us-east-1:xxxxxxxxxxxx:image/test-recipe/0.0.1/1'. 'Not all distribution jobs are completed. 1) EC2 Client Error: 'Snapshots encrypted with the AWS Managed CMK can’t be shared. Specify another snapshot.' when distributing the image from the source account (ID: xxxxxxxxxxxx) to the destination account (ID: xxxxxxxxxxxx) in Region us-east-1.'

您無法共用使用預設 AWS KMS key 加密的 AMI。如需詳細資訊,請參閱與特定 AWS 帳戶共用 AMI

要檢查的案例包括:

  • AWS 受管 KMS 金鑰會在配方的儲存組態中指定。
  • AWS 受管 KMS 金鑰與一或多個目標帳戶一起在分佈組態中指定。
  • 上層 AMI 使用 AWS 受管 KMS 金鑰加密。
  • 上層 AMI 有多個快照,而且至少有一個使用 AWS 受管 KMS 金鑰加密。
  • 您的 AWS 區域已啟用預設為加密,而且使用 AWS 受管 KMS 金鑰。

若要解決此問題,請建立映像配方的新版本,並在配方的儲存組態中指定用於加密的客戶受管 KMS 金鑰。對於分佈組態中的 KMS 金鑰,請在將 AMI 分佈至其他帳戶時,指定用於加密的客戶管理 KMS 金鑰。

AWS KMS 或 IAM 實體沒有必要的許可

您可以使用 launchPermissionstargetAccountIds 組態,在映像產生器中分佈 AMI。

launchPermissions

當您使用 launchPermissions 分佈 AMI 時,映像產生器會在來源帳戶中使用 IAM 角色 AWSServiceRoleForImageBuilder。根據預設,AWSServiceRoleForImageBuilder 對來源帳戶中的資源具有必要的 AWS KMS 許可。

KMS 金鑰政策具有允許根使用者執行 "kms:*" 動作的陳述式。如果此陳述式不在金鑰政策中,則服務連結角色無法存取來源帳戶中的金鑰。如果根使用者不允許執行 "kms:*" 動作,請修改政策以允許服務連結角色使用金鑰。

例如:

{
  "Sid": "Enable IAM User Permissions",
  "Effect": "Allow",
  "Principal": {
    "AWS": "arn:aws:iam::source_account_id:role/aws-service-role/imagebuilder.amazonaws.com/AWSServiceRoleForImageBuilder"
  },
  "Action": [
    "kms:Decrypt",
    "kms:Encrypt",
    "kms:GenerateDataKeyWithoutPlaintext",
    "kms:DescribeKey",
    "kms:CreateGrant",
    "kms:ReEncryptFrom",
    "kms:ReEncryptTo"
  ],
  "Resource": "*"
}

注意:source_account_id 替換為您的來源帳戶 ID。

targetAccountIds

如果目的地帳戶沒有 IAM 角色 EC2ImageBuilderDistributionCrossAccountRole,或來源帳戶未列在信任政策中,則您會收到下列錯誤:

Distribution failed with JobId 'xxxxxxxxxxxxxx', status = 'Failed' for ARN 'arn:aws:imagebuilder:us-east-1:XXXXXXXXXX:image/testdistribution/2.0.0/3'. 'Not all distribution jobs are completed. 1) STS Client Error: 'User: arn:aws:sts::XXXXXXXXXXXX:assumed-role/AWSServiceRoleForImageBuilder/Ec2ImageBuilderIntegrationService is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::xxxxxxxxx:role/EC2ImageBuilderDistributionCrossAccountRole'. Please make sure your 'EC2ImageBuilderDistributionCrossAccountRole' is setup with correct permission policies. If you are copying AMI to opt-in regions, please make sure the region is enabled in the account when distributing the image from the source account (ID: XXXXXXXXXXXX) to the destination account (ID: XXXXXXXXXXXX) in Region us-east-1.'STS Client Error User is not authorized to perform: sts:AssumeRole on resource.

若要解決此問題,請建立 EC2ImageBuilderDistributionCrossAccountRole 角色。接著,連接 Ec2ImageBuilderCrossAccountDistributionAccess 政策以允許跨帳戶分發。然後,在 EC2ImageBuilderDistributionCrossAccountRole 信任政策中列出AWSServiceRoleForImageBuilder

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com",
        "AWS": "arn:aws:iam::XXXXXXXXXX:root"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

由於跨帳戶 AWS KMS 許可問題,您可能會收到以下錯誤:

Distribution failed with JobId 'xxxxxxxxxxxxxx', status = 'Failed' for ARN 'arn:aws:imagebuilder:ap-southeast-2:11111111111:image/test/1.0.0/1'. 'Not all distribution jobs are completed. 1) AMI Copy Reported Failure For 'ami-0047623fbcxxxxx' when distributing the image from the source account (ID: 11111111111) to the destination account (ID: 222222222222) in Region ap-southeast-2.'

當您使用 targetAccountIds 分佈 AMI 時,Image Builder 會在來源帳戶中使用角色 AWSServiceRoleForImageBuilder。在目標帳戶中,則會使用 EC2ImageBuilderDistributionCrossAccountRole 角色。請確定您已授予 EC2ImageBuilderDistributionCrossAccountRole 許可,以便在分佈組態和配方的儲存組態中使用 AWS KMS key。

例如:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "VisualEditor0",
      "Effect": "Allow",
      "Action": [
        "kms:Decrypt",
        "kms:Encrypt",
        "kms:GenerateDataKeyWithoutPlaintext",
        "kms:DescribeKey",
        "kms:CreateGrant",
        "kms:ReEncryptFrom",
        "kms:ReEncryptTo"
      ],
      "Resource": "*"
    }
  ]
}

**注意:**您也可以透過指定 AWS KMS key 的 ARN 來限制 Resource (資源) 區段。

如果客戶受管 KMS 金鑰屬於目的地帳戶,則您必須與來源帳戶共用 AWS KMS key。如果客戶受管 KMS 金鑰屬於來源帳戶,則您必須與目的地帳戶共用 AWS KMS key。

請完成下列步驟以共用 AWS KMS key:

1.    登入 KMS 金鑰所在的帳戶。

2.    在同一個 AWS 區域中開啟 AWS KMS 主控台

3.    在左側導覽窗格中,選擇 Customer managed keys (客戶管理的金鑰)。

4.    選取 KMS key ID (KMS 金鑰識別碼)。

5.    選擇 Key Policy (金鑰政策) 索引標籤。

6.    在 Other AWS accounts (其他 AWS 帳戶) 區段中,選擇 Add other AWS accounts (新增其他 AWS 帳戶)。

7.    指定您要與其共用 KMS 金鑰之帳戶的 ID。

8.    選擇 Save Changes (儲存變更)。


相關資訊

使用映像產生器設定跨帳戶 AMI 分佈

如何在多個 AWS 帳戶之間共用我的 AWS KMS key?

AWS 官方
AWS 官方已更新 2 年前