Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Created July 14, 2015 20:38
Show Gist options
  • Save hagbarddenstore/12e907174927db8a03f3 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/12e907174927db8a03f3 to your computer and use it in GitHub Desktop.
A Bash script to retrieve a file stored on S3 via temporary IAM role credentials.
#!/bin/bash
if [ $# -ne 3 ]
then
echo "Usage: s3-get iam-role-name s3-resource output-file"
exit 1
fi
Role=$1
Resource=$2
Output=$3
AccessKeyId=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${Role} | sed -n '/AccessKeyId/{p;}' | cut -f4 -d'"'`
SecretAccessKey=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${Role} | sed -n '/SecretAccessKey/{p;}' | cut -f4 -d'"'`
Token=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${Role} | sed -n '/Token/{p;}' | cut -f4 -d'"'`
Date="`date +'%a, %d %b %Y %H:%M:%S %z'`"
SignatureString="GET\n\n\n${Date}\nx-amz-security-token:${Token}\n/${Resource}"
Signature=`/bin/echo -en "${SignatureString}" | openssl sha1 -hmac ${SecretAccessKey} -binary | base64`
curl -s -H "Date: ${Date}" -H "X-AMZ-Security-Token: ${Token}" -H "Authorization: AWS ${AccessKeyId}:${Signature}" "https://s3-eu-west-1.amazonaws.com/${Resource}" -o "${Output}"
@tmblue
Copy link

tmblue commented May 14, 2016

AWESOME!!! Thank you, don't think you have been given enough credit!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment