Skip to content

Instantly share code, notes, and snippets.

@kjhosein
Created August 14, 2017 16:30
Show Gist options
  • Save kjhosein/e46c30369ef78834ee69d4f8fb978a08 to your computer and use it in GitHub Desktop.
Save kjhosein/e46c30369ef78834ee69d4f8fb978a08 to your computer and use it in GitHub Desktop.
Get temporary AWS credentials from instance profile
#!/bin/bash
# Get temporary AWS credentials as granted in the Instance Profile.
#
# Run it 'sourced'. i.e.:
# . ./get-temp-creds.sh
#
# Requires: jq
#
# Written Aug 2017 by Khalid J Hosein
instance_profile=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/)
AWS_ACCESS_KEY_ID=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | jq -r .AccessKeyId)
AWS_SECRET_ACCESS_KEY=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | jq -r .SecretAccessKey)
AWS_SESSION_TOKEN=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | jq -r .Token)
echo -e "\n Check that the following environment variables contain AWS access keys and token."
set | grep AWS
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment