Module resty.aws.utils
AWS utility module.
Provides methods for detecting the AWS Region, as well as fetching metadata.
Since the module requires access to the environment variables with metadata
URLs, it should preferably be loaded in the init
phases to make sure it can
access the variables required.
Functions
Utils.getCurrentRegion () | Auto detects the current AWS region. |
Utils.getECSTaskMetadata (subpath, version) | Fetches ECS Task Metadata. |
Utils.getIDMSMetadata (path, version) | Fetches IDMS Metadata (EC2 and EKS). |
Functions
- Utils.getCurrentRegion ()
-
Auto detects the current AWS region.
It will try the following options (in order);
- environment variable
AWS_REGION
- environment variable
AWS_DEFAULT_REGION
- ECS metadata V4 (parse region from "AvailabilityZone") if the environment
variable
ECS_CONTAINERMETADATA_URI_V4
is available - ECS metadata V3 (parse region from "AvailabilityZone") if the environment
variable
ECS_CONTAINERMETADATA_URI
is available - IDMSv2 metadata
The IDMSv2 call makes a call to an IP endpoint, and hence could timeout (timeout is 5 seconds) if called on anything not being an EC2 or EKS instance.
Note: the result is cached so any consequtive calls will not perform any IO.
Returns:
-
region, or nil+err
- environment variable
- Utils.getECSTaskMetadata (subpath, version)
-
Fetches ECS Task Metadata. Both for Fargate as well as EC2 based ECS.
Support version 2, 3, and 4 (version 2 is NOT available on Fargate).
V3 and V4 will return an error if no url is found in the related environment variable, V2 will make a request to
the IP address and hence might timeout if ran on anything else than a EC2-based ECS container.
Parameters:
- subpath (optional) path to return data from (default "/metadata" for V2, nothing for V3+)
- version (optional) metadata version to get "V3" or V4" (case insensitive, default "V4")
Returns:
-
body & content-type (if json, the body will be decoded to a Lua table), or nil+err
- Utils.getIDMSMetadata (path, version)
-
Fetches IDMS Metadata (EC2 and EKS).
Will make a call to the IP address and hence might timeout if ran on anything
else than an EC2-instance.
Parameters:
- path
(optional) path to return data from, default
/latest/meta-data/
- version (optional) version of IDMS to use, either "V1" or "V2" (case insensitive, default "V2")
Returns:
-
body & content-type (if json, the body will be decoded to a Lua table), or nil+err
- path
(optional) path to return data from, default