Learn R Programming

aws.ec2metadata (version 0.2.0)

aws.ec2metadata-package: Get EC2 Instance Metadata

Description

Retrieve EC2 instance metadata from the instance

Usage

is_ec2()

instance_document()

metadata

is_ecs()

ecs_metadata(base_url = "http://169.254.170.2")

Arguments

base_url

Base URL for querying instance metadata

Value

is_ec2() and is_ecs() return a logical. Generally, all other functions will return a character string containing the requested information, otherwise a NULL if the response is empty. The iam_role() and ecs_metadata() functions return a list. An error will occur if, for some reason, the request otherwise fails.

Format

An object of class list of length 26.

Details

is_ec2() returns a logical for whether the current R session appears to be running in an EC2 instance. is_ecs() returns a logical for whether the current R session appears to be running in an ECS task container.

instance_document returns a list containing values from the Instance Identity Document, including the instance ID, AMI ID, region, availability zone, etc.

metadata is a list of functions to return various metadata values for the currently running EC2 instance. These are only meant to be called from an EC2 instance; no guarantees are made about behavior on other platforms. Two functions: versions() and meta_data_items() return the versions of the metadata, and the set of top-level metadata items, respectively.

The function item() retrieves a particular metadata item specified by its full path.

The remaining functions in the list are aliases for potentially commonly needed metadata items.

The environment variable AWS_METADATA_SERVICE_TIMEOUT controls the timeout for instance metadata checks, and defaults to 1 second.

References

Metadata Documentation

Examples

Run this code
# NOT RUN {
names(metadata)

# }
# NOT RUN {
if (is_ec2()) {
  metadata$versions()
  metadata$items()

  # get instance id
  metadata$instance_id()
  # get ami id
  metadata$ami_id()
  
  # get IAM role (NULL if none specified)
  metadata$iam_info()
  metadata$iam_role("myrole")

  # get an arbitrary metadata item
  metadata$item("meta-data/placement/availability-zone")
  
  # get region from instance identity document
  instance_document()$region
}

# Can also get ECS container metadata
if (is_ecs()) {
  # Get ECS role credentials
  metadata$ecs_task_role()
  # or
  ecs_metadata()
}
# }

Run the code above in your browser using DataLab