Struct aws_config::imds::client::Client
source · 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
- Explicit configuration of
Endpoint
via the builder:
use aws_config::imds::client::Client;
let client = Client::builder()
.endpoint("http://customidms:456/").expect("valid URI")
.build();
-
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. -
The
ec2_metadata_service_endpoint
field in~/.aws/config
:
[default]
# ... other configuration
ec2_metadata_service_endpoint = http://my-custom-endpoint:444
- An explicitly set endpoint mode:
use aws_config::imds::client::{Client, EndpointMode};
let client = Client::builder().endpoint_mode(EndpointMode::IpV6).build();
-
An endpoint mode loaded from the
AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE
environment variable. Valid values:IPv4
,IPv6
-
An endpoint mode loaded from the
ec2_metadata_service_endpoint_mode
field in~/.aws/config
:
[default]
# ... other configuration
ec2_metadata_service_endpoint_mode = IPv4
- The default value of
http://169.254.169.254
will be used.
Implementations§
source§impl Client
impl Client
sourcepub async fn get(
&self,
path: impl Into<String>
) -> Result<SensitiveString, ImdsError>
pub async fn get( &self, path: impl Into<String> ) -> Result<SensitiveString, ImdsError>
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();
let ami_id = client
.get("/latest/meta-data/ami-id")
.await
.expect("failure communicating with IMDS");