跳至內容

如何解決當我嘗試建立 AWS 資源時,收到 CloudWatch 錯誤「Unable to enable logging.Policy document length breaking CloudWatch Logs Constraints」(無法啟用記錄。政策文件長度超過 CloudWatch Logs 限制)?

2 分的閱讀內容
0

當我嘗試建立 AWS 資源時,收到 Amazon CloudWatch Logs 錯誤「Unable to enable logging.Policy document length breaking CloudWatch Logs Constraints」(無法啟用記錄。政策文件長度超過 CloudWatch Logs 限制)。

簡短說明

當您為 AWS 服務啟用 CloudWatch Logs 或建立新的 AWS 資源時,可能會收到下列錯誤訊息:

「Unable to enable logging.Policy document length breaking CloudWatch Logs Constraints」(無法啟用記錄。政策文件長度超過 CloudWatch Logs 限制)

CloudWatch 資源政策的最大允許字元長度為 5120。當您的政策超過 5120 個字元時,就會收到上述錯誤。您可以在 AWS CloudTrail 事件歷史記錄中檢閱錯誤詳細資訊。

解決方法

**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本

您只能使用 AWS CLI 來檢查 CloudWatch 資源政策。

若要檢查目前資源政策並進行修改,請完成以下步驟:

  1. 執行 describe-resource-policies AWS CLI 命令來檢查您目前的 CloudWatch 資源政策:

    aws logs describe-resource-policies --REGION-NAME > resource.json

    **注意:**將 REGION-NAME 替換為您的 AWS 區域。

  2. 將輸出儲存到名為 resource.json 的 JSON 檔案。

  3. resource.json 複製 AWSLogDeliveryWrite20150319 政策,然後將該政策貼到文字編輯器中。

  4. 若要減少政策中的字元數,請執行下列動作:
    移除您不使用的項目,例如資源和日誌串流。
    將資源 Amazon Resource Name (ARN) 替換為萬用字元 *
    附加具有所需權限的新資源政策。

    資源政策範例:

    {
    "Version": "2012-10-17",
    "Statement": [
    { "Sid": "AWSLogDeliveryWrite",
    "Effect": "Allow",
    "Principal": {
    "Service": "delivery.logs.amazonaws.com"
    },
    "Action": [
    "logs:CreateLogStream", "logs:PutLogEvents"
    ],
    "Resource":
    [ "arn:aws:logs:us-east-1:869614222995:log-group:aws-waf-logs-albwafs:log-stream:xyz",
    "arn:aws:logs:us-east-1:869614222995:log-group:aws-s3:log-stream:abc"
    
    ],
    "Condition":
    { "StringEquals":
    { "aws:SourceAccount": "869614222995" },
    "ArnLike": { "aws:SourceArn": "arn:aws:logs:us-east-1:869614222995:*"
    }
     }
    }
    ]
    }
    

    **注意:**此範例顯示包含兩個資源的政策。當資源政策超過 5120 個字元的限制時,CloudWatch 會自動將項目 /aws/vendedlogs/* 新增到政策中。然後,您必須建立帶有首碼 /aws/vendedlogs/ 的新日誌群組。如果您想使用自訂日誌群組名稱,請編輯政策,以加入您的特定資源名稱。請勿超過字元限制。

  5. 執行 put-resource-policy AWS CLI 指令,將 AWSLogDeliveryWrite20150319 資源政策替換為 resource.json 中編輯的版本:

    aws logs put-resource-policy --policy-document file://resource.json --POLICY-NAME  AWSLogDeliveryWrite20150319

    **注意:**將 POLICY-NAME 替換為您要取代資源政策的名稱。

新增資源政策時,您可能會收到下列與 CloudWatch 配額相關的錯誤訊息:

「錯誤:建立 CloudWatch Logs 資源政策 (名稱): 作業錯誤 CloudWatch Logs: PutResourcePolicy,超過嘗試次數上限,https 回應錯誤 StatusCode: 400,RequestID: 3d123ce1-f123-4d12-12b8-abc1234ba1a9,LimitExceededException: 超過資源限制。」

您的 AWS 帳戶中每個區域最多可以有 10 個 CloudWatch Logs 資源政策。您無法變更此配額。若要解決此問題,請執行 delete-resource-policy AWS CLI 命令以移除現有政策:

aws logs delete-resource-policy --policy-name POLICY-NAME

**注意:**將 POLICY-NAME 替換為您要刪除資源政策的名稱。

當區域中的資源政策少於 10 個時,您可以建立新的資源政策。

相關資訊

使用主控台檢視最近的管理事件

CloudWatch Logs 配額

啟用 AWS 服務的記錄