Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (19.1k points)

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.resource('ec2', region_name='us-west-2')

vpc = ec2.Vpc("vpc-21c15555")

for i in vpc.instances.all():


What I get in return is






I can change i to be or i.instance_type but when I try name I get:

AttributeError: 'ec2.Instance' object has no attribute 'name'

What is the correct way to get the instance name?

1 Answer

0 votes
by (44.4k points)

According to your code, this should work:

>>> for i in vpc.instances.all():

...    for tag in i.tags:

...      if tag['Key'] == 'Name':

...        print tag['Value']

Using Python's list comprehension tool:

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

print inst_names

Learn more about Elastic Compute Cloud on AWS EC2.

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

29.3k questions

30.6k answers


104k users

Browse Categories