How to get up and running with AWS CLI on macOS
Tags: aws, cli, bash, mac
Published: 2020-04-19 14:20:15

This article was also published on [razcodes.dev](https://razcodes.dev "razcodes.dev")

While you can do all the things you need in AWS using the console, the AWS CLI offers a convenient way to control your environment from the terminal as well as to create simple or complex automations through scripts.

## Get credentials
The first thing that you will need to be able to issue commands is a set of security credentials, that is an Access Key ID and a Secret Access Key. You can do that by using the AWS console.

- login to your account
- Services -\> IAM
- Users
- Add user
- Give it a username (this is more for you, it will not be used ex: service)
- Select **Programmatic access** then click _Next_
- Attach exiting policies
- For this demo I selected _AdministratorAccess_
- Next add tags if you want
- Next Review your choices
- **Create user**

Once you clicked on _Create user_, you will be given the option to download a .csv file with the credentials or you can copy them from this screen for later use and maybe keep them in a password manager. Please keep in mind that this will be the one and only time you will see the Secret access key in the console, so make sure you save it somewhere safe.

## Install the CLI

The easiest way to install the CLI on a Mac is by using **Homebrew**. If you don't have it installed already you can do so by following the instructions on their [website](https://brew.sh/ "Homebrew"), which say that you need to paste this following command in your terminal.

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
```

Once you have Homebrew installed, simply issue the following command in your terminal to install the AWS CLI.

```bash
brew install awscli
```

This will install awscli version 2 at the time of writing this article.

## Configure the CLI with your credentials

Now you need to tell the CLI what credentials to use to it can access your AWS account.

```bash
aws configure
```

- paste your AWS Access Key ID
- paste your AWS Secret Access Key
- fill in your default region (ex: us-east-1)
- default output can be json, yaml, text, table (I chose json)

## Issue commands and get help

Now you can issue commands. For example to see a list of all your s3 buckets you would type:

```bash
aws s3 ls
```

To get general help from the CLI:

```bash
aws help
```

To get help for a specific command, so you know how to use it, issue the command followed by _help_. For example, to get help on using the CLI with S3:

```bash
aws s3 help
```

## Named Profiles (optional)

When you configured your credentials above, this created a folder in your home directory called _.aws_ as well as 2 files in that folder, _credentials_ and _config_. These files can be used to configure additional profiles, either for different users in the same account, with different permissions, or as I use them, for different AWS accounts.

Let's say you want to add another user profile. You would first edit the credentials file.

```bash
vim ~/.aws/credentials
```

Under your default account you add the following lines, replacing the username and the credentials with yours.

```bash
[username]
aws_access_key_id=<your_access_key_id>
aws_secret_access_key=<your_secret_access_key>
```

Next edit the config file.

```bash
vim ~/.aws/config
```

Add your new profile information under the existing one, making sure the username matches the one in your credentials file.

```bash
[profile username]
region=us-east-1
output=json
```

Finally, you can use your new profile by issuing the following command in the terminal.

```bash
export AWS_PROFILE=username
```

From this point, every aws command will use this profile's credentials.

To read more about the AWS CLI and all the options you can visit [this link](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html "AWS CLI").