From a java application running on an EC2 instance, I'd like to know what my own elastic IP address that was manually assigned from the management console. Is there a way to query EC2 API for this?
2
- im thinking that there might be something non-idiomatic about your approach here. there probably is a better way to accomplish your end-goal, but im new to EC2 myself, so i cant tell you what the common sdlc patterns are. I assume, however, that what your suggesting (i.e. the need to find out your own ip at runtime) might not be needed except for in certain corner cases.jayunit100– jayunit1002012-04-27 17:38:43 +00:00Commented Apr 27, 2012 at 17:38
- Probably not. You're somewhat hindered by my trying to limit the scope of my explanation to something simple to comprehend. Seems that I have failed though with your response. Basically what I was trying to say is that I'm looking for answer to the exact question and not workarounds because I have very particular reasons to need to do this that I don't want to go into major detail to explain. :)hockey_dave– hockey_dave2012-04-27 21:15:53 +00:00Commented Apr 27, 2012 at 21:15
Add a comment |
3 Answers
If you using a linux ec2 instance this should work:
Command:
curl http://169.254.169.254/latest/meta-data/public-ipv4 Java Code:
public static String getIP() throws IOException, InterruptedException { Process p = Runtime.getRuntime().exec("curl http://169.254.169.254/latest/meta-data/public-ipv4"); int returnCode = p.waitFor(); if ( returnCode == 0 ) { BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); String ip = r.readLine(); r.close(); return ip; } else { //handle error return null; } } 2 Comments
hockey_dave
Thanks, I'm going to work on something similar to this as this seems to be the only viable approach. I was hoping for more of a direct API call but I can wrap the above into a jax-rs call to make it more api-ish. :)
hockey_dave
okay I've implemented this as a jaxrs service and am happy with it. Seems that we're not getting more elegant answers so I'm going to accept yours. Thank you.
Alternative (not clearest solution), might be request an external service like this to know your public IP. http://whatismyip.org/
EDIT: I found a nice service that returns json or text format. https://www.ipify.org/
1 Comment
Mr. Lance E Sloan
It does work, but the response needs to be parsed, of course.
You can call DescribeInstances - it returns a bunch of information including Public IP Address (ip-address filter).
... DescribeInstancesRequest dis = new DescribeInstancesRequest(); DescribeInstancesResult disresult = ec2.describeInstances(dis); ... 7 Comments
hockey_dave
yes, I tried that but it does not tell me which one of them is mine. Think about it from an sdlc standpoint. I have a devel EC2 instance and a production EC2 instance. I want the devel EC2 instance to connect to the devel database and the production instance to point to the production database. I don't want to create a custom application, configuration, etc. to deploy. I'd like to just ask amazon what elastic IP address I am currently on. There are many other reasons why I want this ability. This is just an example.
aviad
@hockey_dave, how do you create dev/test instances?
hockey_dave
You just create an instance and then use it for what you want. EC2 does not have a concept of a dev instance. This is our own internal designation. We add a meaningful tag to our instances for their purpose so that we can easily tell what is what in the AWS console.
aviad
Wait a minute, you run on amazon cloud and you try to reduce the dependence on amazon AWS API? This does not make any sense to me.
vipw
@aviad Try thinking about it from a business perspective. Vendor lock-in is NOT a good thing for a business. Some sort of adaptation layer will be required to port his application from AWS to (for example) OpenStack. If fewer APIs have to be wrapped, the cost and risk of changing providers goes down.
|