Heesung Yang

How to install and configure AWS CLI (Things to know when using AWS CLI)

Installation

There are two versions of AWS CLI. Of course, version 2 is the latest version, so let’s install version 2. For installation, refer to the official AWS guide. It’s very simple.

After installation, you can enter the follwinng command in the terminal to check whether it is installed properly and which version is installed.

aws --version
# Windows 10
aws-cli/2.2.39 Python/3.8.8 Windows/10 exe/AMD64 prompt/off

# Mac
aws-cli/2.1.10 Python/3.9.1 Darwin/20.6.0 source/x86_64 prompt/off

# Linux
aws-cli/2.0.13 Python/3.7.3 Linux/3.10.0-1160.21.1.el7.x86_64 botocore/2.0.0dev17

Configuration

Set the Access Key and Secret Access Key that are created when creating an AWS account in the AWS CLI. Region and output format are also configurable.

~$ aws configure
AWS Access Key ID [None]: YOUR_ACCESS_KEY
AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY
Default region name [None]: ap-northeast-2
Default output format [None]: json

region name : refer to AWS Region List

output format : refer to AWS CLI Output Format Sample

After configure the AWS CLI, config and credentials files are created under the $HOME directory.

The $HOME path depends on the OS as shown below. (Assuming that the user name is hsyang)

Windows : C:\Users\hsyang\.aws

Mac : /Users/hsyang/.aws

Linux : /home/hsyang/.aws

  • $HOME/.aws/config

    [default]
    region = ap-northeast-2
    output = json
    
  • $HOME/.aws/credentials

    [default]
    aws_access_key_id = YOUR_ACCESS_KEY
    aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
    

In the above example, [default] is the name of the profile. AWS CLI has the concept of profile. Access Key/Secret Access Key can be set for each profile, and profile can be selected when executing commands.

Let’s add a profile named dev.

~$ aws configure --profile dev
AWS Access Key ID [None]: YOUR_ACCESS_KEY_FOR_DEV
AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY_FOR_DEV
Default region name [None]: us-east-1
Default output format [None]: table

Then config/credentials files will be changed as below.

  • $HOME/.aws/config

    [default]
    region = ap-northeast-2
    output = json
    [profile dev]
    region = us-east-1
    output = table
    
  • $HOME/.aws/credentials

    [default]
    aws_access_key_id = YOUR_ACCESS_KEY
    aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
    [dev]
    aws_access_key_id = YOUR_ACCESS_KEY_FOR_DEV
    aws_secret_access_key = YOUR_SECRET_ACCESS_KEY_FOR_DEV
    

If you don’t know what values should be set for the region and output in the first place, you can skip it. Because you can set it when you run the command.

The following is an example of setting the profile, region, and output options when executing the command.

# The default profile is used
~$ aws ec2 describe-instances

## Change only the region of the default profile
~$ aws ec2 describe-instances --region us-east-1

## Change only the output format for the default profile
~$ aws ec2 describe-instances --output text

## Use the dev profile
~$ aws ec2 describe-instances --profile dev

## Use the dev profile but change the region and output format of the dev profile
~$ aws ec2 describe-instances --profile dev --region eu-central-1 --output yaml

## Change the region of the dev profile
~$ aws configure --profile dev
AWS Access Key ID [****************_DEV]:          # Press Enter to keep the existing value
AWS Secret Access Key [****************_DEV]:      # Press Enter to keep the existing value
Default region name [us-east-1]: ap-northeast-2    # us-east-1 => ap-northeast-2
Default output format [table]:                     # Press Enter to keep the existing value

Appendix

AWS Region List

Name Region Country
Seoul ap-northeast-2 Asia
Mumbai ap-south-1 Asia
Osaka ap-northeast-3 Asia
Singapore ap-southeast-1 Asia
Sydney ap-southeast-2 Asia
Tokyo ap-northeast-1 Asia
Hongkong ap-east-1 Asia
Ohio us-east-2 US
Virginia us-east-1 US
Califonia us-west-1 US
Oregon us-west-2 US
Sanpaulo sa-east-1 South America
Canada ca-central-1 Canada
Frankfurut eu-central-1 Europe
Island eu-west-1 Europe
London eu-west-2 Europe
Paris eu-west-3 Europe
Stockholm eu-north-1 Europe

