Skip to content

[aws-profile] Added profile functions to mirror the region functions #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion aliases
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -167,4 +169,18 @@ function region() {
else
AWS_DEFAULT_REGION="$inputs";
fi
}
}

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
}
44 changes: 44 additions & 0 deletions docs/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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


Expand Down
2 changes: 2 additions & 0 deletions functions
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions lib/profile-functions
Original file line number Diff line number Diff line change
@@ -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"
}