Skip to content

Instantly share code, notes, and snippets.

@NitriKx
Last active December 21, 2015 07:28
Show Gist options
  • Save NitriKx/6270900 to your computer and use it in GitHub Desktop.
Save NitriKx/6270900 to your computer and use it in GitHub Desktop.
[Old CLI Tool] Create an aws-credential-file based on EC2 instance role.
#!/bin/bash
ROLE=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/`
JSON=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/${ROLE}`
AWS_ACCESS_KEY=`echo ${JSON} | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w 'AccessKeyId' | cut -d":" -f2| sed -e 's/^ *//g' -e 's/ *$//g'`
AWS_SECRET_KEY=`echo ${JSON} | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w 'SecretAccessKey' | cut -d":" -f2| sed -e 's/^ *//g' -e 's/ *$//g'`
echo "AWSAccessKeyId=${AWS_ACCESS_KEY}" > /tmp/aws_credentials
echo "AWSSecretKey=${AWS_SECRET_KEY}" >> /tmp/aws_credentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment