How to obtain the public ip address of the current EC2 instance in python ?
To view the IPv4 addresses for an instance using the consoleOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances and select your instance. The following information is available on the Networking tab: Public IPv4 address — The public IPv4 address.
To allocate and associate an Elastic IP address with your EC2 Windows or Linux instance, follow these steps: Allocate an Elastic IP address from either Amazon's pool of public IPv4 addresses or a custom IP address pool that you bring to your AWS account. Associate the Elastic IP address with a running instance.
import urllib.request
urllib.request.urlopen("http://169.254.169.254/latest/meta-data/public-ipv4").read()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With