Interface Ec2MetadataClient
- All Superinterfaces:
- AutoCloseable,- SdkAutoCloseable
- All Known Implementing Classes:
- DefaultEc2MetadataClient
Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running EC2 instance.
  
 
 
 
 
Note: A single client instance should be reused for multiple requests when possible.
Instantiate the Ec2MetadataClient
Default configuration
Ec2MetadataClient client = Ec2MetadataClient.create();
Custom configuration
Example of a client configured for using IPV6 and a fixed delay for retry attempts :Ec2MetadataClient client = Ec2MetadataClient.builder()
    .retryPolicy(p -> p.backoffStrategy(FixedDelayBackoffStrategy.create(Duration.ofMillis(500))))
    .endpointMode(EndpointMode.IPV6)
    .build();
Use the client
To retrieve EC2 Instance Metadata, call theget method on the client with a path to an instance metadata:
 
Ec2MetadataClient client = Ec2MetadataClient.create();
Ec2MetadataResponse response = client.get("/latest/meta-data/");
System.out.println(response.asString());
Closing the client
Once all operations are done, you may close the client to free any resources used by it.Ec2MetadataClient client = Ec2MetadataClient.create();
// ... do the things
client.close();
Note: A single client instance should be reused for multiple requests when possible.
- 
Nested Class SummaryNested ClassesModifier and TypeInterfaceDescriptionstatic interfaceThe builder definition for aEc2MetadataClient.
- 
Method SummaryModifier and TypeMethodDescriptionstatic Ec2MetadataClient.Builderbuilder()Creates a default builder forEc2MetadataClient.static Ec2MetadataClientcreate()Create anEc2MetadataClientinstance using the default values.Gets the specified instance metadata value by the given path.Methods inherited from interface software.amazon.awssdk.utils.SdkAutoCloseableclose
- 
Method Details- 
getGets the specified instance metadata value by the given path. For more information about instance metadata, check the Instance metadata documentation- Parameters:
- path- Input path
- Returns:
- Instance metadata value as part of MetadataResponse Object
- Throws:
- Ec2MetadataClientException- if the request returns a 4XX error response. The exception includes the HTTP status code, headers, and error response body
- RetryableException- if the request returns a 5XX error response and should be retried
- SdkClientException- if the maximum number of retries is reached, if there's an IO error during the request, or if the response is empty when success is expected
 
- 
createCreate anEc2MetadataClientinstance using the default values.- Returns:
- the client instance.
 
- 
builderCreates a default builder forEc2MetadataClient.
 
-