【问题标题】:Who created an Amazon EC2 instance using Boto and Python?谁使用 Boto 和 Python 创建了 Amazon EC2 实例?
【发布时间】:2015-08-16 07:01:00
【问题描述】:

我想知道是谁创建了一个特定的实例。我正在使用 Cloud Trail 来查找统计信息,但我无法获得有关谁创建了该实例的特定统计信息。我正在使用 Python 和 Boto3 来查找详细信息。 我正在使用此代码-boto3 中 Cloud trail 中的 Lookup events() 来提取有关实例的信息。

ct_conn = sess.client(service_name='cloudtrail',region_name='us-east-1')


events=ct_conn.lookup_events()

【问题讨论】:

  • 您能否下载 CloudTrail 日志、过滤 RunInstances、找到特定的实例 ID,然后从该日志中检索用户身份?
  • 你能提供一个代码sn-p吗?或者如何使用 Boto 来解决这个问题?
  • 请显示您尝试使用的代码,@upaangsaxena。
  • @upaangsaxena 抱歉,我没有使用 boto CloudTrail API。文档很少,我看不到如何使用它的好例子。
  • 您不需要代码就可以从 CloudTrail 中找到它。请导航到 AWS 控制台中的 CloudTrail 并使用日期进行过滤

标签: python amazon-web-services amazon-ec2 boto amazon-cloudtrail


【解决方案1】:

我使用lookup_events()函数找到了上述问题的解决方案。

ct_conn = boto3.client(service_name='cloudtrail',region_name='us-east-1')

events_dict= ct_conn.lookup_events(LookupAttributes=[{'AttributeKey':'ResourceName', 'AttributeValue':'i-xxxxxx'}])
for data in events_dict['Events']:
    json_file= json.loads(data['CloudTrailEvent'])
    print json_file['userIdentity']['userName']

【讨论】:

  • 如果可能的话,您能否提供整个代码,或者您能否在“ct_conn = sess.client(service_name='cloudtrail',region_name= 'us-east-1')"
  • 您可以在 ct_conn 行上方定义会话对象。
【解决方案2】:

@Karthik - 这是创建会话的示例

import boto3
import json
import os

session = boto3.Session(region_name='us-east-1',aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'])

ct_conn = session.client(service_name='cloudtrail',region_name='us-east-1')

events_dict= ct_conn.lookup_events(LookupAttributes=[{'AttributeKey':'ResourceName', 'AttributeValue':'i-xxx'}])

for data in events_dict['Events']:
    json_file= json.loads(data['CloudTrailEvent'])
    print (json_file['userIdentity']['userName'])

【讨论】:

    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 2011-06-27
    • 2015-09-26
    • 2013-08-08
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多