aws / aws-sdk-java

The official AWS SDK for Java 1.x. The AWS SDK for Java 2.x is available here: https://github.com/aws/aws-sdk-java-v2/
https://aws.amazon.com/sdkforjava
Apache License 2.0
4.12k stars 2.83k forks source link

Make EC2MetadataClient.readResource() fail fast in when outside of EC2 #450

Closed eddgrant closed 3 months ago

eddgrant commented 9 years ago

Scenario

readResource() makes use of HttpURLConnection to get metadata. When running this code outside of an EC2 instance the connection rightly fails due to the unavailability of http://169.254.169.254/latest/meta-data/instance-id outside of EC2.

The exception raised in this case is a java.net.SocketException: Connection reset. As far as I can tell from my testing the time taken to raise this exception is dependent upon the configuration of the outbound 'point-of-egress' network infrastructure. For example I discovered that whilst tethered through my phone I consistently get much quicker connection resets than whilst connected to our corporate infrastructure, which take several minutes to issue the reset packets. Incidentally I did try to configure the HttpURLConnection instance whilst in a debugger, setting connection.setConnectTimeout(5L) however this seems to have no effect.

Problem

I'm raising this as an issue because it is frustrating to have to wait for several minutes each time I want to test code locally outside of EC2. My particular use case is that I'm using Spring's Cloud AWS framework and this issue causes the Spring container to pause for several minutes upon startup when running locally. I have investigated whether it's possible only to initialise the EC2MetadataClient however this doesn't seem to be the case.

Would it be possible to introduce some sort of "am I in EC2 check" which fails fast prior to trying to obtain the meta-data? This would really ease the development cycle frustrations here.

Hope this makes sense, happy to provide more information if needed.

Many thanks!

david-at-aws commented 9 years ago

I'm not sure there's a great way to tell if we're on EC2 other than checking if the instance metadata service responds, but definitely worth looking into.

debora-ito commented 5 years ago

This is just a quick update letting you know that the SDK team has reviewed the feature request list for V1 and this one looks like a great candidate for a community PR, which we’ll help merge in and support.

debora-ito commented 3 months ago

We don't have plans to support this in v1 before going into Maintenance Mode, so I'll go ahead and close this feature request.

Please check the v2 Ec2MetadataClient (code examples can be found in the Developer Guide and in the Javadoc), and if this issue still persists please open a new issue in the v2 repo.

Reference:

github-actions[bot] commented 3 months ago

This issue is now closed.

Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.