如何确定 AWS 安全组的创建时间戳?

问题描述 投票:0回答:1

我想确定 AWS 安全组的创建时间戳。

这怎么办?

amazon-web-services aws-security-group
1个回答
0
投票

不幸的是,AWS 不直接在标准 EC2

DescribeSecurityGroups
API 响应中提供安全组的创建时间戳。

但是,如果您启用了 Cloud Trail,则可以按照以下步骤操作。

CloudTrail:如果您启用了 AWS CloudTrail,并且存储了创建安全组期间的日志,您可以在这些日志中查询安全组的创建事件。如果在此期间启用了 CloudTrail,此方法对于跟踪历史事件是有效的。

以下是使用适用于 Python 的 AWS 开发工具包 (Boto3) 的代码示例:

import boto3

def find_security_group_creation_date(security_group_id, trail_name):
    # Create CloudTrail client
    client = boto3.client('cloudtrail')

    # Look up events related to the security group in question
    response = client.lookup_events(
        LookupAttributes=[
            {
                'AttributeKey': 'ResourceName',
                'AttributeValue': security_group_id
            },
        ],
        MaxResults=50  # Adjust as necessary. Default is 50, max is 50.
    )

    # Iterate through the events and find the creation event
    for event in response.get('Events', []):
        if event['EventName'] == 'CreateSecurityGroup':
            return event['EventTime']

    return None

# Example usage:
security_group_id = "YOUR_SECURITY_GROUP_ID"
trail_name = "YOUR_CLOUDTRAIL_TRAIL_NAME"
creation_date = find_security_group_creation_date(security_group_id, trail_name)
if creation_date:
    print(f"Security Group {security_group_id} was created on {creation_date}")
else:
    print(f"Could not determine creation date for Security Group {security_group_id}")

YOUR_SECURITY_GROUP_ID
替换为您的安全组 ID,将
YOUR_CLOUDTRAIL_TRAIL_NAME
替换为您的 CloudTrail 跟踪的名称。

请记住,CloudTrail 会在有限的时间内保留事件,除非您已将其配置为将日志传送到 S3 存储桶。根据您的 CloudTrail 设置,如果旧事件已被修剪或从未捕获,则可能无法访问它们。

© www.soinside.com 2019 - 2024. All rights reserved.