pub struct Client { /* private fields */ }
Expand description

IMDSv2 Client

Client for IMDSv2. This client handles fetching tokens, retrying on failure, and token caching according to the specified token TTL.

Note: This client ONLY supports IMDSv2. It will not fallback to IMDSv1. See transitioning to IMDSv2 for more information.

Note: When running in a Docker container, all network requests will incur an additional hop. When combined with the default IMDS hop limit of 1, this will cause requests to IMDS to timeout! To fix this issue, you’ll need to set the following instance metadata settings :

amazonec2-metadata-token=required
amazonec2-metadata-token-response-hop-limit=2

On an instance that is already running, these can be set with ModifyInstanceMetadataOptions. On a new instance, these can be set with the MetadataOptions field on RunInstances.

For more information about IMDSv2 vs. IMDSv1 see this guide

Client Configuration

The IMDS client can load configuration explicitly, via environment variables, or via ~/.aws/config. It will first attempt to resolve an endpoint override. If no endpoint override exists, it will attempt to resolve an EndpointMode. If no EndpointMode override exists, it will fallback to IpV4. An exhaustive list is below:

Endpoint configuration list

  1. Explicit configuration of Endpoint via the builder:
use aws_config::imds::client::Client;
use http::Uri;
let client = Client::builder()
  .endpoint(Uri::from_static("http://customidms:456/"))
  .build()
  .await;
  1. The AWS_EC2_METADATA_SERVICE_ENDPOINT environment variable. Note: If this environment variable is set, it MUST contain to a valid URI or client construction will fail.

  2. The ec2_metadata_service_endpoint field in ~/.aws/config:

[default]
ec2_metadata_service_endpoint = http://my-custom-endpoint:444
  1. An explicitly set endpoint mode:
use aws_config::imds::client::{Client, EndpointMode};
let client = Client::builder().endpoint_mode(EndpointMode::IpV6).build().await;
  1. An endpoint mode loaded from the AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE environment variable. Valid values: IPv4, IPv6

  2. An endpoint mode loaded from the ec2_metadata_service_endpoint_mode field in ~/.aws/config:

[default]
ec2_metadata_service_endpoint_mode = IPv4
  1. The default value of http://169.254.169.254 will be used.

Implementations

IMDS client builder

Retrieve information from IMDS

This method will handle loading and caching a session token, combining the path with the configured IMDS endpoint, and retrying potential errors.

For more information about IMDSv2 methods and functionality, see Instance metadata and user data

Examples
use aws_config::imds::client::Client;
let client = Client::builder().build().await.expect("valid client");
let ami_id = client
  .get("/latest/meta-data/ami-id")
  .await
  .expect("failure communicating with IMDS");

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more