user3351750
user3351750

Reputation: 947

get public ip of instances in AWS

I am trying to get the public ip address of all the running instances. I am using boto3 and python version 2.7.6.

>>> instances = ec2.instances.filter(
    ...     Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
>>> for instance in instances:
...     print(instance.public_ip_address,instance.platform,instance.public_dns_name);

It lists all the instances along with instances not having public ip address assigned to it.

(None, None, '')

Is there any way to filter out those instances which do not have a public ip while populating instances using ec2.instances.filter?

Upvotes: 3

Views: 4120

Answers (1)

M. Shaw
M. Shaw

Reputation: 1742

filter(lambda x:x[0] is not None, instances)

Upvotes: 1

Related Questions