Skip to content

Instantly share code, notes, and snippets.

@novice81
Created March 11, 2020 11:42
Show Gist options
  • Select an option

  • Save novice81/0f71f63ac586377fe38312218805aa0a to your computer and use it in GitHub Desktop.

Select an option

Save novice81/0f71f63ac586377fe38312218805aa0a to your computer and use it in GitHub Desktop.
AWS CLI to assume role
#!/bin/bash
$PROFILE_NAME="user-has-assume-role"
$ROLE_ARN="role-arn-that-has-policies-to-work"
$SESSION_NAME="session-name-to-identify"
export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile $PROFILE_NAME)
export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile $PROFILE_NAME)
unset AWS_SESSION_TOKEN
ASSUME_ROLE_RESULT=$(aws sts assume-role \
--role-arn $ROLE_ARN \
--role-session-name $SESSION_NAME)
export AWS_ACCESS_KEY_ID=$(echo $ASSUME_ROLE_RESULT | jq -r '.Credentials.AccessKeyId')
export AWS_SECRET_ACCESS_KEY=$(echo $ASSUME_ROLE_RESULT | jq -r '.Credentials.SecretAccessKey')
export AWS_SESSION_TOKEN=$(echo $ASSUME_ROLE_RESULT | jq -r '.Credentials.SessionToken')
aws sts get-caller-identity
# You can use this like '$ source assume-role.sh'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment