Obtaining tags from AWS instances with boto

后端 未结 4 1611
有刺的猬
有刺的猬 2020-12-31 02:19

I\'m trying to obtain tags from instances in my AWS account using Python\'s boto library.

While this snippet works correctly bringing all tags:

    t         


        
4条回答
  •  半阙折子戏
    2020-12-31 03:12

    You have to be sure that the 'Name' tag exists before accessing it. Try this:

    import boto.ec2
    conn=boto.ec2.connect_to_region("eu-west-1")
    reservations = conn.get_all_instances()
    for res in reservations:
        for inst in res.instances:
            if 'Name' in inst.tags:
                print "%s (%s) [%s]" % (inst.tags['Name'], inst.id, inst.state)
            else:
                print "%s [%s]" % (inst.id, inst.state)
    

    will print:

    i-4e444444 [stopped]
    Amazon Linux (i-4e333333) [running]
    

提交回复
热议问题