'Get the public IPv4 address of a newly created amazon ec2 instance with boto3

I am creating a ec2 instance with boto3 and I want print the ip address of that new instance.

ec2 = boto3.resource('ec2')

# create the instance 
new_instance = ec2.create_instances(
    ImageId='###', 
    MinCount = 1, 
    MaxCount = 1, 
    InstanceType = 't2.nano',
    KeyName = "key",
    SecurityGroupIds = ["###"]
)

... wait until running ...
ip = new_instance[0].ipv4 # something like this

Is there a way to do something like this after it is running?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source