We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Introducing a new client in the Amazon Web Services SDK for Java 2.x for retrieving EC2 Instance Metadata
You can now use
There are now two new clients classes available with the Amazon Web Services SDK for Java 2.x: a synchronous client for blocking operations and an asynchronous client for non-blocking operations. The new clients use
This blog post shows how to use the new clients to retrieve metadata in your application and takes a deeper look at some of the new features.
Getting Started
To use the EC2 Instance Metadata Client, first add the
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>imds</artifactId>
<version>${imds.version}</version>
</dependency>
You will also need classes for an SdkHttpClient
(or SdkAsyncHttpClient
for the asynchronous variant) on the classpath, since its required by the metadata clients. You can learn more details on this in the Configurable HTTP client section.
Sending requests
To retrieve instance metadata, simply instantiate the EC2MetadataClient
class and call the get
method with a path parameter that specifies the
Ec2MetadataClient client = Ec2MetadataClient.create();
Ec2MetadataResponse response = client.get("/latest/meta-data/ami-id");
System.out.println(response.asString());
client.close(); // closes the internal resources used by the Ec2MetadataClient class
If an invalid path is specified, the get
method throws an exception.
Remember that you don’t need to create a new client for every request. You can reuse the same instance for multiple requests, but remember to close the client, when it is no longer needed, by calling the close
method on the client instance. The client method can no longer be used once the close
method is called.
Parsing response
EC2 instance metadata can be output in different formats. Since Plain text and JSON are the most commonly used formats, the metadata clients offer easy ways to work with these formats.
As the following example shows, use the asString
method to get the data as a simple Java String. You can also use the asList
method to separate a plain text response that returns multiple lines.
Ec2MetadataClient client = Ec2MetadataClient.create();
Ec2MetadataResponse response = client.get("/latest/meta-data/");
String fullResponse = response.asString();
List<String> splits = response.asList();
If the response is in JSON, use the Ec2MetadataResponse#asDocument
method to parse the JSON response into a Document
instance.
Document fullResponse = response.asDocument();
The client will throw an exception if the format of the metadata is not in JSON. If the response is successfully parsed, the
Configuring the EC2 Metadata Client
You can configure the EC2 Instance Metadata Client with a
Since majority of applications require only the default retry configuration, we recommend you keep the default retry policy for your projects. However, if your use cases require unique configuration, modifying the retry mechanism is quite easy. The following example shows a synchronous client configured with a fixed delay between attempts and 5 retry attempts.
BackoffStrategy fixedBackoffStrategy =
FixedDelayBackoffStrategy.create(Duration.ofSeconds(2));
Ec2MetadataClient client =
Ec2MetadataClient.builder()
.retryPolicy(retryPolicyBuilder ->
retryPolicyBuilder.numRetries(5)
.backoffStrategy(fixedBackoffStrategy))
.build();
You can also disable the retry mechanism entirely using the following snippet.
Ec2MetadataClient client =
Ec2MetadataClient.builder()
.retryPolicy(Ec2MetadataRetryPolicy.none())
.build();
Using Ec2MetadataRetryPolicy.none()
will make sure no retries are attempted. There are many different
Key Features
Asynchronous Client
To use the non-blocking version of the client, instantiate an instance of the Ec2MetadataAsyncClient class. The code in the following example creates an asynchronous client with default settings and uses the get method to retrieve the value for the ami-id
key.
Ec2MetadataAsyncClient asyncClient = Ec2MetadataAsyncClient.create();
CompletableFuture<Ec2MetadataResponse> response = asyncClient.get("/latest/meta-data/ami-id");
The java.util.concurrent.CompletableFuture
returned by the get
method completes when a response is returned. The following example prints the ami-id metadata to the console.
response.thenAccept(metadata -> System.out.println(metadata.asString()));
If your workload does not require asynchronous programming, use the synchronous Ec2MetadataClient instead.
Configurable HTTP client
The following example shows how you can customize a
Sync Client |
Async Client |
|
|
Use an HTTP client to configure common options related to HTTP requests, such as timeouts or proxy options. To configure and use an HTTP client, add the artifacts to your project dependencies. Other implementations of synchronous HTTP clients and asynchronous HTTP clients are available in the Amazon Web Services SDK for Java v2.x. They all work with the metadata client.
Token Caching
All requests are associated with a session, because the new EC2 instance metadata client uses IMDSv2. A session is defined by a token that has an expiration, and is managed by the metadata client. Every metadata request automatically reuses the token until it expires.
By default, a token will last for 6 hours (21,600 seconds), but that duration is configurable using the tokentTtl builder method. For example, the following code snippet creates a client with a session duration of 5 minutes.
Ec2MetadataClient client =
Ec2MetadataClient.builder()
.tokenTtl(Duration.ofMinutes(5))
.build();
If you omit calling the tokenTtl
method on the builder, the default of 21,600 would be used instead. We recommend you keep the default time-to-live value unless your specific use case requires advanced configurations.
Conclusion
In this blog post, we showed the key features of the new Java SDK EC2 Instance Metadata Clients available in the Amazon Web Services SDK for Java v2.x. We provided examples to show how to use these clients and retrieve EC2 instance metadata. Tell us how you use EC2’s IMDS (Instance Metadata Service) and the new clients in your enterprise Java applications by commenting on this blog or by contacting us in
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.