Displaying EC2 Instance name using Boto 3

后端 未结 2 1977
醉酒成梦
醉酒成梦 2020-12-31 00:18

I\'m not sure how to display the name of my instance in AWS EC2 using boto3

This is some of the code I have:

import boto3

ec2 = boto3.r         


        
2条回答
  •  时光取名叫无心
    2020-12-31 00:35

    There may be other ways. But from your code point of view, the following should work.

    >>> for i in vpc.instances.all():
    ...   for tag in i.tags:
    ...     if tag['Key'] == 'Name':
    ...       print tag['Value']
    

    One liner solution if you want to use Python's powerful list comprehension:

    inst_names = [tag['Value'] for i in vpc.instances.all() for tag in i.tags if tag['Key'] == 'Name']
    print inst_names
    

提交回复
热议问题