Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aashishrbhandari/41a35f76ba8b4548815f90b13590c1fa to your computer and use it in GitHub Desktop.
Save aashishrbhandari/41a35f76ba8b4548815f90b13590c1fa to your computer and use it in GitHub Desktop.
## Single Liner command to get the Role Temporary Security Credentials
TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && ROLE_NAME=`curl -s -H "X-aws-ec2-metadata-token: $TOKEN" "http://169.254.169.254/latest/meta-data/iam/security-credentials/"` && curl -s -H "X-aws-ec2-metadata-token: $TOKEN" "http://169.254.169.254/latest/meta-data/iam/security-credentials/$ROLE_NAME"
## Breakdown
# Get the Token to access the AWS EC2 Metadata, with the IMDSv2 we need to have a TOKEN to access EC2 metadata
TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
# The Below curl will use the Above Token and get the Rolename assigned to the EC2
ROLE_NAME=`curl -s -H "X-aws-ec2-metadata-token: $TOKEN" "http://169.254.169.254/latest/meta-data/iam/security-credentials/"`
# The below curl will use the TOKEN and the Rolename and fetch the Role Temporary Security Credentials
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" "http://169.254.169.254/latest/meta-data/iam/security-credentials/$ROLE_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment