diff --git a/aliases b/aliases index 8910f43e..2614c477 100644 --- a/aliases +++ b/aliases @@ -36,6 +36,8 @@ alias aws-account-cost-recommendations='~/.bash-my-aws/bin/bma aws-account-cost- alias aws-account-each='~/.bash-my-aws/bin/bma aws-account-each' alias aws-account-id='~/.bash-my-aws/bin/bma aws-account-id' alias aws-panopticon='~/.bash-my-aws/bin/bma aws-panopticon' +alias aws-profile-each='~/.bash-my-aws/bin/bma aws-profile-each' +alias aws-profiles='~/.bash-my-aws/bin/bma aws-profiles' alias bucket-acls='~/.bash-my-aws/bin/bma bucket-acls' alias bucket-remove='~/.bash-my-aws/bin/bma bucket-remove' alias bucket-remove-force='~/.bash-my-aws/bin/bma bucket-remove-force' @@ -167,4 +169,18 @@ function region() { else AWS_DEFAULT_REGION="$inputs"; fi -} \ No newline at end of file +} + +function aws-profile() { + local inputs="$@ $(skim-stdin)"; + # Remove leading and trailing spaces + inputs="${inputs/# /}" + inputs="${inputs/% /}" + + if [[ -z "${inputs}" ]]; then + echo "${AWS_DEFAULT_PROFILE:-AWS_DEFAULT_PROFILE not set}"; + else + AWS_DEFAULT_PROFILE="${inputs}"; + export AWS_DEFAULT_PROFILE + fi +} diff --git a/docs/command-reference.md b/docs/command-reference.md index 01c7d006..182c296b 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -9,6 +9,8 @@ The first few sets of commands were chosen because they are likely to be of the most interest to readers. !!! Note "General Rules" + - Commands expect `$AWS_DEFAULT_PROFILE` environment variable to be set + (check/set with `aws-profile` command) - Commands expect `$AWS_DEFAULT_REGION` environment variable to be set (check/set with `region` command) - Most commands that list resources (`stacks`, `instances , etc) @@ -84,6 +86,48 @@ in Cost Recommendations. #=> Opens web browser to AWS Cost Recommendations with accounts selected +## profile-commands + +### aws-profiles + +List profiles + +The aws-profile() function must be sourced in order to update the +AWS_DEFAULT_PROFILE environment variable. This is because it +cannot update an environment variable when run as a subprocess. + + $ aws-profiles + dev + test + stage + prod + + +### aws-profile + +Get/Set `$AWS_DEFAULT_PROFILE` shell environment variable + + $ aws-profile + test + + $ aws-profile prod + + $ aws-profile + prod + + +### aws-profile-each + +Run a command in every profile. +Any output lines will be appended with "#${PROFILE}". + + $ aws-profile-each stacks | column -t + example-ec2-ap-northeast-1 CREATE_COMPLETE 2011-05-23T15:47:44Z NEVER_UPDATED NOT_NESTED #prod + example-ec2-ap-northeast-2 CREATE_COMPLETE 2011-05-23T15:47:44Z NEVER_UPDATED NOT_NESTED #stage + ... + example-ec2-us-west-2 CREATE_COMPLETE 2011-05-23T15:47:44Z NEVER_UPDATED NOT_NESTED #test + + ## region-commands diff --git a/functions b/functions index bf8fd9bd..fc9fc240 100644 --- a/functions +++ b/functions @@ -34,6 +34,8 @@ aws-account-cost-recommendations aws-account-each aws-account-id aws-panopticon +aws-profile-each +aws-profiles bucket-acls bucket-remove bucket-remove-force diff --git a/lib/profile-functions b/lib/profile-functions new file mode 100644 index 00000000..703f08d1 --- /dev/null +++ b/lib/profile-functions @@ -0,0 +1,49 @@ +#!/bin/bash +# +# profile-functions +# + + +aws-profiles() { + + # List profiles + # + # The profile() function must be sourced in order to update the + # AWS_DEFAULT_PROFILE environment variable. This is because it + # cannot update an environment variable when run as a subprocess. + # + # $ aws-profiles + # test + # stage + # prod + aws_creds=~/.aws/credentials + while read -r line; do + if [[ "${line:0:1}" == "[" ]]; then + echo "${line:1:-1}" + fi + done < "${aws_creds}" + + #LC_ALL=C sort +} + + +aws-profile-each() { + + # Run a command in every profile. + # Any output lines will be appended with "#${PROFILE}". + # + # $ aws-profile-each stacks | column -t + # example-ec2-ap-northeast-1 CREATE_COMPLETE 2011-05-23T15:47:44Z NEVER_UPDATED NOT_NESTED #prod + # example-ec2-ap-northeast-2 CREATE_COMPLETE 2011-05-23T15:47:44Z NEVER_UPDATED NOT_NESTED #stage + # ... + # example-ec2-us-west-2 CREATE_COMPLETE 2011-05-23T15:47:44Z NEVER_UPDATED NOT_NESTED #test + + local old_aws_default_profile="$AWS_DEFAULT_PROFILE" + for aws_profile in $(aws-profiles); do + # Have to export this so that it works with the following commands + export AWS_DEFAULT_PROFILE=$aws_profile + eval "$@" | sed "s/$/ #${AWS_DEFAULT_PROFILE}/" + done + AWS_DEFAULT_PROFILE="$old_aws_default_profile" +} +