Make EC2MetadataClient.readResource() fail fast in when outside of EC2 #450
Labels
feature-request
A feature should be added or improved.
help wanted
We are asking the community to submit a PR to resolve this issue.
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 theHttpURLConnection
instance whilst in a debugger, settingconnection.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!
The text was updated successfully, but these errors were encountered: