86

I am attempting to utilize the AWS CLI along with a for loop in bash to iteratively purge multiple SQS message queues. The bash script works almost as intended, the problem I am having is with the return value each time the AWS CLI sends a request. When the request is successful, it returns an empty value and opens up an interactive pager in the command line. I then have to manually type q to exit the interactive screen and allow the for loop to continue to the next iteration. This becomes very tedious and time consuming when attempting to purge a large number of queues.

Is there a way to configure AWS CLI to disable this interactive pager from popping up for every return value? Or a way to pipe the return values into a separate file instead of being displayed?

I have played around with configuring different return value types (text, yaml, JSON) but haven't had any luck. Also the --no-pagination parameter doesn't change the behavior.

Here's an example of the bash script I'm trying to run:

for x in 1 2 3; do 
  aws sqs purge-queue --queue-url https://sqs.<aws-region>.amazonaws.com/<id>/<env>-$x-<queueName>.fifo; 
done
Liz Av
  • 2,864
  • 1
  • 25
  • 35
Colin Armstrong
  • 1,609
  • 1
  • 12
  • 18
  • 2
    I can't reproduce this, Can you share an example? maybe share with us the SQS or the interactive pager? – Amit Baranes Feb 08 '20 at 09:30
  • 2
    Did you try `echo q | aws sqs ...` ? – Walter A Feb 08 '20 at 17:42
  • 2
    Rewrite the `for x (1 2 3)`, options: `for x in 1 2 3`, `for x in {1..3}` and `for ((x=1; x<=3; x++))`. – Walter A Feb 08 '20 at 17:45
  • 1
    `--no-pagination` refers to server-side pagination, for situations where the result of a list command would be too long for the server to respond the full list in a single response. It does not affect client-side pagination. – Liz Av Jun 10 '21 at 15:59

6 Answers6

106

Just running into this issue myself, I was able to disable the behaviour by invoking the aws cli as AWS_PAGER="" aws ....

Alternatively you could simply export AWS_PAGER="" at the top of your (bash) script.

Source: https://github.com/aws/aws-cli/pull/4702#issue-344978525

cueedee
  • 1,473
  • 1
  • 14
  • 17
  • Nice find on that Github pull request, that was exactly what I was trying to find in the AWS documentation but never had any luck. Thanks! – Colin Armstrong Feb 24 '20 at 16:35
  • 17
    As that source mentions, if you aren't into the env var solution, you can also add `cli_pager = cat` to your `~/.aws/config` – mziwisky Mar 10 '20 at 17:14
  • 1
    Amazing! finally got rid of the annoying behaviour on my ssm script – petern-sc Oct 20 '20 at 05:27
  • 5
    Here's the documentation on this as well as a lot of other useful configuration options: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-pagination.html – Scott McAllister May 23 '21 at 12:22
  • 2
    And if you don't want the extra feed through `cat`, you can just set `cli_pager = ` in your aws config – adelphus Sep 25 '22 at 12:25
56

You can also use --no-cli-pager in AWS CLI version 2.

See the "Client-side pager" section here https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-pagination.html

Danylo Fedorov
  • 741
  • 7
  • 9
45

You can disable pager either by exporting AWS_PAGER="" or by modifying you AWS cli config file.

export AWS_PAGER="" 

### or update your ~/.aws/config with 

[default]
cli_pager=

Alternatively, you can enable the default pager to output of less program as

export AWS_PAGER="less" 

or corresponding config change.

Ref: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-pagination.html#cli-usage-pagination-clientside

nakamume
  • 660
  • 6
  • 11
  • 2
    To update aws configuration file, this command can be used: `aws configure set cli_pager ""`. Append `--profile profile-name` to disable pager specifically for a profile. – cbliard Sep 05 '20 at 09:26
9

You can set the environment variable PAGER to "cat" to force awscli to not start up less:

PAGER=cat aws sqs list-queues

I set up as a shell alias to make my life easier:

# ~/.zshrc
alias aws="PAGER=cat aws"
DSimon
  • 3,280
  • 2
  • 21
  • 24
5

I am using the aws cli v2 via docker and passing the --env AWS_PAGER="" on the docker run command fixed this issue for me on windows 10 using git bash.

I set it up as an alias as well so things work with jq.

How to set your docker env values:

Example alias: docker run --rm -it -v c:/users/me/.aws:/root/.aws --env AWS_PAGER="" amazon/aws-cli

Ryan
  • 4,594
  • 1
  • 32
  • 35
steven87vt
  • 500
  • 7
  • 6
3

Inside your ~/.aws/config file, add:

cli_pager=

OutNSpace
  • 373
  • 2
  • 8
  • nice approach! works for me. it should belong to some profile, which i missed in the first step https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html – mimin0 Jan 17 '23 at 09:38