AWS Command Aliases

Wil Moore III
2 min readAug 17, 2022

An AWS CLI Hidden Feature

The AWS CLI has a feature called Command Aliases. Similar to git aliases, this feature allows the definition of custom commands that don’t currently exist. If you feel like a command should exist, you can add it as an alias. If you frequently use a hard to remember command or chain of commands, you can encapsulate that into a command alias.

Currently, the AWS CLI User Guide does not specify this feature. The best source of information is the awslabs/awscli-aliases GitHub repo; which itself seems a bit abandoned.

Create a minimal alias file:

> mkdir -p ~/.aws/cli
> echo '[toplevel]' > ~/.aws/cli/alias

~/.aws/cli/alias

> cat ~/.aws/cli/alias
[toplevel]

Add an “internal command alias“ to the alias file:

[toplevel]identity = sts get-caller-identity

In the alias file, since we are calling an existing command, the alias definition (the part after the “=”) can be written without the “aws “ prefix.

Normally, if you wanted to retrieve your assumed identity after authenticating, you’d type the command:

> aws sts get-caller-identity

Add a “shell command alias” to the alias file:

[toplevel]account = ! cat ~/.aws/credentials | grep '^\(account\|assumed\)_' | cut -d ' ' -f3 | tr '\n' ' ' | awk '{ printf "Account: %s (%s) Role: %s", $1, $2, $3 }'

In the alias file, since we are calling a shell command (pipes included of course), the alias definition (the part after the “=”) must begin with a “!” character. We could have defined the command as a function; however, this was not necessary in the above case as there are no parameters (arguments) that need to be passed.

--

--