Differences between revisions 4 and 5
Revision 4 as of 2022-05-01 21:49:53
Size: 1677
Editor: PieterSmit
Comment:
Revision 5 as of 2022-05-01 21:59:56
Size: 1842
Editor: PieterSmit
Comment:
Deletions are marked like this. Additions are marked like this.
Line 32: Line 32:
   * Online grep for account using linux {{{
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep -oP '"accountId"\s*:\s*"\K[^"]+'
}}}

AWS/Ec2MetaData

  • https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html

  • Get TOKEN env var and then list of endpoints

    TOKEN=`curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
    echo "TOKEN=$TOKEN"
    curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/
  • Use TOKEN env to get hostname

    # for debug replace -sS with -v
    curl -H "X-aws-ec2-metadata-token: $TOKEN" -sS http://169.254.169.254/latest/meta-data/local-hostname ; echo
    curl -H "X-aws-ec2-metadata-token: $TOKEN" -sS http://169.254.169.254/latest/meta-data/public-hostname ; echo
  • Use TOKEN to get tags (Tags not available by default, have to enable see --metadata-options "InstanceMetadataTags=enabled"

    TOKEN=`curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
    echo "TOKEN=$TOKEN"
    ## Check for tags
    curl -H "X-aws-ec2-token: $TOKEN" -sS http://169.254.169.254/latest/meta-data/ | grep "tags" || ( echo "no tags exit=$? in metadata, InstanceMetadataTags not enabled for this instance ?")
    #
    curl -H "X-aws-ec2-metadata-token: $TOKEN" -sS http://169.254.169.254/latest/meta-data/tags/instance
  • Get account number from aws meta-data (search with jq or grep)

    TOKEN=`curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
    echo "TOKEN=$TOKEN"
    curl -s http://169.254.169.254/latest/dynamic/instance-identity/document ; echo
    • Online grep for account using linux

      curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep -oP '"accountId"\s*:\s*"\K[^"]+'

AWS/Ec2MetaData (last edited 2022-05-01 21:59:56 by PieterSmit)