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=2On 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
Endpointvia 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_ENDPOINTenvironment variable. Note: If this environment variable is set, it MUST contain to a valid URI or client construction will fail. -
The
ec2_metadata_service_endpointfield 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_MODEenvironment variable. Valid values:IPv4,IPv6 -
An endpoint mode loaded from the
ec2_metadata_service_endpoint_modefield in~/.aws/config:
[default]
# ... other configuration
ec2_metadata_service_endpoint_mode = IPv4- The default value of
http://169.254.169.254will 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");Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more