Skip to content

Instantly share code, notes, and snippets.

@ChristophP
Created May 15, 2020 14:55
Show Gist options
  • Save ChristophP/1f695e86aa15af9b4236838d50fdb3cb to your computer and use it in GitHub Desktop.
Save ChristophP/1f695e86aa15af9b4236838d50fdb3cb to your computer and use it in GitHub Desktop.
little shell script which can be sourced to act on behalf of an IAM role in AWS.
#!/usr/bin/env sh
# this script will modify your environment
# so that you can work in the context of an assumed role in AWS
set -u
if [ -z ${ASSUME_ROLE_ARN+x} ]
then
echo "NO ASSUME_ROLE_ARN variable configured in Environment.";
exit 1;
fi
# fetch credentials
CREDENTIALS=$(aws sts assume-role --role-arn $ASSUME_ROLE_ARN --role-session-name ci-pipeline --output text | awk 'NR==2')
# set relevant env vars
export AWS_ACCESS_KEY_ID=$(echo $CREDENTIALS | cut -d " " -f 2);
export AWS_SECRET_ACCESS_KEY=$(echo $CREDENTIALS | cut -d " " -f 4);
export AWS_SESSION_TOKEN=$(echo $CREDENTIALS | cut -d " " -f 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment