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
metadatafrom EC2’s Instance MetaData Service (IMDS) running on the current host, by sending an HTTP GET request tohttp://169.254.169.254/latest/meta-data/<metadata>.To get a list of all valid values of
metadatarun 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
metadataorNoneif 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 ofInstanceMetadatawhere 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
tokenfrom EC2’s Instance MetaData Service (IMDSV2) running on the current host, by sending an HTTP GET request tohttp://169.254.169.254/latest/meta-data/<metadata>.- Parameters:
timeout – request timeout
- Returns:
the specified
tokenor""if IMDSV2 cannot be reached