所以我有我的python脚本,其中列出了关于每个ec2实例的报告。我有一个对实例的网络附加时间进行排序的函数,但每次我使用stftime来格式化日期的输出时,似乎会混乱排序并以随机顺序列出实例,而不是最古老的CreationDateTime。我得到的输出如下:
i-09dc54328002240ff,Aug 05 2021,asg-workxxx
i-048e92c5a4741d2b1,Mar 09 2017,False
i-0d649cebdf54bd2f4,Mar 12 2020,asg-dyyyyy
i-0ff596f1dc01b61d8,Mar 17 2021,asg-base-test
i-06db4eb158ad0b071,May 12 2021,False
i-0f285277529543462,May 18 2018,False
i-0f67cf99fb9f96c3f,Oct 14 2020,asg-elk-test
i-01734dfef0159c5c8,Oct 20 2020,asg-lb-test
i-0539c8dfc839cbfda,Oct 26 2020,asg-stand-base-test
您可以看到CreationDateTime并不是按顺序排序的。
我的代码如下:response = ec2_client.describe_instances(
MaxResults=10
)
# return json data from describe instances and filter what is needed
instances = (len(response['Reservations']))
header_row = 'InstanceID, CreationDateTime, AutoScalingGroupName' + 'n'
for x in range(instances):
# Get InstanceId
instance_id = (response['Reservations'][x]
['Instances'][0]['InstanceId'])
# Get NetworkInterfacws AttatchTime
network_interface_id = (
response['Reservations'][x]['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId'])
network_interface_details = ec2_client.describe_network_interfaces(
NetworkInterfaceIds=[network_interface_id])
networkinterface_id_attachedtime = network_interface_details[
'NetworkInterfaces'][0]['Attachment']['AttachTime']
time_between_insertion = datetime.now(
timezone.utc) - networkinterface_id_attachedtime
# Get Autoscaling GroupName
tags = (response['Reservations'][x]['Instances'][0]['Tags'])
autoscaling_group_name = get_tag(tags, 'aws:autoscaling:groupName')
# print results
if time_between_insertion.days > max_age:
line = '{},{},{}'.format(
instance_id, formatted_date_networkinterface_id, autoscaling_group_name)
instances_list.append(line)
sorted_list= sorted(instances_list, key=lambda v: v.split(',')[1])
for instance in sorted_list:
print(instance) ```
要解析日期,可以使用datetime.datetime.strptime
:
import datetime # strptime
import operator # itemgetter
data_unparsed = ['i-09dc54328002240ff,Aug 05 2021,asg-workxxx',
'i-048e92c5a4741d2b1,Mar 09 2017,False',
'i-0d649cebdf54bd2f4,Mar 12 2020,asg-dyyyyy',
'i-0ff596f1dc01b61d8,Mar 17 2021,asg-base-test',
'i-06db4eb158ad0b071,May 12 2021,False',
'i-0f285277529543462,May 18 2018,False',
'i-0f67cf99fb9f96c3f,Oct 14 2020,asg-elk-test',
'i-01734dfef0159c5c8,Oct 20 2020,asg-lb-test',
'i-0539c8dfc839cbfda,Oct 26 2020,asg-stand-base-test']
data = [((row := s.split(','))[0], datetime.datetime.strptime(row[1], '%b %d %Y'), row[2]) for s in data_unparsed]
data_sorted = sorted(data, key=operator.itemgetter(1))
print(data_sorted)
# [('i-048e92c5a4741d2b1', datetime.datetime(2017, 3, 9, 0, 0), 'False'),
# ('i-0f285277529543462', datetime.datetime(2018, 5, 18, 0, 0), 'False'),
# ('i-0d649cebdf54bd2f4', datetime.datetime(2020, 3, 12, 0, 0), 'asg-dyyyyy'),
# ('i-0f67cf99fb9f96c3f', datetime.datetime(2020, 10, 14, 0, 0), 'asg-elk-test'),
# ('i-01734dfef0159c5c8', datetime.datetime(2020, 10, 20, 0, 0), 'asg-lb-test'),
# ('i-0539c8dfc839cbfda', datetime.datetime(2020, 10, 26, 0, 0), 'asg-stand-base-test'),
# ('i-0ff596f1dc01b61d8', datetime.datetime(2021, 3, 17, 0, 0), 'asg-base-test'),
# ('i-06db4eb158ad0b071', datetime.datetime(2021, 5, 12, 0, 0), 'False'),
# ('i-09dc54328002240ff', datetime.datetime(2021, 8, 5, 0, 0), 'asg-workxxx')]
或者,作为一行代码:
data_sorted = sorted(data_unparsed, key=lambda s: datetime.datetime.strptime(s.split(',')[1], '%b %d %Y'))
print(data_sorted)
# ['i-048e92c5a4741d2b1,Mar 09 2017,False',
# 'i-0f285277529543462,May 18 2018,False',
# 'i-0d649cebdf54bd2f4,Mar 12 2020,asg-dyyyyy',
# 'i-0f67cf99fb9f96c3f,Oct 14 2020,asg-elk-test',
# 'i-01734dfef0159c5c8,Oct 20 2020,asg-lb-test',
# 'i-0539c8dfc839cbfda,Oct 26 2020,asg-stand-base-test',
# 'i-0ff596f1dc01b61d8,Mar 17 2021,asg-base-test',
# 'i-06db4eb158ad0b071,May 12 2021,False',
# 'i-09dc54328002240ff,Aug 05 2021,asg-workxxx']
相关文档:
datetime.strptime(date_string, format)
;strftime
和strptime
格式码。