aws_config::imds::client

Struct 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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>,

Source§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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