I always like to try new things out. So when I saw AWS CLI v2 is generally available, I just upgraded it without checking changes.
One thing I found different after the upgrade is that, seems all outputs are redirected to things like less
. It’s somehow inconvenient when calling AWS CLI in a shell script:
#! /bin/bash
# If you pipe into something or assigne to a variable, it will be fine.
REGIONS=`aws ec2 describe-regions | jq '.Regions[].RegionName' -r`
for REGION in $REGIONS; do
echo "Listing keypairs in $REGION..."
aws --region $REGION ec2 describe-key-pairs
done
What happens at the line of aws --region $REGION ec2 describe-key-pairs
is that the script gets stuck in the screen of less
. It won’t proceed unless you hit q key.
The reason is mentioned in the document:
By default, AWS CLI version 2 returns all output through your operating system’s default pager program. By default this program is the less program on Linux and macOS, and the more program on Windows. This can make it easier for you to navigate a large amount of output from a service by displaying that output one page at a time.
And we have a few solutions:
Add
cli_pager=
in~/.aws/config
file.[default] cli_pager= ...
However, you have to add this option under all of your profiles.
Set environment variable
AWS_PAGER
to an empty string.$ export AWS_PAGER=""
So, the lesson here? Don’t be lazy and read the document. Especially when it’s a major version upgrade.