• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • AWS Command Line Interface or CLI is a set of open-source tools that allows you to manage and automate AWS services using commands.

    AWS CLI supports multiple platforms like Windows, Linux, and macOS and provides direct access to public APIs for AWS. Further, automation of tedious infrastructure tasks is easily possible in a CLI environment.

    AWS CLI Versions

    AWS CLI has two major versions available, i.e., version 1.x, which is available for backward compatibility while version 2.x is the current general release available and is intended for production use. It’s not completely backward compatible and hence scripts that use version 1 of AWS CLI may not be compatible with version 2.

    Install AWS CLI

    Here we’ll be covering the installation of AWS CLI on different platforms. Since version 2.x is the general release available at the time of this writing we’ll be only covering the installation of the same.

    Linux

    The official method of installation of AWS CLI on Linux-based systems is available via a ZIP package. It requires a 64 bit/ARM Linux system and assumes that the system already has unzip, glibc, groff and less packages installed and available for use. All major Linux distributions are supported like CentOS, Fedora, Ubuntu, and Amazon Linux.

    To install the latest version of AWS CLI on Linux x86 (64-bit), use the following commands:

    $ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    unzip awscliv2.zip
    sudo ./aws/install

    To confirm successful installation, run:

    $ aws --version
    aws-cli/2.2.5 Python/3.8.8 Linux/4.14.133-113.105.amzn2.x86_64 botocore/2.0.0

    macOS

    macOS installation of AWS CLI is officially offered in GUI as well CLI methods. To go the GUI way, download the latest version of the AWS CLI package from the following link: https://awscli.amazonaws.com/AWSCLIV2.pkg and double click the downloaded package to launch the installer.

    Follow the on-screen instructions to complete the installation. Most of the options can be left as default for normal installation requirements.

    If you want to install using CLI way, on a new terminal issue below commands to download and install the latest AWS CLI version:

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

    sudo in above command is needed if you plan to install it system-wide. If you want to install as a local user without admin rights, there’s a simple official workaround.

    You need to create a XML file that will provide a path /Users/myusername where you want AWS CLI to be installed. This path should be an existing directory or created before launching the installer else it’ll fail.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <array>
        <dict>
          <key>choiceAttribute</key>
          <string>customLocation</string>
          <key>attributeSetting</key>
          <string>/Users/myusername</string>
          <key>choiceIdentifier</key>
          <string>default</string>
        </dict>
      </array>
    </plist>

    Then you can proceed to download the latest installation package using curl as:

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

    Once you have the package and the XML configuration file ready, AWS CLI can be installed for the local user using:

    $ installer -pkg AWSCLIV2.pkg \
                -target CurrentUserHomeDirectory \
                -applyChoiceChangesXML choices.xml

    Here AWSCLIV2.pkg is the package that we downloaded in an earlier step using curl and -target specifies that installation is for local user and -applyChoiceChangesXML basically indicates to the installer that we’ll be using custom choices that contain our relative installation path /Users/myusername or whatever the local directory path has been specified in the XML.

    As the last step, run the following commands to create the required symlinks which are required by AWS CLI to work correctly.

    $ sudo ln -s /folder/installed/aws-cli/aws /usr/local/bin/aws
    $ sudo ln -s /folder/installed/aws-cli/aws_completer /usr/local/bin/aws_completer

    If your $PATH includes a directory that you can write to and you specify that directory as the target’s path, you can run the above command without sudo. But if you don’t have a writable directory in your $PATH, you must use sudo for permissions to write to the specified target folder. The default location for a symlink is /usr/local/bin/.

    To verify the install, use:

    $ which aws
    /usr/local/bin/aws 
    $ aws --version
    aws-cli/2.1.29 Python/3.7.4 Darwin/18.7.0 botocore/2.0.0

    Windows

    For a Windows install, AWS CLI v2 requires a Windows 64-bit version or later and admin rights for installation.

    The latest Windows installation package can be downloaded from: https://awscli.amazonaws.com/AWSCLIV2.msi and launched with admin rights for installation to complete.

    Alternatively, you can use misexec in CLI to install on the command line as:

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

    You can use aws --version in a command prompt to verify the install.

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

    Docker

    Finally, if you don’t want to install AWS CLI on your local OS and happen to have access to Docker, it is just a matter of single command to execute AWS CLI commands as:

    $ docker run --rm -it amazon/aws-cli command

    where command is the AWS CLI command to execute. The above command will launch a container with the required AWS CLI setup and will directly proceed to execute the specified command and exit once it’s done.

    There’s a bunch of options to use your existing AWS CLI credentials, configuration, and environment variables as an input for the container that makes it all the more usable and portable. There’s also an alias option to shorten the long docker command to something simple and comprehensible like for Linux host use:

    $ alias aws='docker run --rm -it amazon/aws-cli'

    Make sure to add the above command in your shell’s profile file for persistence. Once done, you can use it like:

    $ aws --version
    aws-cli/2.1.29 Python/3.7.3 Linux/4.9.184-linuxkit botocore/2.0.0dev10

    Easy and looks better. Read more about AWS CLI Docker-based offering and possibilities here.

    Summary

    AWS CLI offers a command-based experience to manage your AWS infrastructure with ease of control especially with repetitive tasks and ultimately opens the door for automation and managing Infrastructure as Code (IaC). You learned about how to install it on different platforms. As a next step, you can read here on how to configure it to use it with your AWS account.