Skip to content

Instantly share code, notes, and snippets.

@kherge
Last active March 4, 2023 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kherge/5c12192695d30f4b662977155ccdc5b0 to your computer and use it in GitHub Desktop.
Save kherge/5c12192695d30f4b662977155ccdc5b0 to your computer and use it in GitHub Desktop.
AWS CLI Extension

AWS CLI Extension

A simple POSIX compatible shell script to allow aws CLI commands and subcommands to be added or overwritten.

Usage

There are two naming conventions for your executables.

  1. aws-cmd
  2. aws-cmd-subcmd

Where cmd and subcmd are your custom (or existing) command and subcommand names. If aws-example and aws-another-example are in your $PATH, then you will be able to execute aws example and aws another example as if they were part of the AWS CLI.

Note: You gain none of the benefits of a true AWS CLI command, such as profile configuration. However, you can use aws configure get $SETTING to get profile appropriate settings.

Installation

  1. Copy the script to $HOME/.local/opt/aws-extend.sh
  2. Edit your shell profile to include the script (e.g. source "$HOME/.local/opt/aws-extend.sh").
#!/usr/bin/env sh
####################################################################################################
# AWS CLI Extension #
# #
# Intercepts commands intended for the AWS CLI to allow custom commands and subcommands to be used #
# instead. If a custom command or subcommand does not exist, then the default (sub)command will be #
# executed. #
####################################################################################################
# shellcheck disable=SC3043
# The path to the AWS CLI.
AWS_CLI="$(command -v aws)"
aws()
{
# Default to the real AWS CLI.
local COMMAND="$AWS_CLI"
# Determine what the extended command would be.
if [ "$#" -ge 1 ]; then
local CHECK="aws-$1"
if [ "$#" -ge 2 ]; then
local SUB_CHECK="$CHECK-$2"
if command -v "$SUB_CHECK" > /dev/null; then
COMMAND="$SUB_CHECK"
shift 2
fi
elif command -v "$CHECK" > /dev/null; then
COMMAND="$CHECK"
shift
fi
fi
# Execute the extended or real command.
"$COMMAND" "$@"
return $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment