Introduction

This article provides instructions for using the AWS Command Line Interface (CLI) to query multiple fields. The AWS CLI is a powerful tool that allows developers and system administrators to interact with various AWS services easily.

We will show you how to construct AWS CLI commands that retrieve the desired fields from AWS services. We will also introduce you to JMESPath, a powerful query language used with the AWS CLI, that allows you to filter and manipulate JSON data effectively.

Prerequisites

Before diving into querying multiple fields, ensure that you have the following prerequisites:

  • AWS CLI is installed and configured on your local machine.
  • Familiarity with basic AWS CLI commands and syntax.
  • Access to the AWS services you intend to query, with the necessary permissions.

Querying Multiple Fields with AWS CLI

  1. Start by determining the AWS service and resource type you want to query. Familiarize yourself with the available options for filtering based on your specific service and resource type. Refer to the AWS CLI documentation or service-specific guides for detailed information.
  2. Once you have identified the service and resource type, construct the AWS CLI command to retrieve the desired fields. The basic structure of the AWS CLI command is as follows:
aws <service> <operation> --query 'FieldsToRetrieve'
  1. Replace <service> with the AWS service, you are querying, <operation> with the appropriate action (e.g., describe-instances for EC2), and FieldsToRetrieve with the specific fields you want to retrieve. Use valid JMESPath expressions to define the fields you need. JMESPath is a powerful query language that allows you to filter and manipulate JSON data.
  2. Execute the constructed AWS CLI command in your terminal. The output will display the requested fields for the specified resource.
  3. To query multiple fields, separate them with a comma (,) within the `‘FieldsToRetrieve’`.

Examples

Retrieve specific information from the EC2 service:

aws ec2 describe-instances --query 'Reservations[].Instances[].{InstanceID:InstanceId, State:State.Name, Type:InstanceType}'

In this command, we use the describe-instances operation within the ec2 service to retrieve the InstanceId, State, and InstanceType fields. The command constructs and executes a query using JMESPath expressions.

Example output for this command:

[
    {
        "InstanceID": "i-0a535949d2a7c51be",
        "State": "running",
        "Type": "t2.medium"
    },
    {
        "InstanceID": "i-0a4250b8fd6d22d53",
        "State": "running",
        "Type": "t2.medium"
    },
    ...
]

Querying multiple fields from Amazon S3:

aws s3api list-objects --bucket my-bucket --query 'Contents[].{Key:Key, Size:Size, LastModified:LastModified}'

In this command, we use the list-objects operation within the s3api service to retrieve the Key, Size, and LastModified fields from the specified bucket.

Example output for this command:

[
    {
        "Key": "050d2a8d-abe8-4790-86bb-7d5a4b3ead08.csv",
        "Size": 226,
        "LastModified": "2023-03-22T18:03:40+00:00"
    },
...
]

Using Logical Operators (OR / AND)

In addition to retrieving specific fields, you can also use logical operators to construct more complex queries when querying multiple fields with the AWS CLI. The two commonly used logical operators are the “OR” operator (||) and the “AND” operator (&&).

OR Operator (||)

The OR operator allows you to specify multiple conditions and retrieve fields that satisfy any of those conditions. To use the OR operator, separate the conditions with the || symbol within the --query parameter.

Here’s an example that demonstrates how to use the OR operator:

aws ec2 describe-instances --query \
"Reservations[].Instances[?State.Name=='running'||State.Name=='stopped'].[InstanceId, State.Name]"

In this example, we query the EC2 service and retrieve instances that are in either the “running” or “stopped” state. The expression State.Name==running|State.Name==stopped specifies the conditions using the OR operator. We retrieved the InstanceId and State.Name fields for these instances.

Example output:

[
    [
        [
            "i-0a535949d2a7c51be",
            "running"
        ]
    ],
....
]

AND Operator (&&)

The AND operator allows you to combine multiple conditions and retrieve fields that satisfy all of those conditions simultaneously. To use the AND operator, separate the conditions with the & symbol within the --query parameter.

Here’s an example that demonstrates how to use the AND operator:

aws ec2 describe-instances --query \
"Reservations[].Instances[?State.Name=='running'&&InstanceType=='t2.medium'].[InstanceId, State.Name, InstanceType]"

In this example, we query the EC2 service and retrieve instances that are in the running state and have an instance type of t2.medium. The expression State.Name=='running'&&InstanceType=='t2.medium' specifies the conditions using the AND operator. We retrieve the InstanceId, State.Name, and InstanceType fields for these instances.

Combining OR and AND Operators

You can combine the OR and AND operators to create more complex queries. By using parentheses ( and ) to group conditions, you can control the order of evaluation.

Here’s an example that demonstrates combining the OR and AND operators:

aws ec2 describe-instances --query \
"Reservations[].Instances[?(State.Name=='running'&&(InstanceType=='t2.small'||InstanceType=='t2.medium'))].[InstanceId, State.Name, InstanceType]"

In this example, we query the EC2 service and retrieve instances that are in the “running” state and have either an instance type of “t2.medium” or “t2.small”. The expression includes both AND and OR operators to create the desired conditions. We retrieve the InstanceId, State.Name, and InstanceType fields for these instances.

Example output:

[
    {
        "InstanceId": "i-0a535949d2a7c51be",
        "State": "running",
        "InstanceType": "t2.small"
    },
    {
        "InstanceId": "i-0a4250b8fd6d22d53",
        "State": "running",
        "InstanceType": "t2.medium"
    },
    ...
]

By using logical operators in combination, you can create more specific and targeted queries to retrieve the exact data you need from AWS services. Experiment with different combinations of fields and filters to tailor the output to your requirements. Remember to consult the AWS CLI documentation or service-specific guides for further details on available options and query syntax.

Conclusion

In this article, we have provided step-by-step instructions on how to utilize the AWS Command Line Interface (CLI) to query multiple fields from AWS services. By following these steps, you can effectively retrieve specific data that is relevant to your needs.

We started by familiarizing ourselves with the basic structure of an AWS CLI command for querying fields. We then introduced JMESPath, a powerful query language used with the AWS CLI, which allows you to filter and manipulate JSON data effectively.

Throughout the article, we provided examples of querying multiple fields from different AWS services, such as EC2 and S3, demonstrating how to construct queries and retrieve desired data.

It is important to note that AWS CLI documentation and service-specific guides are valuable resources for further details on available options and query syntax. With practice and familiarity, you will gain proficiency in leveraging the AWS CLI to streamline your operations within the AWS ecosystem.