R: Get EC2 Instance Metadata

aws.ec2metadata-package {aws.ec2metadata} | R Documentation

## Get EC2 Instance Metadata

### Description

Retrieve EC2 instance metadata from the instance

### Usage

    is_ec2()

    instance_document()

    metadata

    is_ecs()

    ecs_metadata()

### Format

An object of class `list` of length 25.

### Details

`is_ec2` returns a logical for whether the current R session appears to be running in an EC2 instance.

`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.

### Value

`is_ec2` returns 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()` function returns 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

    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
      ecs_metadata()
    }

    ## End(Not run)

[Package aws.ec2metadata version 0.1.5 Index]