Skip to content

Instantly share code, notes, and snippets.

@hflamboauto1
Forked from magnetikonline/example.sh
Created July 6, 2016 11:04
Show Gist options
  • Save hflamboauto1/d944a8562ebf8340125e3c437cef4322 to your computer and use it in GitHub Desktop.
Save hflamboauto1/d944a8562ebf8340125e3c437cef4322 to your computer and use it in GitHub Desktop.
AWS EC2 extract IAM role data from HTTP instance data using bash & curl.
#!/bin/bash
IAM_BASE_URL="http://169.254.169.254/latest/meta-data/iam/security-credentials"
IAMRoleName=$(curl -s $IAM_BASE_URL/)
IAMRoleData=$(curl -s $IAM_BASE_URL/$IAMRoleName/)
IAMRoleAccessKeyID=$(echo -n "$IAMRoleData" | sed -nr 's/.*?"AccessKeyId"[^"]+"([^"]+)",?/\1/p')
IAMRoleAccessKeySecret=$(echo -n "$IAMRoleData" | sed -nr 's/.*?"SecretAccessKey"[^"]+"([^"]+)",?/\1/p')
IAMRoleToken=$(echo -n "$IAMRoleData" | sed -nr 's/.*?"Token"[^"]+"([^"]+)",?/\1/p')
echo $IAMRoleAccessKeyID
echo $IAMRoleAccessKeySecret
echo $IAMRoleToken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment