What's the fastest way, in terms of execution time, for a Java JVM to determine if it's running on an AWS EC2 instance?
My current method is to do an http fetch to http://169.254.169.254/latest/instance-id
, but that takes several seconds to time out when I'm not running on AWS.
Update:
My solution was to look at the Java system property "user.name" and see if it contained "ec2". We always run our app under the "ec2-user" user name so this method is fast and reliable.
To check if you have any active resources in your account, do the following: Open the AWS Billing and Cost Management console. In the navigation pane, on the left side of the screen, choose Bills. The Details section shows all the charges incurred by various AWS services on your account.
To view status checks using the Amazon EC2 console, perform the following steps. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances. On the Instances page, the Status check column lists the operational status of each instance.
AWS provides several tools for working with Java and Elastic Beanstalk. Regardless of the platform branch that you choose, you can use the AWS SDK for Java to use other AWS services from within your Java application.
AWS has documentation for: Determining an Application’s Current Region.
Regarding their API "The new Regions.getCurrentRegion() method makes this a lot easier. For example, if you start an Amazon EC2 instance in us-west-1 and run your application on that instance, it would know it’s running in us-west-1 and you could use that information to easily configure your application to talk to other services in us-west-1."
.. and here is a code snippet with the key words you're looking for to get you going in the right direction:
// When running on an Amazon EC2 instance, this method
// will tell you what region your application is in
Region region = Regions.getCurrentRegion();
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