Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active March 26, 2024 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/81e31af0fed61b8faa837fc3fb171ea6 to your computer and use it in GitHub Desktop.
Save guitarrapc/81e31af0fed61b8faa837fc3fb171ea6 to your computer and use it in GitHub Desktop.
ShellScript to assume role by single command
  1. Prepare profile in ~/.aws/credentials.
  2. Change from_profile and to_profile to your profile name, then Run command.
from_profile=profile-A to_profile=profile-B sts=$(aws sts assume-role --profile "${from_profile}" --role-arn $(aws configure get "${to_profile}.role_arn") --role-session-name "${to_profile}-assume"); \
export AWS_ACCESS_KEY_ID=$(echo "${sts}" | jq -r '.Credentials.AccessKeyId'); \
export AWS_SECRET_ACCESS_KEY=$(echo "${sts}" | jq -r '.Credentials.SecretAccessKey'); \
export AWS_SESSION_TOKEN=$(echo "${sts}" | jq -r '.Credentials.SessionToken') \
export AWS_EXPIRE=$(echo "${sts}" | jq -r '.Credentials.Expiration')
# ~/.aws/credentials
[profile-A]
aws_access_key_id = AKIAxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[profile-B]
role_arn = arn:aws:iam::xxxxxxxxxxxx:role/iam-assume-role-name
source_profile = profile-A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment