51

I am learning the AWS CLI (v2) and have installed it on Ubuntu 18.04. I am running zsh with oh-my-zsh installed. I am trying to get aws command output to be reported back to the terminal as a JSON string (or even as text), but the output is always redirected to vi.

My AWS account is brand new - no EC2 instances. When I run the following command:

aws ec2 describe-instances

It sends the expected output value (e.g. { "Reservations": [] }), but directly to vi instead of outputting a JSON string to the terminal requiring closing vi afterwards. This occurs regardless of output format (json, text, table) or what shell I use (bash, zsh).

I am not sure if this is a AWS CLI configuration issue/change or a shell/Linux configuration issue/change

  • I've reviewed my .zshrc, .bashrc, .bash_profile and .bash_aliases files and have not seen any obvious solution here that would change or redirect output.
  • I've been scouring the AWS CLI documentation, Stack Overflow and Google and I have not found a fix or a similar case.
desertnaut
  • 57,590
  • 26
  • 140
  • 166
moulder
  • 1,857
  • 2
  • 12
  • 8
  • 6
    I'm probably wrong, but I have a feeling it's related to PAGER/MANPAGER environment variables - see if any of the troubleshooting in https://github.com/aws/aws-cli/issues/237 gives you any hints – Sorin Feb 22 '20 at 00:55
  • 1
    Yup - this appears to be it and I was mistaking less for vi - /facepalm. @Sorin - if you want to make this an answer, I'll look to mark it as the accepted answer – moulder Feb 22 '20 at 03:24
  • glad I could help. I'm sorry, it was just a stab in the dark, I don't know to much about aws cli, It just seemed like a similar issue I had a few months ago, I can't write up an answer. But you might. – Sorin Feb 22 '20 at 07:06
  • Good stuff. saved me quite a few hours of head scratching. setting the cli_pager= in the ~/.aws/config worked for me! thanks! – miniGweek Nov 18 '21 at 18:49
  • https://docs.aws.amazon.com/cli/latest/userguide/cliv2-migration-changes.html#cliv2-migration-output-pager – Jason Nov 17 '22 at 20:09

5 Answers5

123

It was the PAGER environment variable set to "less" (which I was confusing with vi).

This fix is to update the ~/.aws/config file and setting cli_pager to an empty value, e.g. :

[default]
region = us-west-2
output = json
cli_pager =

Thank you to Sorin who commented on my question lead me to the answer.

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
moulder
  • 1,857
  • 2
  • 12
  • 8
  • 6
    For me, `cli_pager=` didn't work, but `PAGER=` – Fabian Nov 27 '20 at 20:21
  • 4
    Link to relevant documentation: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-output.html#cli-usage-output-pager – Joshua Hansen Dec 16 '20 at 22:55
  • 2
    `cli_pager` or `PAGER` didn't work for me. I had to the `AWS_PAGER=` env var instead. Using aws-cli version: `aws-cli/2.0.50 Python/3.8.5 Darwin/19.6.0 source/x86_64` – visokoo Jan 09 '21 at 01:29
  • 7
    There is also the CLI parameter `--no-cli-pager` https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-pagination.html#cli-usage-pagination-clientside – heypano Mar 13 '21 at 15:53
  • https://docs.aws.amazon.com/cli/latest/userguide/cliv2-migration-changes.html#cliv2-migration-output-pager – Jason Nov 17 '22 at 20:09
34

Just run your command with --no-cli-pager like so

aws ec2 describe-instances --no-cli-pager

nacholibre
  • 3,874
  • 3
  • 32
  • 35
16

You can also run a one liner command:

aws configure set cli_pager ""
sufinawaz
  • 3,623
  • 1
  • 25
  • 24
  • This is what really worked for me for some reason it doesn't work at default level it needs to be added separately to each profile using AWS CLI 2.7.9 on MAC M1 – Rohit Salecha Jun 26 '22 at 16:08
0

in V2:

https://docs.aws.amazon.com/cli/latest/userguide/cliv2-migration-changes.html#cliv2-migration-output-pager

By default, the AWS CLI version 2 returns all output through your operating system’s default pager program. This program is the less program on Linux or macOS, and the more program on Windows. This can help you navigate a large amount of output from a service by displaying that output one page at a time.

Jason
  • 2,451
  • 2
  • 23
  • 31
-4

There is a document on AWS with the title Controlling Command Output from the AWS CLI. According to this doc, the correct syntax should be

aws ec2 describe-instances --output json
kub7
  • 1
  • 2
  • 2
    In his text he mentions that "regardless of the output format" so we must assume that he has formatted his command properly. Doesn't hurt to ask, but I don't think this helps fix his issue. The output format appears to be JSON still, it's just throwing it into VI for some reason. :P – Farley Feb 21 '20 at 22:56