跳至內容

如何疑難排解 CloudWatch Logs 中失敗的匯出任務?

4 分的閱讀內容
0

我嘗試將日誌資料從 Amazon CloudWatch Logs 匯出至 Amazon Simple Storage Service (Amazon S3) 儲存貯體時,匯出任務仍會失敗,即使該 Amazon S3 儲存貯體已存在。

簡短說明

您可以為同一 AWS 帳戶或不同帳戶中的 S3 儲存貯體設定匯出任務。

若要將日誌資料從 CloudWatch Logs 匯出至儲存貯體,您必須為目的地儲存貯體設定正確的權限,且該儲存貯體必須與日誌群組位於相同的 AWS 區域。

如果未正確設定儲存貯體政策、AWS Identity and Access Management (IAM) 權限或加密設定,匯出任務就會失敗,且您可能會收到以下其中一個錯誤:

  • 「Could not create export task.GetBucketAcl call on the given bucket failed.Please check if CloudWatch Logs has been granted permission to perform this operation.」
  • 「Could not create export task.PutObject call on the given bucket failed.Please check if CloudWatch Logs has been granted permission to perform this operation.」
  • 「Unable to create export task.Resource limit exceeded.」
  • 「PutObject call on the given bucket failed.Please check if CloudWatch Logs has been granted permission to perform this operation.This may be a result of a KMS key or bucket encryption misconfiguration.」
  • 「GetBucketAcl call on the given bucket failed.Please check if the specified Amazon S3 Bucket is in the same AWS region as CloudWatch Logs.」

解決方法

新增匯出任務所需的權限政策

若要執行匯出任務,呼叫匯出任務 API 的 IAM 使用者、群組或角色必須具備 s3:PutObject 權限政策。如果目的地儲存貯體使用 SSE-KMS 加密,另請納入 AWS Key Management Service (KMS) 政策。

將以下權限政策新增至您的 IAM 使用者、群組或角色:

{
  "Version": "2012-10-17",
  "Statement": [
      {
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": arn:aws:s3:::MY-EXPORTED-LOGS/*
    },
    {
      "Effect": "Allow",
      "Action": [
      "kms:GenerateDataKey",
      "kms:Decrypt"
       ],
    "Resource": ARN_OF_KMS_KEY
    }
  ]
}

注意:MY-EXPORTED-LOGS 替換為目的地儲存貯體名稱,並將 ARN_OF_KMS_KEY 替換為 AWS KMS key 的 Amazon Resource Name (ARN)。

AmazonS3ReadOnlyAccess 受管政策附加至呼叫匯出任務的 IAM 角色。另請附加包含以下 CloudWatch Logs 權限的政策:

  • logs:CreateExportTask
  • logs:CancelExportTask
  • logs:DescribeExportTasks
  • logs:DescribeLogStreams
  • logs:DescribeLogGroups

授與目的地儲存貯體權限

根據預設,所有儲存貯體和物件都是私有的。只有資源擁有者或建立儲存貯體的帳戶,才能存取該儲存貯體及其中的物件。必要時,資源擁有者可以撰寫存取政策,將存取權限授與其他資源和使用者。

設定政策時,請使用隨機產生的字串作為儲存貯體的前綴。如此便只會將指定的日誌串流匯出至儲存貯體。

若要授與目的地儲存貯體權限,請依照以下步驟操作:

  1. 開啟 S3 console (S3 主控台)。
  2. 選擇您要將日誌匯出至其中的儲存貯體。
  3. 選擇 Permissions (權限),然後選擇 Bucket policy (儲存貯體政策)。
  4. Bucket Policy Editor (儲存貯體政策編輯器) 中新增以下政策:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": "s3:GetBucketAcl",
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::exammple-s3-bucket",
          "Principal": {
            "Service": "logs.example-region.amazonaws.com"
          },
          "Condition": {
            "StringEquals": {
              "aws:SourceAccount": [
                "AccountId1",
                "AccountId2",
                ...
              ]
            },
            "ArnLike": {
              "aws:SourceArn": [
                "arn:aws:logs:Region:AccountId1:log-group:",
                "arn:aws:logs:Region:AccountId2:log-group:",
                ...
              ]
            }
          }
        },
        {
          "Action": "s3:PutObject",
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::example-s3-bucket/",
          "Principal": {
            "Service": "logs.example-region.amazonaws.com"
          },
          "Condition": {
            "StringEquals": {
              "s3:x-amz-acl": "bucket-owner-full-control",
              "aws:SourceAccount": [
                "AccountId1",
                "AccountId2",
                ...
              ]
            },
            "ArnLike": {
              "aws:SourceArn": [
                "arn:aws:logs:Region:AccountId1:log-group:",
                "arn:aws:logs:Region:AccountId2:log-group:",
                ...
              ]
            }
          }
        },
        {
          "Effect": "Allow",
          "Principal": {
            "AWS": "arn:aws:iam::create_export_task_caller_account:role/role_name"
          },
          "Action": "s3:PutObject",
          "Resource": "arn:aws:s3:::example-s3-bucket/",
          "Condition": {
            "StringEquals": {
              "s3:x-amz-acl": "bucket-owner-full-control"
            }
          }
        }
      ]
    }
    注意:example-s3-bucket 替換為您的儲存貯體名稱。另將 example-region 替換為 Principal 參數所需的區域端點。系統會將上述政策設為儲存貯體的存取政策。此政策允許 CloudWatch Logs 將日誌資料匯出至您的儲存貯體,並授與儲存貯體擁有者對所有匯出物件的完整權限。
  5. 選擇 Save (儲存)。

