imds#

EC2 Instance Metadata Service related methods

class fkat.utils.aws.imds.InstanceMetadata(instance_id: str, instance_type: str, hostname: str, public_hostname: str, local_hostname: str, local_ipv4: str, availability_zone: str, region: str, ami_id: str)[source]#

Struct representing the instance metadata as fetched from IMDS on the current host. Use fkat.utils.aws.imds.instance_metadata() to get a filled-out instance of this object.

ami_id: str#
availability_zone: str#
hostname: str#
instance_id: str#
instance_type: str#
local_hostname: str#
local_ipv4: str#
public_hostname: str#
region: str#
fkat.utils.aws.imds.fetch(metadata: str = '', token: str | None = None) str | None[source]#

Fetches the specified metadata from EC2’s Instance MetaData Service (IMDS) running on the current host, by sending an HTTP GET request to http://169.254.169.254/latest/meta-data/<metadata>.

To get a list of all valid values of metadata run this method with no arguments then split the return value by new-line.

Parameters:
  • metadata – Name of the instance metadata to query (e.g. instance-type)

  • token – IMDS token

Returns:

the specified metadata or None if IMDS cannot be reached

fkat.utils.aws.imds.instance_metadata() InstanceMetadata[source]#

Fetches IMDS instance metadata for the current host from EC2’s Instance Metadata Service (IMDS), which typically runs on localhost at http://169.254.169.254. If IMDS cannot be reached for any reason returns an instance of InstanceMetadata where all the fields are empty strings.

Note

This method is memoized (value is cached) hence, only the first call will actually hit IMDS, and subsequent calls will return the memoized value. Therefore, it is ok to call this function multiple times.

fkat.utils.aws.imds.token(timeout: int = 60) str | None[source]#

Fetches IMDS token from EC2’s Instance MetaData Service (IMDSV2) running on the current host, by sending an HTTP GET request to http://169.254.169.254/latest/meta-data/<metadata>.

Parameters:

timeout – request timeout

Returns:

the specified token or "" if IMDSV2 cannot be reached