from urllib.request import urlopen
import json
# modify this to be the number of Workers you have running (in Clusters UI)
numWorkers = 10
worker_instance_ids = sc.parallelize(range(numWorkers)).map(lambda x:
(urlopen('http://169.254.169.254/latest/meta-data/local-hostname').read(),urlopen('http://169.254.169.254/latest/meta-data/local-ipv4').read())
)
print (worker_instance_ids.distinct().collect())
[('ec2-54-191-168-248.us-west-2.compute.amazonaws.com', '54.191.168.248'), ('ec2-54-213-157-27.us-west-2.compute.amazonaws.com', '54.213.157.27')]
Describe My EC2 Spark Instances with static and dynamic EC2 instance metadata