带有状态的 EC2 的 AWS boto3 配置服务列表

AWS boto3 Config Service list of EC2s with states

我想使用 Boto3 生成 EC2 列表以及一组两个日期时间之间的状态变化(待处理、运行、关闭、终止等)。我的理解是,即使 EC2 不再存在,配置服务也会维护 EC2 的历史记录。我查看了 this 文档,但是我很难理解要使用哪些函数来完成手头的任务。

谢谢

假设您已经配置 AWS Config 规则 来跟踪 ec2-instance 状态,此方法将满足您的需要。

1) 使用 list_discovered_resources API.Ensure includeDeletedResources 获取 ec2-instances 列表设置为 True 如果您想在响应中包含已删除的资源。

response = client.list_discovered_resources(
    resourceType='AWS::EC2::Instance',
    limit=100,
    includeDeletedResources=True,
    nextToken='string'
)

解析响应并存储 resource-id

2) 将每个 resource_id 传递给 get_resource_config_history API.

response = client.get_resource_config_history(
    resourceType='AWS::EC2::Instance',
    resourceId='i-0123af12345be162h5',      // Enter your EC2 instance id here
    laterTime=datetime(2018, 1, 7),         // Enter end date. default is current date.
    earlierTime=datetime(2018, 1, 1),       // Enter start date
    chronologicalOrder='Reverse'|'Forward',
    limit=100,
    nextToken='string'
)

您可以解析响应并获取状态更改,其中 ec2 实例在相应时间段内经历过。