Sharing is caring 🙂

The AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

AWS CLI Key Points

• The Amazon Web Services Command Line Interface is a tool that enables you to interact with AWS services using commands in the terminal or command line of the machine you choose to use. This can be Microsoft Windows, Apple macOS, or Linux.
• By using the AWS CLI, you gain direct access to the public APIs of AWS services. You lose the guidance of a graphical user interface when using this approach. Still, the benefit is jobs can be completed with the CLI faster and more efficiently if you become skilled in the command line interface.
• When using the AWS CLI, it is possible to develop scripts to manage resources running in an AWS account.
• AWS CLI is open-source and can be seen here https://github.com/aws/aws-cli
• The CLI is an excellent alternative to using AWS Management Console

AWS CLI Python

The aws-cli package is written in Python. Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general-purpose language used to create various programs and isn’t specialized for specific problems. The AWS CLI works with the following versions of Python:

  • 3.7.x and greater
  • 3.8.x and greater
  • 3.9.x and greater
  • 3.10.x and greater
  • 3.11.x and greater

AWS CLI is built on the AWS SDK for Python. There are several SDKs available to use with AWS.

JavaScript – The AWS SDK for JavaScript simplifies using AWS Services by providing a set of consistent and familiar libraries for JavaScript developers. It supports API lifecycle considerations such as credential management, retries, data marshaling, serialization, and deserialization.

Java – The AWS SDK for Java simplifies using AWS Services by providing a set of consistent and familiar libraries for Java developers. It supports API lifecycle considerations such as credential management, retries, data marshaling, and serialization.

Go – The AWS SDK for Go provides APIs and utilities that developers can use to build Go applications that use AWS services, such as Amazon Elastic Compute Cloud (Amazon EC2) and Amazon Simple Storage Service (Amazon S3). The SDK removes the complexity of coding directly against a web service interface.

PHP – The PHP SDK is a modern, open-source PHP library that makes it easy to integrate your PHP application with AWS services like Amazon S3, Amazon Glacier, and Amazon DynamoDB.

.NET – AWS SDK for .NET simplifies use of AWS Services by providing a set of libraries that are consistent and familiar for .NET developers. All AWS SDKs provide support for API lifecycle considerations such as credential management, retries, data marshaling, and serialization.

Ruby – The Ruby SDK helps take the complexity out of coding by providing Ruby classes for many AWS services, including Amazon S3, Amazon EC2, DynamoDB, and more. The SDK is provided as individual downloadable packages for each AWS service, which include code and documentation.

C++ – The C++ SDK is a modern, open-source C++ library that makes it easy to integrate your C++ application with AWS services like Amazon S3, Amazon Kinesis, and Amazon DynamoDB.

Install AWS CLI

How To Install AWS CLI On Windows

To update your current installation of AWS CLI on Windows, download a new installer each time you update to overwrite previous versions. AWS CLI is updated regularly. To see when the latest version was released, see the AWS CLI version 2 Changelog on GitHub.

Download and run the AWS CLI MSI installer for Windows (64-bit):

https://awscli.amazonaws.com/AWSCLIV2.msi

Alternatively, you can run the msiexec command to run the MSI installer.

C:> msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi

For various parameters that can be used, see msiexec on the Microsoft Docs website.

To confirm the installation, open the Start menu, search for cmd to open a command prompt window, and use the aws --version command at the command prompt.

C:> aws --version
aws-cli/2.7.24 Python/3.8.8 Windows/10 exe/AMD64 prompt/off

If Windows cannot find the program, you might need to close and reopen the command prompt window to refresh the path or follow the troubleshooting in Troubleshooting AWS CLI errors.

How To Install AWS CLI On macOS

If you have sudo permissions, you can install the AWS CLI for all users on the computer.

$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
$ sudo installer -pkg AWSCLIV2.pkg -target /

Download the file using the curl command. The -o option specifies the file name that the downloaded package is written to. In this example, the file is written to AWSCLIV2.pkg in the current folder.

$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"

Run the standard macOS installer program, specifying the downloaded .pkg file as the source. Use the -pkg parameter to specify the name of the package to install and the -target / parameter for which drive to install the package to. The files are installed to /usr/local/aws-cli, and a symlink is automatically created in /usr/local/bin. You must include sudo on the command to grant write permissions to those folders.

$ sudo installer -pkg ./AWSCLIV2.pkg -target /

After installation is complete, debug logs are written to /var/log/install.log.

To verify that the shell can find and run the aws command in your $PATH, use the following commands.

$ which aws
/usr/local/bin/aws
$ aws --version
aws-cli/2.7.24 Python/3.8.8 Darwin/18.7.0 botocore/2.4.5

If the aws command cannot be found, you might need to restart your terminal.

How To Install AWS CLI On Linux

Using the pip package manager or the bundled installer, you can install the AWS Command Line Interface (AWS CLI) version 1 and its dependencies on most Linux distributions.

Although the awscli package is available in repositories for other package managers such as apt and yum, these are not produced, managed, or supported by AWS. As documented in this guide, installing the AWS CLI from only the official AWS distribution points is recommended.

Using The AWS CLI

Before you can use the AWS CLI, you need to create access keys. Access keys authenticate with AWS when you try to run commands from the AWS CLI. Within IAM in AWS, you should select an account that is not the root user. Do not use the root account to create access keys. Use access keys to send programmatic calls to AWS from the AWS CLI, AWS Tools for PowerShell, AWS SDKs, or direct AWS API calls. You can have two access keys (active or inactive) at a time.

When you create the Access key, it is exceptionally very secret. This is the only time you will see them; they can be downloaded. You cannot recover them later, but you can create new ones if you want to. You should never share these keys with anyone. If you like, you can download a CSV file to keep them on your computer, or you can show the Access key ID and the Secret access key.

Configure AWS CLI

Once you have access keys, you can configure the AWS CLI installation on your machine to use them. Therefore, commands you run with the CLI will be authenticated against your AWS account.

Enter aws configure at the command line, and then press Enter.

The AWS CLI outputs lines of text, prompting you to enter additional information.

Enter each of your access keys in turn, and then press Enter.

Then, enter an AWS Region name in the format shown, press Enter, and then press Enter a final time to skip the output format setting.

The final Enter command is shown as replaceable text because there is no user input for that line.

$ aws configure
AWS Access Key ID [None]: ANOTAREALKEY7EXAMPLE
AWS Secret Access Key [None]: l/iNrMM7CPKEbUwItPnKE/DFxERXLYfYGXAaEJEM
Default region name [None]: us-east-2
Default output format [None]: ENTER

Once these steps are completed, you can try a few commands from the cli. First, we can test the list-users command.

$ aws iam list-users
{
    "Users": [
        {
            "Path": "/",
            "UserName": "george",
            "UserId": "NOTAREALIDLATPIXAGEFL",
            "Arn": "arn:aws:iam::123456789123:user/george",
            "CreateDate": "2022-08-18T20:05:01+00:00",
            "PasswordLastUsed": "2022-10-05T19:05:51+00:00"
        }
    ]
}

How To Create a Key-Pair Using the AWS CLI

If you ever find yourself needing to sync up SSH to a new instance, you will have to begin with creating their Key-Pair combination before getting to the rest of your code. It’s often the first command needed to stand up a new instance and can be used consistently during and after a new instance has been set up. You will never know when you need to generate new access keys.

$ aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem

How to set up a security group using the AWS CLI

Every time you create a VPC or internet gateway, you need to know which access you allow into your workflow. Here is where security groups come in. You can set up and connect to a VPC in seconds to ensure you only allow access when needed.

$ aws ec2 create-security-group --group-name my-sg --description "My security group" --vpc-id vpc-1234abcd

Delete an S3 bucket and all its contents

The following command will delete a bucket and all its content, including directories.

$ aws s3 rb s3://bucket-name –force

Output As Table

$ aws iam list-users --output table
----------------------------------------------------------------
|                           ListUsers                          |
+--------------------------------------------------------------+
||                            Users                           ||
|+-------------------+----------------------------------------+|
||  Arn              |  arn:aws:iam::12345678901:user/george  ||
||  CreateDate       |  2022-08-18T20:05:01+00:00             ||
||  PasswordLastUsed |  2022-10-10T22:47:11+00:00             ||
||  Path             |  /                                     ||
||  UserId           |  NOTAREALIDLATPIXAGEFL                 ||
||  UserName         |  george                                ||
|+-------------------+----------------------------------------+|

The above commands are only the beginning when it comes to using the AWS CLI, but they have hopefully given you an idea of how useful it can be when used correctly. It’s easy to get comfortable using the AWS GUI Console for most of your work, but this article should have given you some ideas for trying out the AWS CLI to experience its benefits.

Sharing is caring 🙂