Last active
December 21, 2015 07:29
-
-
Save NitriKx/6271454 to your computer and use it in GitHub Desktop.
[New AWS CLI Tool]
Create an aws-credential-file based on EC2 instance role.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}` | |
# Get access keys | |
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'` | |
SECURITY_TOKEN=`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 'Token' | cut -d":" -f2| sed -e 's/^ *//g' -e 's/ *$//g'` | |
# Get instance region and make it the default endpoint | |
EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone` | |
AWS_INSTANCE_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`" | |
echo "[default]" > /tmp/aws_credentials | |
echo "aws_access_key_id=${AWS_ACCESS_KEY}" >> /tmp/aws_credentials | |
echo "aws_secret_access_key=${AWS_SECRET_KEY}" >> /tmp/aws_credentials | |
echo "aws_security_token=${SECURITY_TOKEN}" >> /tmp/aws_credentials | |
echo "region=${AWS_INSTANCE_REGION}" >> /tmp/aws_credentials |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment