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

  1. 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();
  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]
# ... other configuration
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();
  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]
# ... other configuration
ec2_metadata_service_endpoint_mode = IPv4
  1. The default value of http://169.254.169.254 will be used.

Implementations§

source§

impl Client

source

pub fn builder() -> Builder

IMDS client builder

source

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§

source§

impl Clone for Client

source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Client

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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