簡體   English   中英

誰使用Boto和Python創建了Amazon EC2實例?

[英]Who created an Amazon EC2 instance using Boto and Python?

我想知道誰創建了一個特定的實例。 我正在使用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()

我使用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']

@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'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM