Skip to content

Instantly share code, notes, and snippets.

@Naoya9922
Last active October 22, 2019 08:41
Show Gist options
  • Save Naoya9922/e61410596efad6a14a622d29d0632d1d to your computer and use it in GitHub Desktop.
Save Naoya9922/e61410596efad6a14a622d29d0632d1d to your computer and use it in GitHub Desktop.
aws env variable string from 'aws sts assume-role' cli
#!/bin/bash
set -e
# Using jq command, you can format json to key=value string
session=$(aws sts assume-role --role-arn $ARN --role-session-name $USER --query 'Credentials')
env_options=$(echo $session | jq -r 'del(.Expiration) | to_entries | map([" ", "AWS_" + (.key | gsub("(?<l>[a-z])(?<f>[A-Z])";.l+"_"+.f)|ascii_upcase), "=", .value] | add) | add')
echo $env_options
#=> AWS_SECRET_ACCESS_KEY=xxxx AWS_SESSION_TOKEN=yyyy AWS_ACCESS_KEY_ID=zzzz
# Execute another aws cli using above credentials
env $env_options aws s3api list-buckets
# For docker usage, add "-e" argument
env_options=$(echo $session | jq -r 'del(.Expiration) | to_entries | map([" -e ", "AWS_" + (.key | gsub("(?<l>[a-z])(?<f>[A-Z])";.l+"_"+.f)|ascii_upcase), "=", .value] | add) | add')
#=> -e AWS_SECRET_ACCESS_KEY=xxxx -e AWS_SESSION_TOKEN=yyyy -e AWS_ACCESS_KEY_ID=zzzz
docker run --rm -it ${env_options} $IMAGE bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment