Contents
How to Use the AWS Command Line Interface
This course introduces you to the AWS Command Line Interface.
Learning Objectives
- Install and configure the AWS Command Line Interface
- Manage CLI credential profiles
- Structure CLI commands
- Better control the outputted response data
- Use input functionality to make working with the CLI easier
Intended Audience
- Those looking to get more familiar with the AWS Command Line Interface
Prerequisites
- Have a good understanding of command line basics
- Understand some of the fundamental AWS services, such as Amazon EC2, Amazon DynamoDB, Amazon S3, and IAM
- Familiarity with JSON and YAML will help to better understand some of the practical parts of the course.
- For more information on some of these services, check out the following courseware titled:
In your default profile, you choose the default output for your commands. For example, if I run aws configure, and press enter until I get to output, I can then see that my default profile uses YAML as its default output.
For the output format, you can choose between 5 options: json, yaml, yaml-stream, text, and table. Generally, the JSON and YAML formats are best when working programmatically, as it interfaces well with programming languages. Text is best for scripting languages, table is best for humans looking to read this data, and YAML-stream is best for large data sets, as it provides a faster and more responsive output.
Let’s take a look at a few of these outputs.
I’ll first type in the command “aws ec2 describe-instances” to compare outputs. This command will list all instances in my default region, which is us-west-2. When I press enter, it provides the response in my chosen default format, which is YAML.
Now, let’s look at what happens when I specify the output. I’ll press q to exit out of the current response and clear my screen. Then, I’ll type in the same command, aws ec2 describe-instances and then I’ll use the –output parameter to change the output to JSON.
When I press enter, I can now see the output is in JSON. I can tell, because of these pesky little brackets. I won’t go through the rest of the outputs, but I encourage you to pause the video and change the output for this command to see the differences in all 5 of the formats.
Okay, welcome back. As you can see regardless of the output format, this command aws ec2 describe-instances spits out quite a bit of data. And if I only need specific details about my instances, then there may be a lot of unnecessary information here.
To reduce the amount of returned data, I can use the query argument by specifying –query in my command. The Query argument uses the JMESPath syntax which is a querying language for JSON. Here’s an example of what that looks like.
Let’s say I want to use the aws ec2 describe-instances command, but I only want to return the instance ID for every instance in this region. To do this, I can attach the –query parameter, and then I can use JMESPath syntax to say for all my reservations for all my ec2 instances I want just the Instance ID. It then returns a nice list of all the InstanceIds in my Region, so I don’t have to hunt through tons of returned data.
I can even attach –output to this command, and choose the table output to make this a bit more human-readable.
I can continue adding on to this command and return both the instanceId, and the Availability Zone of the instance if I’d like. First I’ll clear my screen, and then I’ll run the aws ec2 describe-instances command, and then I’ll look for Availability Zone. I can see that the AZ is one of three attributes under Placement. So when I select this attribute, I’ll refer to it as Placement.AvailabilityZone.
So let’s do this. I’ll use the same command as before, aws ec2 describe-instances –query ‘Reservations[*].Instance[*]. and then choose the fields I want. Since I’m selecting multiple fields, I’ll use curly braces this time and type in InstanceId.
Then, I’ll add a comma after InstanceId, and specify Placement.AvailabilityZone. I can also label each of these fields, so in front of instance-id, I can label this id: and in front of Placement.AvailabilityZone, I can label it as AZ:. I also want to update my output to be table. All right, and now you can see my table shows the ID and the AZ, each with column headers.
While query can help list fields you want returned, you can additionally filter data as well and reduce the amount of results you get back by specifying which resources you want to see.
For example, maybe I only want to return a list of instances that are in the us-west-2a Availability Zone. One thing to note is that the syntax for filters is a bit different. I usually go into the CLI reference to see what the list of filter options are. Here in the documentation I can see availability-zone is what I’m looking for. So, I’ll run the aws ec2 describe-instances command again and this time use the –filter command. This command starts with the quotes and then a Name field, and a Values field.
In this case, the Name will equal availability-zone, which we got from the filter options in the documentation, and the Values will equal us-west-2a.
So this limits my response down to the two instances that are in us-west-2a. If I wanted to go further, I can then query on this information. I’ll use the same command I just ran, then add on –query and say for all reservations, for all instances, I want the instance ID. Then it returns the instance ID for the two instances in us-west-2a.
That’s it for this one! See you next time.
Alana Layton is an experienced technical trainer, technical content developer, and cloud engineer living out of Seattle, Washington. Her career has included teaching about AWS all over the world, creating AWS content that is fun, and working in consulting. She currently holds six AWS certifications. Outside of Cloud Academy, you can find her testing her knowledge in bar trivia, reading, or training for a marathon.