Formatting Output in the AWS CLI with Amazon Q Developer
Note: This content was originally published on the AWS Blog and is reproduced here for archival purposes. Please visit the original post for the most up-to-date version.
Welcome to another installment of Q-Bits, our regular series showcasing cool ways Amazon employees are leveraging Amazon Q Developer. Today, we're diving into how Q Developer can assist with Formatting Output in the AWS CLI.
The AWS Command Line Interface (CLI) provides a powerful set of tools for interacting with Amazon Web Services (AWS) resources. One of the key capabilities of the AWS CLI is the ability to filter, query, and format the output of commands. This allows you to extract the specific information you need from the vast amount of data returned by AWS APIs.
However, the syntax for these filtering and querying capabilities can be quite complex and confusing, especially for new users. Fortunately, Amazon Q Developer for command line can help simplify this process.
Amazon Q Developer for command line provides natural language translation of instructions into executable AWS CLI commands. Instead of having to remember the exact syntax for filtering and querying AWS CLI output, you can simply describe what you want to do in plain English, and Amazon Q Developer will generate the appropriate AWS CLI command for you.
Let's take a look at a few examples of how this works. Suppose I want to list all of my Amazon Elastic Compute Cloud (Amazon EC2) instances, and display the instance ID, name tag, public IP address, and private IP address in a table format. With Amazon Q Developer, I can simply type:
1
# list ec2 instances with id, name, public and private ip address in a table
When I hit enter, Amazon Q Developer will translate my natural language instruction into the appropriate AWS CLI command.
1
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, Tags[?Key==`Name`].Value | [0], PublicIpAddress, PrivateIpAddress]' --output table
I can review the command and choose to edit the suggestion or execute it. Here is the output for my demo account. Note that I redacted a few values.
1
2
3
4
5
6
7
------------------------------------------------------------------------------
| DescribeInstances |
+----------------------+-------------------+----------------+----------------+
| i-xxxxxxxxxxxxxxxxx | EKS Worker Node | 35.173.xxx.xx | 172.31.8.xxx |
| i-yyyyyyyyyyyyyyyyy | Web Server 01 | 3.85.yyy.yyy | 172.31.27.yy |
| i-zzzzzzzzzzzzzzzzz | Model Hosting | None | 172.31.11.zz |
+----------------------+-------------------+----------------+----------------+
I can also use Amazon Q Developer to sort the output by a specific field, such as instance type:
1
list ec2 instances with id, and instance type sorted by instance type as text
Again, Q will translate this into a CLI command.
1
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,InstanceType]' --output text | sort -k2
If I choose to execute the command, the result properly sorts the output by the instance type.
1
2
3
i-zzzzzzzzzzzzzzzzz g5.8xlarge
i-yyyyyyyyyyyyyyyyy t2.micro
i-xxxxxxxxxxxxxxxxx t3.medium
Finally, I can use Amazon Q Developer to filter the output based on tags. For example, to list the name tags of all EC2 instances that have a "Team=Marketing" tag:
1
# list the Name tag of ec2 instances that are tagged with Team=Marketing
Amazon Q Developer takes care of constructing the appropriate AWS CLI filters and queries, like this.
1
aws ec2 describe-instances --filters Name=tag:Team,Values=Marketing --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text
If I choose to execute it, the CLI will return the desired information.
1
Web Server 01
The key benefits of using Amazon Q Developer with the AWS CLI are:
1. Simplified syntax: Amazon Q Developer translates natural language instructions into the correct AWS CLI syntax, eliminating the need to remember complex command structures.
2. Increased productivity: By automating the generation of AWS CLI commands, Amazon Q Developer can save you time and reduce the risk of errors when working with the CLI.
3. Enhanced readability: The natural language instructions provided to Amazon Q Developer are much more readable and intuitive than the underlying AWS CLI syntax.
Whether you're a seasoned AWS CLI user or just starting out, Amazon Q Developer can be a valuable tool for filtering, querying, and formatting the output of your AWS CLI commands. By abstracting away the complexity of the CLI syntax, Amazon Q Developer empowers you to focus on the task at hand rather than the mechanics of the command line.