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 bucket(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 存储桶位置。
前面的表语句使用从 /2021/1/1/ 到 /2121/12/31/ 的分区投影对 Athena 表进行分区。
**注意:**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
**注意:**当您比较同月和同一 AWS 区域的 Athena 查询输出和 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
相关信息
相关内容
- AWS 官方已更新 1 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 6 个月前