So I have a list of public IP addresses and I'd like to see if they are a public IP that is associated with our account. I know that I can simply paste each IP into the search box in the AWS EC2 console. However I would like to automate this process via a Python program.
I'm told anything you can do in the console, you can do via CLI/program, but which function do I use to simply either return a result or not, based on whether it's a public IP that's associated with our account?
I understand I may have to do two searches, one of instances (which would cover non-EIP public IPs) and one of EIPs (which would cover disassociated EIPs that we still have).
But how?
Using boto, the first thing you'll need to do is call connect_to_region in order to connect to the AWS region you're interested in. (If you have instances in multiple regions then you'll need to iterate over each region one by one).
Once you've connected to a region you'll need to call get_only_instances which will return a list of instance objects. Go through the list of those objects and look at the ip_address field for the instances public IP (or the private_ip_address field for the private one).
Then you'll want to call get_all_addresses to get a list of Elastic IP's. Once again you'll need to loop through the list of EIP objects and look at the public_ip field in this case. And if you want to determine which instance the EIP is associated with (if any) then the instance_id field will do that.
Here's the method I have come up with:
To look up all IPs to see if they are EIPs associated with our AWS account
- Get a list of all our EIPs
- Get a list of all instances
- Build list of all public IPs of instances
- Merge lists/use same list
- Check desired IPs against this list.
Comments welcome.