將任務匯出至使用 SSE-KMS 加密的儲存貯體

建立並設定 AWS KMS key

請依照以下步驟操作:

  1. 開啟 AWS KMS console (AWS KMS 主控台)。若要變更區域,請使用導覽列上的 Region (區域) 選取器。
  2. 在導覽窗格中,選擇 Customer managed keys (客戶自管金鑰),然後選擇 Create Key (建立金鑰)。
    Key type (金鑰類型) 中,選擇 Symmetric (對稱)。
    Key usage (金鑰用途) 中,選擇 Encrypt and decrypt (加密和解密)。
  3. 選擇 Next (下一步)。
  4. Add labels (新增標籤) 下,輸入金鑰別名。您也可以新增描述或標籤,然後選擇 Next (下一步)。
  5. Key administrators (金鑰管理員) 下,選取可以管理此金鑰的人員,然後選擇 Next (下一步) 兩次,直接前往檢閱頁面。
  6. 檢閱設定,然後選擇 Finish (完成)。
  7. 前往 Customer managed keys (客戶自管金鑰) 頁面,然後選擇您先前建立的金鑰名稱。
  8. 選擇 Key policy (金鑰政策) 索引標籤,然後選擇 Switch to policy view (切換至政策檢視)。
  9. Key policy (金鑰政策) 區段中,選擇 Edit (編輯),然後將以下陳述式新增至金鑰政策陳述式清單:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "Allow CWL Service Principal usage",
          "Effect": "Allow",
          "Principal": {
            "Service": "logs.EXAMPLE-REGION.amazonaws.com"
          },
          "Action": [
            "kms:GenerateDataKey",
            "kms:Decrypt"
          ],
          "Resource": ""
        },
        {
          "Sid": "Enable IAM User Permissions",
          "Effect": "Allow",
          "Principal": {
            "AWS": EXAMPLE-ACCOUNT-ARN
          },
          "Action": [
            "kms:GetKeyPolicy",
            "kms:PutKeyPolicy",
            "kms:DescribeKey",
            "kms:CreateAlias",
            "kms:ScheduleKeyDeletion",
            "kms:Decrypt"
          ],
          "Resource": "*"
        },
        {
          "Sid": "Enable IAM Role Permissions",
          "Effect": "Allow",
          "Principal": {
            "AWS": "arn:aws:iam::create_export_task_caller_account:role/role_name"
          },
          "Action": [
            "kms:GenerateDataKey",
            "kms:Decrypt"
          ],
          "Resource": ARN_OF_KMS_KEY
        }
      ]
    }
    注意:EXAMPLE-REGION 替換為日誌所在的區域,並將 EXAMPLE-ACCOUNT-ARN 替換為 AWS KMS key 擁有帳戶的 ARN。將 create_export_task_caller_account 替換為呼叫者的帳戶 ID,並將 role_name 替換為 IAM 角色名稱。另將 ARN_OF_KMS_KEY 替換為 AWS KMS key 的 ARN。
  10. 選擇 Save changes (儲存變更)。

設定儲存貯體加密

請依照以下步驟操作:

  1. 開啟 Amazon S3 console (Amazon S3 主控台)。
  2. 選擇您要將日誌匯出至其中的儲存貯體。
  3. 選擇 Properties (屬性) 索引標籤。接著,在 Default Encryption (預設加密) 下選擇 Edit (編輯)。
  4. Server-side Encryption (伺服器端加密) 下,選擇 Enable (啟用)。
  5. Encryption type (加密類型) 下,選擇 AWS Key Management Service key (SSE-KMS) (AWS Key Management Service 金鑰 (SSE-KMS))。
  6. 選擇 Choose from your AWS KMS keys (從您的 AWS KMS 金鑰中選擇),然後尋找您建立的金鑰。
  7. Bucket key (儲存貯體金鑰) 中,選擇 Enable (啟用)。
  8. 選擇 Save changes (儲存變更)。
    注意: CloudWatch Logs 允許每個帳戶在每個區域一次只能執行一個匯出任務。如果您收到「Unable to create export task.Resource limit exceeded」錯誤,請等待目前的匯出任務完成後,再啟動新的匯出任務。

相關資訊

使用主控台將日誌資料匯出至 Amazon S3

AWS 官方已更新 6 個月前