AWS command line interface (CLI) enables you to control your AWS environment from your local client terminal. You can use CLI to configure, monitor, and automate resource management. This article provides a walkthrough that explains how to use AWS CLI. You will also learn five very useful AWS CLI commands.
The AWS command line interface (CLI) is a tool provided by Amazon to help you manage your resources and services. It enables you to control your environment from the terminal of your local client. You can use it to change configurations, monitor assets, and automate the management of resources through scripting.
Most AWS commands have the following structure:
You can see the full documentation for details of all commands.
When getting started with the AWS CLI, there are a variety of tricks and shortcuts you can use to increase your productivity. Below are a few that are particularly useful.
Your cloud environment is made of a collection of services. Each service has a different set of options and commands that can be difficult to remember. Rather than having to search through the documentation to find these options and commands, you can use the help command built into the AWS CLI.
To access this command, you need to type aws help. This pulls up a list of globally available commands. You can refine this command by inserting the service name for more specific options. For example, if you’re trying to manage your EBS volumes but have forgotten which commands you need, you can aws ebs help –.
Once you find the command you need, you can even drill down further by inserting the command name after the service name. For example, aws ebs describe-service help. This enables you to quickly access only the information you need. Alternatively, you can use aws help topics to view a full list of your available options.
Setting up auto-complete for commands Built-in to the AWS CLI is an auto-complete feature for commands. This feature helps speed your CLI use by predicting which commands you are typing and enabling you to quickly insert text with the Tab key.
This feature is preconfigured for Amazon Linux but may require some configuration if you are using another OS. For example, on non-Linux platforms, developers must define the location of the AWS_Completer script and the name of the shell in use.
To accomplish this, you can use the echo $SHELL command. This will tell you which shell you’re using if you aren’t sure. Then, you can use the Which AWS_Completer command to find the location of your script.
Create command templates with skeletons One practice when working with collections of complex commands is to create text files containing those commands. These can be shared amongst teams and referenced as needed. However, these files can be a pain to modify or update.
As an alternative, the AWS CLI includes utilities that you can use to generate a skeleton. Skeletons enable you to modify input parameters for complex commands using JSON files as opposed to scripts. To create a skeleton, you can use the command –generate-cli-skeleton.
Another option is to implement chains of commands from the CLI. This tool enables you to combine commands to be run sequentially or to drive future commands with the output of the current one.
Now that you understand a few of the tricks and shortcuts available to you, it may be helpful to see some commands in action. Below are some of the most commonly used commands and ones you may want to remember.
S3 buckets can grow to massive sizes, with thousands of files contained within. While you can individually delete files, this is time-consuming and unnecessary if you know the bucket is unneeded.
Rather than going through one by one, you can duplicate any files you may need and then delete the bucket as a whole. The command to accomplish this is:
When managing EC2 instances, you may be unsure which images you are using. Rather than individually checking, you can call up a description of all available images. You can use this command on its own, or you can filter the images by type. For example, the following command defines available images built on Ubuntu as called from a Mac or Linux machine:
When viewing lists of users, especially long lists, the format can make interpretation and use a challenge. When displaying these lists, the format depends on the default you have set. However, you may need that format for other processes while manual list calls are infrequent.
Rather than manually reformatting lists or temporarily modifying defaults, you can output lists in a table format. The following command can help you achieve this:
Sometimes, rather than deleting buckets what you really need is just to move the files to the appropriate location. You can do this by copying and then deleting the unnecessary duplicate but this is slow and prone to error. A better alternative is to move the bucket with the following command. This example moves a bucket from the US West 1 region to the US West 2 region.
Downtime in your cloud environment can be a nightmare, ruining productivity and causing revenue loss. When downtime occurs, you need to quickly identify which instances are affected and what caused the outage. You can accomplish this with a command like the following one.
Hopefully, this article has helped you better understand how to use AWS CLI. By now, you should be able to download and install AWS CLI on Windows, Mac, or Linux. Remember that whenever you need a reference, you can frill down for help, rather than go look for the documentation. You can also set up auto-complete for commands, and create command templates with skeletons. These three tricks, if implemented continually, can save you a lot of time and effort as you scale up your operations.