Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Java determine if running on AWS

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.

like image 670
ccleve Avatar asked May 22 '13 20:05

ccleve


People also ask

How do you check what is running on AWS?

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.

How do I know if a server is running on AWS?

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.

Does AWS run on Java?

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.


1 Answers

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();
like image 105
StartupGuy Avatar answered Sep 29 '22 03:09

StartupGuy