Skip to content

Instantly share code, notes, and snippets.

@JGalego
Last active June 27, 2024 10:52
Show Gist options
  • Save JGalego/252f2008ef7cb2ee79d7000df7220cd3 to your computer and use it in GitHub Desktop.
Save JGalego/252f2008ef7cb2ee79d7000df7220cd3 to your computer and use it in GitHub Desktop.
Harvest AWS Credentials from IMDSv2
# Adapted from https://rhynorater.github.io/AWS-Metadata-Identity-Credentials
token=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
export AWS_DEFAULT_REGION=`curl -s -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/placement/region`
creds=`curl -s -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/identity-credentials/ec2/security-credentials/ec2-instance/`
export AWS_ACCESS_KEY_ID=`echo ${creds} | jq -r .AccessKeyId`
export AWS_SECRET_ACCESS_KEY=`echo ${creds} | jq -r .SecretAccessKey`
export AWS_SESSION_TOKEN=`echo ${creds} | jq -r .Token`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment