AWS CLI

The AWS CLI is an open source tool built on top of the AWS SDK for Python (Boto) that provides commands for interacting with AWS services. With minimal configuration, you can start using all of the functionality provided by the AWS Management Console from your favorite terminal program.

For installation instructions refer to the official documentation.

Advantages

  1. Easy to install
  2. Supports all Amazon Web Services
  3. Easy to use
  4. Can be incorporated in shell scripts for automation and reproducibility

Setting up your profile

Before you can start using the aws-cli you need to configure the CLI with your AWS credentials. The aws configure command is the fastest way to set this up. This command automatically generates the credentials file at ~/.aws/credentials and the config file at ~/.aws/config.

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: json

The AWS CLI will prompt you for four pieces of information. AWS Access Key ID and AWS Secret Access Key are your account credentials.

Alternatively you can manually create and populate these files.

~/.aws/credentials

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

~/.aws/config

[default]
region=us-east-1
output=json

If you have multiple profiles you can also configure additional named profiles using the --profile option

$ aws configure --profile user2
AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLE
AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: text

Commands

Help:

To get help when using the AWS CLI, you can simply add help at the end of a command or sub-command.

$ aws help
$ aws ec2 help
$ aws ec2 describe-instances help

The help for each command is divided into six sections: Name, Description, Synopsis, Options, Examples and Output.

Command Structure:

$ aws <command> <sub-command> [options and parameters]

Specifying parameter values

$ aws ec2 create-key-pair --key-name my-key-pair

Output:

The AWS CLI supports three different output formats:

  • json
  • Tab-delimited text
  • ASCII formatted table

The default output format is chosen during the configuration step of aws configure. This can be changed by editing the config file or setting the AWS_DEFAULT_OUTPUT environment variable.

Additionally, per command output can be changed using the --output option

$ aws swf list-domains --registration-status REGISTERED --output text
# Example output
$ aws ec2 describe-volumes
{
    "Volumes": [
        {
            "AvailabilityZone": "us-west-2a",
            "Attachments": [
                {
                    "AttachTime": "2013-09-17T00:55:03.000Z",
                    "InstanceId": "i-a071c394",
                    "VolumeId": "vol-e11a5288",
                    "State": "attached",
                    "DeleteOnTermination": true,
                    "Device": "/dev/sda1"
                }
            ],
            "VolumeType": "standard",
            "VolumeId": "vol-e11a5288",
            "State": "in-use",
            "SnapshotId": "snap-f23ec1c8",
            "CreateTime": "2013-09-17T00:55:03.000Z",
            "Size": 30
        },
        {
            "AvailabilityZone": "us-west-2a",
            "Attachments": [
                {
                    "AttachTime": "2013-09-18T20:26:16.000Z",
                    "InstanceId": "i-4b41a37c",
                    "VolumeId": "vol-2e410a47",
                    "State": "attached",
                    "DeleteOnTermination": true,
                    "Device": "/dev/sda1"
                }
            ],
            "VolumeType": "standard",
            "VolumeId": "vol-2e410a47",
            "State": "in-use",
            "SnapshotId": "snap-708e8348",
            "CreateTime": "2013-09-18T20:26:15.000Z",
            "Size": 8
        }
    ]
}

You can query the resultant output using the --query option.

$ aws ec2 describe-instances --instance-ids i-0787e4282810ef9cf --query 'Reservations[0].Instances[0].PublicIpAddress'
"54.183.22.255"

Examples:

The following examples show the interface in action performing various tasks and demonstrate how powerful it can be.

# deleting an s3 bucket
aws s3 rb s3://bucket-name --force
# start ec2 instances
aws ec2 start-instances --instance-ids i-34hj23ie

Miscellaneous

  • Try the aws-shell to get a more interactive command line experience.
  • Use jq to parse the json outputs from various cli commands.