5 Tips for Getting More Out of the AWS CLI

5 Tips for Getting More Out of the AWS CLI Image source

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.

How to Use the AWS CLI

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.

How to download and install the AWS CLI on Windows

  1. Download the 64-bit installer or 32-bit installer and run it.
  2. Confirm the installation by running aws -version in the cmd command prompt.
  3. Run aws configure to provide your access AWS Access Key ID, AWS Secret Access Key, and Amazon region.

How to download and install the AWS CLI on Mac or Linux

  • Ensure you have Python 2.6.3 or later installed. Check by running $ python –version
  • Run the following commands:
$ curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
$ unzip awscli-bundle.zip
$ sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
  • Edit the ~/.aws/config file and add your Access Key ID, Secret Access Key, and region.
  • Protect the config file by running chmod 600 ~/.aws/config

Basic command structure

Most AWS commands have the following structure:

aws <command> <subcommand> [options]

You can see the full documentation for details of all commands.

AWS Command Line Interface Tricks and Shortcuts

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.

Drilling down for help

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.

5 Super Useful AWS CLI Commands

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.

Delete an S3 bucket and all its contents

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:

aws s3 rb s3://your-bucket –force

Display subsets of all available ec2 images

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:

aws ec2 describe-images | grep ubuntu

List users in a different format

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:

aws iam list-users –output table

Move S3 bucket to a different location

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.

aws s3 sync s3://old-bucket s3://new-bucket --source-region us-west-1 --region us-west-2

List all of your instances that are currently stopped and the reason for the stop

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.

aws ec2 describe-instances --filters Name=instance-state-name,Values=stopped --region us-west-2 --output json | jq -r .Reservations[].Instances[].StateReason.Message

Conclusion

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.

See Also




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!

Recent Tweets