Skip to content

Instantly share code, notes, and snippets.

@aztecrex
Last active February 26, 2023 18:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztecrex/38e0edbf27d2cdc31bb8 to your computer and use it in GitHub Desktop.
Save aztecrex/38e0edbf27d2cdc31bb8 to your computer and use it in GitHub Desktop.
AWS Session Helper Functions
# source this for some sweet sweet aws session helpers
# invoke STS to get session creds and put them in vars where they can
# be passed into something such as a docker container. This places creds
# in the vars expected by the CLI so they will mask any set by aws configure
#
# aws-session
# docker run -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN ...
aws-session() {
eval "$(aws sts get-session-token \
--query \
'Credentials |
join (`\n`,
values({
AccessKeyId: join(``, [`export AWS_ACCESS_KEY_ID=`,AccessKeyId]),
SecretAccessKey:join(``, [`export AWS_SECRET_ACCESS_KEY=`,SecretAccessKey]),
SessionToken:join(``, [`export AWS_SESSION_TOKEN=`,SessionToken])
}))' \
--output text)"
}
# invoke STS to get session creds and puts them in temp vars where they can
# be passed into something such as a docker container. This version creates vars that
# are not picked up by the aws CLI.
#
# aws-session-t
# docker run -e AWS_ACCESS_KEY_ID=$T_AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$T_AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN=$T_AWS_SESSION_TOKEN ...
aws-session-t() {
eval "$(aws sts get-session-token \
--query \
'Credentials |
join (`\n`,
values({
AccessKeyId: join(``, [`export T_AWS_ACCESS_KEY_ID=`,AccessKeyId]),
SecretAccessKey:join(``, [`export T_AWS_SECRET_ACCESS_KEY=`,SecretAccessKey]),
SessionToken:join(``, [`export T_AWS_SESSION_TOKEN=`,SessionToken])
}))' \
--output text)"
}
# unset aws session vars of both actual and temp type
aws-clear-session() {
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
unset T_AWS_ACCESS_KEY_ID
unset T_AWS_SECRET_ACCESS_KEY
unset T_AWS_SESSION_TOKEN
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment