Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
如何擷取 AWS Config 每月記錄的組態項目數量?
我想尋找 AWS Config 記錄的組態項目 (CI) 的數量,以便了解 AWS 帳戶的計費。
解決方法
使用 Amazon Athena 確定您帳戶每月的 CI 數量。
檢查您的 Amazon S3 儲存貯體是否包含組態檔案
確保 AWS Config 可以將組態歷史記錄檔案傳送到您指定的 Amazon Simple Storage Service (Amazon S3) 儲存貯體。AWS Config 通常每 6 小時傳送一次組態歷史記錄檔案到儲存貯體。
若要檢查您的 S3 儲存貯體是否包含組態檔案,請完成以下步驟:
- 開啟 AWS Config 主控台。
- 在導覽窗格中,選擇 Settings (設定)。
- 在 Amazon S3 儲存貯體 區段中,請注意儲存貯體名稱。
- 開啟 Amazon S3 主控台,然後選取您的 S3 儲存貯體。
- 檢查 S3 儲存貯體是否包含組態檔案。
注意: 如果沒有組態檔案,則您的 AWS Identity and Access Management (IAM) 角色可能沒有 Amazon S3 所需的權限。
在 Athena 中建立一個資料表
使用 Amazon Athena 查詢編輯器建立資料表。在查詢編輯器中,輸入以下陳述式:
CREATE EXTERNAL TABLE awsconfig ( fileversion string, configSnapshotId string, configurationitems ARRAY < STRUCT < configurationItemVersion : STRING, configurationItemCaptureTime : STRING, configurationStateId : BIGINT, awsAccountId : STRING, configurationItemStatus : STRING, resourceType : STRING, resourceId : STRING, resourceName : STRING, ARN : STRING, awsRegion : STRING, availabilityZone : STRING, configurationStateMd5Hash : STRING, resourceCreationTime : STRING > > ) ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe' LOCATION 's3://BUCKET-NAME/AWSLogs/ACCOUNT-ID/Config/REGION/';
注意: 將範例 S3 儲存貯體位置替換為您的 S3 儲存貯體位置。
如果您使用 Athena 引擎版本 2,則文字檔案的行長度上限為 100 MB。如果您擁有大量資源,則儲存在指定 AWS Config S3 儲存貯體中的 CI 可能會超出配額。例如,由於 AWS Config 也會在相同的儲存貯體位置提供組態快照檔案,因此組態快照檔案可能會超出配額。
如果超出配額,則查詢 CI 後您會收到類似以下內容的錯誤:
「HIVE_BAD_DATA: 文字檔案中的行太長:s3\path\to\config\data\object」
若要防止此問題,請執行下表陳述式,以便 Athena 直接查詢儲存組態歷史記錄檔案的 S3 路徑:
CREATE EXTERNAL TABLE awsconfig ( fileversion string, configSnapshotId string, configurationitems ARRAY < STRUCT < configurationItemVersion : STRING, configurationItemCaptureTime : STRING, configurationStateId : BIGINT, awsAccountId : STRING, configurationItemStatus : STRING, resourceType : STRING, resourceId : STRING, resourceName : STRING, ARN : STRING, awsRegion : STRING, availabilityZone : STRING, configurationStateMd5Hash : STRING, resourceCreationTime : STRING > > ) PARTITIONED BY (`year` string,`month` string,`day` string) ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe' LOCATION 's3://BUCKET-NAME/AWSLogs/ACCOUNT-ID/Config/REGION/' TBLPROPERTIES ( 'projection.enabled'='true', 'projection.year.interval'='1', 'projection.year.range'='2021,2121', 'projection.year.type'='integer', 'projection.month.interval'='1', 'projection.month.range'='1,12', 'projection.month.type'='integer', 'projection.day.interval'='1', 'projection.day.range'='1,31', 'projection.day.type'='integer', 'storage.location.template'='s3://BUCKET-NAME/AWSLogs/ACCOUNT-ID/Config/REGION/${year}/${month}/${day}/ConfigHistory/')
注意: 將範例 S3 儲存貯體位置替換為您的 S3 儲存貯體位置。
上述資料表陳述式會對 Athena 表進行分區,分區投影從 /2021/1/1/ 到 /2121/12/31/。
注意: AWS Config 資料 S3 路徑日期格式與 Athena 分區投影日期類型格式不相容。
Athena 查詢範例
以下查詢範例會擷取 2021 年 2 月的每日 CI 數量:
SELECT result.configurationitemcapturetime, count(result.configurationitemcapturetime) AS NumberOfChanges FROM (SELECT regexp_replace(configurationItem.configurationItemCaptureTime, '(.+)(T.+)', '$1') AS configurationitemcapturetime FROM default.awsconfig CROSS JOIN UNNEST(configurationitems) AS t(configurationItem) WHERE "$path" LIKE '%ConfigHistory%' AND configurationItem.configurationItemCaptureTime >= '2021-02-01T%' AND configurationItem.configurationItemCaptureTime <= '2021-02-28T%') result GROUP BY result.configurationitemcapturetime ORDER BY result.configurationitemcapturetime
範例輸出結果:
configurationitemcapturetime NumberOfChanges 2021-02-02 7 2021-02-03 3 2021-02-07 11 ...
以下查詢範例會擷取 2021 年 2 月每個資源的變更數目,並依最常變更的項目進行排序:
SELECT configurationItem.resourceType, configurationItem.resourceId, COUNT(configurationItem.resourceId) AS NumberOfChanges FROM default.awsconfig CROSS JOIN UNNEST(configurationitems) AS t(configurationItem) WHERE "$path" LIKE '%ConfigHistory%' AND configurationItem.configurationItemCaptureTime >= '2021-02-01T%' AND configurationItem.configurationItemCaptureTime <= '2021-02-28T%' GROUP BY configurationItem.resourceType, configurationItem.resourceId ORDER BY NumberOfChanges DESC
範例輸出結果:
resourcetype resourceid NumberOfChanges AWS::EC2::VPC vpc-9ed00bfa 7 AWS::EC2::Subnet subnet-4472e248 5 AWS::EC2::SecurityGroup sg-450c6531 4
注意: 當您比較 Athena 查詢輸出與相同月份和 AWS 區域的 AWS 計費資料之間的 CI 總數時,可能會出現差異。Athena 查詢的資料可能會超出單日範圍,並且也納入相鄰月份內計費的 CI。AWS Config 是根據啟動 configurationItemCaptureTime 的時間來測量 CI。
最佳做法是將結束日期從每月的最後一天增加一天。
例如,在以下查詢中,結束日期是 2 月 28 日:
AND configurationItem.configurationItemCaptureTime <= '2021-02-28T%') result
將結束日期更新為 3 月 1 日:
AND configurationItem.configurationItemCaptureTime <= '2021-03-01T%') result
在 Control Tower 環境中查詢 AWS Config
由於 Control Tower 將所有組態日誌置於同一個 S3 儲存貯體中,因此您可以查詢 AWS Organizations 中的所有帳戶。
若要為 Control Tower 設定建立資料表,請執行類似以下內容的陳述式:
CREATE EXTERNAL TABLE awsconfig ( fileversion string, configSnapshotId string, configurationitems ARRAY < STRUCT < configurationItemVersion : STRING, configurationItemCaptureTime : STRING, configurationStateId : BIGINT, awsAccountId : STRING, configurationItemStatus : STRING, resourceType : STRING, resourceId : STRING, resourceName : STRING, ARN : STRING, awsRegion : STRING, availabilityZone : STRING, configurationStateMd5Hash : STRING, resourceCreationTime : STRING > > ) PARTITIONED BY (`account` string,`region` string,`year` string,`month` string,`day` string) ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe' LOCATION 's3://bucket-name/org-id/AWSLogs/' TBLPROPERTIES ( 'projection.enabled'='true', 'projection.account.type'='enum', 'projection.account.values'='account-id1, accountid2', 'projection.region.type'='enum', 'projection.region.values'='us-east-1,us-east-2,us-west-2', 'projection.year.interval'='1', 'projection.year.range'='2021,2121', 'projection.year.type'='integer', 'projection.month.interval'='1', 'projection.month.range'='1,12', 'projection.month.type'='integer', 'projection.day.interval'='1', 'projection.day.range'='1,31', 'projection.day.type'='integer', 'storage.location.template'='s3://bucket-name/org-id/AWSLogs/${account}/Config/${region}/${year}/${month}/${day}/ConfigHistory/')
注意: 新增您組織中每個帳戶的 ID,並將所有區域替換為您的區域。
上述陳述式會自動建立帳戶、年、月、日的分區。若要查詢特定月份的 CI 數量,請執行以下陳述式:
SELECT result.configurationitemcapturetime, count(result.configurationitemcapturetime) AS NumberOfChanges FROM (SELECT regexp_replace(configurationItem.configurationItemCaptureTime, '(.+)(T.+)', '$1') AS configurationitemcapturetime FROM default.awsconfig CROSS JOIN UNNEST(configurationitems) AS t(configurationItem) WHERE "$path" LIKE '%ConfigHistory%' AND year='2024' AND month='9') result GROUP BY result.configurationitemcapturetime ORDER BY result.configurationitemcapturetime
若要取得同一個月內每個資源的變更次數,請執行以下陳述式:
SELECT configurationItem.resourceType, configurationItem.resourceId, COUNT(configurationItem.resourceId) AS NumberOfChanges FROM default.awsconfig CROSS JOIN UNNEST(configurationitems) AS t(configurationItem) WHERE "$path" LIKE '%ConfigHistory%' AND year='2024' AND month='9' GROUP BY configurationItem.resourceType, configurationItem.resourceId ORDER BY NumberOfChanges DESC
相關資訊
相關內容
- 已提問 5 個月前lg...
- 已提問 5 個月前lg...
- 已提問 9 個月前lg...
- AWS 官方已更新 2 年前
- AWS 官方已更新 3 個月前