ec2metadata: Get EC2 Instance Metadata

Description Usage Arguments Format Details Value Author(s) References Examples

Description

Retrieve EC2 instance metadata from the instance

Usage

1
2
3
4
5
6
7
8
9
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

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.

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.

Author(s)

Thomas J. Leeper <thosjleeper@gmail.com>

References

Metadata Documentation

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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()
}

## End(Not run)

Example output

 [1] "versions"          "meta_data_items"   "item"             
 [4] "ami_id"            "launch_index"      "ancestor_ami_ids" 
 [7] "hostname"          "iam_info"          "iam_role_names"   
[10] "iam_role"          "instance_action"   "instance_id"      
[13] "instance_type"     "kernel_id"         "local_hostname"   
[16] "local_ipv4"        "availability_zone" "public_hostname"  
[19] "public_ipv4"       "public_key"        "ramdisk_id"       
[22] "reservation_id"    "security_groups"   "domain"           
[25] "partition"         "ecs_task_role"    

aws.ec2metadata documentation built on July 15, 2019, 5:02 p.m.