AWS CLI Output Format Sample

The following is an example of the execution result of the aws iam list-groups command.

json

aws iam list-groups --output json
{
    "Groups": [
        {
            "Path": "/",
            "GroupName": "Administrator",
            "GroupId": "GROUP_ID_1",
            "Arn": "arn:aws:iam::YOUR_ACCOUNT_ID:group/Administrator",
            "CreateDate": "2021-01-22T04:34:11+00:00"
        },
        {
            "Path": "/",
            "GroupName": "Operator",
            "GroupId": "GROUP_ID_2",
            "Arn": "arn:aws:iam::YOUR_ACCOUNT_ID:group/Operator",
            "CreateDate": "2021-02-04T06:39:10+00:00"
        },
        {
            "Path": "/",
            "GroupName": "SysAdministrator",
            "GroupId": "GROUP_ID_3",
            "Arn": "arn:aws:iam::YOUR_ACCOUNT_ID:group/SysAdministrator",
            "CreateDate": "2021-01-22T04:34:43+00:00"
        }
    ]
}

table

aws iam list-groups --output table
----------------------------------------------------------------------------------------------------------------------------------
|                                                               ListGroups                                                       |
+--------------------------------------------------------------------------------------------------------------------------------+
||                                                                Groups                                                        ||
|+------------------------------------------------------+----------------------------+-------------+-------------------+--------+|
||                        Arn                           |        CreateDate          |  GroupId    |     GroupName     | Path   ||
|+------------------------------------------------------+----------------------------+-------------+-------------------+--------+|
||  arn:aws:iam::YOUR_ACCOUNT_ID:group/Administrator    |  2021-01-22T04:34:11+00:00 |  GROUP_ID_1 |  Administrator    |  /     ||
||  arn:aws:iam::YOUR_ACCOUNT_ID:group/Operator         |  2021-02-04T06:39:10+00:00 |  GROUP_ID_2 |  Operator         |  /     ||
||  arn:aws:iam::YOUR_ACCOUNT_ID:group/SysAdministrator |  2021-01-22T04:34:43+00:00 |  GROUP_ID_3 |  SysAdministrator |  /     ||
|+------------------------------------------------------+----------------------------+-------------+-------------------+--------+|

yaml

aws iam list-groups --output yaml
Groups:
- Arn: arn:aws:iam::YOUR_ACCOUNT_ID:group/Administrator
  CreateDate: '2021-01-22T04:34:11+00:00'
  GroupId: GROUP_ID_1
  GroupName: Administrator
  Path: /
- Arn: arn:aws:iam::YOUR_ACCOUNT_ID:group/Operator
  CreateDate: '2021-02-04T06:39:10+00:00'
  GroupId: GROUP_ID_2
  GroupName: Operator
  Path: /
- Arn: arn:aws:iam::YOUR_ACCOUNT_ID:group/SysAdministrator
  CreateDate: '2021-01-22T04:34:43+00:00'
  GroupId: GROUP_ID_3
  GroupName: SysAdministrator
  Path: /

text

aws iam list-groups --output text
GROUPS  arn:aws:iam::YOUR_ACCOUNT_ID:group/Administrator     2021-01-22T04:34:11+00:00  GROUP_ID_1  Administrator     /
GROUPS  arn:aws:iam::YOUR_ACCOUNT_ID:group/Operator          2021-02-04T06:39:10+00:00  GROUP_ID_2  Operator          /
GROUPS  arn:aws:iam::YOUR_ACCOUNT_ID:group/SysAdministrator  2021-01-22T04:34:43+00:00  GROUP_ID_3  SysAdministrator  /