I created a .bashrc
function for AWS ec2 describe-instances
and for some reason, I cannot run the command twice, in terminal:
$ aws-instance -lp my-aws-profile -l dev
dev-application 10.10.11.122 i-xxxxxxxxxxxxxxxxx None ami-xxxxxxxxxxxxxxxxx running
$ aws-instance -lp my-aws-profile -l dev
$
.bashrc.d/aws_commands
in RHEL9 environment:
function help-aws-instance() {
# requires tabs instead of spaces, for proper formatting
cat <<- EOF
Actions related to AWS EC2 instances.
Usage: aws-instance [OPTION]...
-h, --help Display help
-l, --list List all EC2 instances
-n NAME, --name=NAME EC2 instance prefix or full name
-p PROFILE, --profile=PROFILE AWS profile to use
-s, --start Start EC2 instance
-o, --stop Stop EC2 instance
EOF
}
aws-instance() {
local filters list name profile query start stop
declare {list,start,stop}=false
for arg in "$@"; do
shift
case "$arg" in
--help) set -- "$@" '-h' ;;
--list) set -- "$@" '-l' ;;
--name) set -- "$@" '-n' ;;
--profile) set -- "$@" '-p' ;;
--start) set -- "$@" '-s' ;;
--stop) set -- "$@" '-o' ;;
*) set -- "$@" "$arg" ;;
esac
done
while getopts "hlson:p:" options; do
case "$options" in
h) help-aws-instance && exit 0 ;;
l) list=true ;;
n) name="$OPTARG"; shift 0 ;;
p) profile="$OPTARG"; shift 0 ;;
s) start=true ;;
o) stop=true ;;
?) help-aws-instance && exit 1 ;;
esac
done
if $list; then
if [ -z "$profile" ]; then
help-aws-instance
exit 1
fi
if [ -n "$name" ]; then
name="$name*"
else
name='*'
fi
filters="Name=tag:Name,Values=$name"
query='Reservations[].Instances[].[Tags[?Key==`Name`]|[0].'
query+='Value,PrivateIpAddress,InstanceId,'
query+='Placement.HostId,ImageId,State.Name]'
aws ec2 describe-instances --profile $profile --query $query
--filters $filters --output text
fi
}
I did some research, but is very difficult to find the root cause. Also, I”m not sure what is the proper way to debug functions in bashrc? I’m not sure what am I missing, thank you for providing some guidance.