这是我想了解的一小段python代码 它连接到Amazon ec2实例并打印值。
import boto3
ec2 = boto3.client('ec2')
filters =[{'Name':'tag:Name','Values':['instance_one','instance_two']}]
reservations=ec2.describe_instances(Filters=filters)
print (reservations)
print ('#####\n')
reservations=ec2.describe_instances()
print (reservations)
输出为
{'Reservations': [], 'ResponseMetadata': {'RequestId': '037a7801-700e-4ada-8044-b65f0126bbdb', 'HTTPStatusCode': 200, 'HTTPHeaders': {'content-type': 'text/xml;charset=UTF-8', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'date': 'Sun, 19 Aug 2018 08:56:01 GMT', 'server': 'AmazonEC2'}, 'RetryAttempts': 0}}
#####
{'Reservations': [{'Groups': [], 'Instances': [{'AmiLaunchIndex': 0, 'ImageId': 'ami-d783a9b8', 'InstanceId': 'i-03e7f6391a0f523ee', 'InstanceType': 't2.micro', 'KeyName': 'datastructutre key', 'LaunchTime': datetime.datetime(2018, 8, 19, 2, 17, 8, tzinfo=tzutc()), 'Monitoring': {'State': 'disabled'}, 'Placement': {'AvailabilityZone': 'ap-south-1a', 'GroupName': '', 'Tenancy': 'default'}, 'PrivateDnsName': 'ip-172-31-20-16.ap-south-1.compute.internal', 'PrivateIpAddress': '172.31.20.16', 'ProductCodes': [], 'PublicDnsName': '', 'State': {'Code': 80, 'Name': 'stopped'}, 'StateTransitionReason': 'User initiated (2018-08-19 02:34:57 GMT)', 'SubnetId': 'subnet-d3fdbabb', 'VpcId': 'vpc-08356c60', 'Architecture': 'x86_64', 'BlockDeviceMappings': [{'DeviceName': '/dev/xvda', 'Ebs': {'AttachTime': datetime.datetime(2018, 8, 1, 4, 28, 52, tzinfo=tzutc()), 'DeleteOnTermination': True, 'Status': 'attached', 'VolumeId': 'vol-09716d3308f44c63f'}}], 'ClientToken': '', 'EbsOptimized': False, 'EnaSupport': True, 'Hypervisor': 'xen', 'NetworkInterfaces': [{'Attachment': {'AttachTime': datetime.datetime(2018, 8, 1, 4, 28, 52, tzinfo=tzutc()), 'AttachmentId': 'eni-attach-08d060230b617ca70', 'DeleteOnTermination': True, 'DeviceIndex': 0, 'Status': 'attached'}, 'Description': '', 'Groups': [{'GroupName': 'launch-wizard-1', 'GroupId': 'sg-0e81c2a33e1039f58'}], 'Ipv6Addresses': [], 'MacAddress': '02:5a:17:52:69:a6', 'NetworkInterfaceId': 'eni-0146aab6d9503bf47', 'OwnerId': '1xxxxxxxxx', 'PrivateDnsName': 'ip-172-31-20-16.ap-south-1.compute.internal', 'PrivateIpAddress': '172.31.20.16', 'PrivateIpAddresses': [{'Primary': True, 'PrivateDnsName': 'ip-172-31-20-16.ap-south-1.compute.internal', 'PrivateIpAddress': '172.31.20.16'}], 'SourceDestCheck': True, 'Status': 'in-use', 'SubnetId': 'subnet-d3fdbabb', 'VpcId': 'vpc-08356c60'}], 'RootDeviceName': '/dev/xvda', 'RootDeviceType': 'ebs', 'SecurityGroups': [{'GroupName': 'launch-wizard-1', 'GroupId': 'sg-0e81c2a33e1039f58'}], 'SourceDestCheck': True, 'StateReason': {'Code': 'Client.UserInitiatedShutdown', 'Message': 'Client.UserInitiatedShutdown: User initiated shutdown'}, 'VirtualizationType': 'hvm', 'CpuOptions': {'CoreCount': 1, 'ThreadsPerCore': 1}}], 'OwnerId': '12xxxxxxxxxx', 'ReservationId': 'r-057axxxxxxxxxxx'}], 'ResponseMetadata': {'RequestId': '2ee752bf-4f25-4c9e-8da2-553f7ae72d96', 'HTTPStatusCode': 200, 'HTTPHeaders': {'content-type': 'text/xml;charset=UTF-8', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'date': 'Sun, 19 Aug 2018 08:56:01 GMT', 'server': 'AmazonEC2'}, 'RetryAttempts': 0}}
我听不懂
reservations=ec2.describe_instances(Filters=filters)
我已经在使用过滤器后不使用过滤器的情况下放置了打印语句。在使用过滤器时,第一个打印语句打印一个空白的Reservations []列表,而第二个打印语句ResponseMetadata标签后跟value的是长长的保留列表。
reservations=ec2.describe_instances(Filters=filters)
语句中Filters = filters做什么?
为什么使用它?
答案 0 :(得分:2)
这些行:
filters =[{'Name':'tag:Name','Values':['instance_one','instance_two']}]
reservations=ec2.describe_instances(Filters=filters)
说:“仅返回名为Name
的标签具有instance_one
或instance_two
值的实例”
答案 1 :(得分:0)
请阅读Boto3的文档-> EC2.Client.describe_instances https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.describe_instances
Filter 参数将为您提供放置在 Filter 参数列表中的实例的唯一请求的详细信息。
根据该文档
名称(字符串)- 过滤器的名称。过滤器名称区分大小写。
值(列表)- 一个或多个过滤器值。过滤器值区分大小写。
和
tag:key = value -分配给资源的标签的键/值组合。在过滤器名称中指定标签的键,并在过滤器值中指定标签的值。例如,对于标记目的= X,请指定tag:目的作为过滤器名称,并指定X作为过滤器值。
如果没有参数传递给describe_instances。实例的所有参数将被打印。