Skip to content

Instantly share code, notes, and snippets.

@dan-osms
Forked from xuwang/get-aws-metadata
Created October 27, 2017 15:12
Show Gist options
  • Save dan-osms/b92f2a3f10b5c2d38a3fb76918c9e894 to your computer and use it in GitHub Desktop.
Save dan-osms/b92f2a3f10b5c2d38a3fb76918c9e894 to your computer and use it in GitHub Desktop.

Revisions

  1. @xuwang xuwang revised this gist Jun 18, 2015. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions get-aws-metadata
    Original file line number Diff line number Diff line change
    @@ -48,10 +48,8 @@ case $info in
    S3SECRET)
    result=$(get_sts_value "SecretAccessKey")
    ;;
    REGION)
    result=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document \
    | grep -i '"region"' \
    | sed 's/region//;s/"//g;s/://;s/,//;s/ //g')
    ZONE)
    result=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/)
    ;;
    *)
    echo "Usage: $(basename $0) <argument>. Input is not case sensitive"
  2. @xuwang xuwang created this gist Jun 18, 2015.
    64 changes: 64 additions & 0 deletions get-aws-metadata
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #!/bin/bash
    # Retrieve AWS instrance's commonly used metadata. Require curl.
    # ./get-metadata help
    # ./get-metadata id

    # Input is case insensitive; format to uppper case to generate self-help page.
    info=${1^^}
    meta_data_url=http://169.254.169.254/latest/meta-data/
    roleProfile=$(curl -s http://169.254.169.254/latest/meta-data/iam/info \
    | grep -Eo 'instance-profile/([a-zA-Z.-]+)' | sed 's#instance-profile/##')

    # auth values
    get_sts_value() {
    echo -n $(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/$roleProfile/ \
    | grep "$1" \
    | awk -F":" '{print $2}' \
    | sed 's/^[ ^t]*//;s/"//g;s/,//g')
    }

    case $info in
    ACCOUNT)
    result=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep -Eo '([[:digit:]]{12})')
    ;;
    HOSTNAME)
    result=$(curl -s http://169.254.169.254/latest/meta-data/public-hostname)
    ;;
    ID|INSTANCEID)
    result=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    ;;
    PRIVATEIP)
    result=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
    ;;
    PUBLICIP)
    result=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
    ;;
    ROLE)
    result=$roleProfile
    ;;
    STSCRED)
    result=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/$roleProfile)
    ;;
    STSTOKEN)
    result=$(get_sts_value "Token")
    ;;
    STSKEY)
    result=$(get_sts_value "AccessKeyId")
    ;;
    S3SECRET)
    result=$(get_sts_value "SecretAccessKey")
    ;;
    REGION)
    result=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document \
    | grep -i '"region"' \
    | sed 's/region//;s/"//g;s/://;s/,//;s/ //g')
    ;;
    *)
    echo "Usage: $(basename $0) <argument>. Input is not case sensitive"
    grep -Eo '([A-Z.]+\))' $0 | sed 's/)//'
    ;;
    esac

    if [ ! -z "$result" ]; then
    echo "$result"
    fi