Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用boto3的实例标记信息:获取无对象_Python_Python 2.7_Amazon Web Services_Amazon Ec2_Boto3 - Fatal编程技术网

Python 使用boto3的实例标记信息:获取无对象

Python 使用boto3的实例标记信息:获取无对象,python,python-2.7,amazon-web-services,amazon-ec2,boto3,Python,Python 2.7,Amazon Web Services,Amazon Ec2,Boto3,我的python代码如下,同样需要帮助。谢谢 import boto3 import yaml def get_all_running_instances(unique_name): """ Returns instance data for given instance id. :param unique_name: :return: """ count = 1

我的python代码如下,同样需要帮助。谢谢

    import boto3
    import yaml

    def get_all_running_instances(unique_name):
        """
        Returns instance data for given instance id.
        :param unique_name:
        :return:
        """
        count = 1
        rname = unique_name.rsplit('-', 1)[0] + '-' + str(count)
        print rname
        ec2 = boto3.resource('ec2')
        instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running', 'pending']}])
        for instance in instances:
          print(instance.id, instance.instance_type)
        for instance in instances:
          for tag in instance.tags:
                if rname in tag['Value'] and "Name" in tag['Key']:
                    count = count + 1
                    rname = rname.rsplit('-', 1)[0] + '-' + str(count)
        return count
    x = get_all_running_instances('test-1')
    print x

输出如下,无法获取与每个实例关联的标记信息。 我正在对作为此函数一部分返回的标记信息运行Lambda

    ('i-08987804493yyyyyy82', 't2.medium')
    ('i-0e96754xxxxxxxxx', 't2.small')
    Traceback (most recent call last):
      File "dummy.py", line 30, in <module>
        x = get_all_running_instances('test-1')
      File "dummy.py", line 23, in get_all_running_instances
        for tag in instance.tags:
    **TypeError**: 'NoneType' object is not iterable
('i-08987804493yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
('i-0e96754xxxxxxxxx','t2.small')
回溯(最近一次呼叫最后一次):
文件“dummy.py”,第30行,在
x=获取所有正在运行的实例(“测试-1”)
文件“dummy.py”,第23行,在get_all_running_实例中
对于instance.tags中的标记:
**TypeError**:“非类型”对象不可编辑

假设所有实例至少有一个标记。在您的情况下,有些实例没有标记,这会导致代码失败。相反,您可以在迭代
标记之前检查标记是否存在

for instance in instances:
  if instance.tags:
    for tag in instance.tags:
上面的代码将忽略没有定义标记的实例。您可以修改代码以满足您的需要。要验证这一点,您还可以在打印
实例id
实例类型
时打印标记

for instance in instances:
  print(instance.id, instance.instance_type, instance.tags)

非常感谢你,你好。这就成功了。