How to Install AWS CLI and Configure “credentials” and “config” Files on a Mac

Install AWS CLI on Mac

Amazon Web Services (AWS) is a powerful cloud platform, and the AWS Command Line Interface (CLI) makes it easier to manage your AWS services through commands on your local machine. In this guide, we’ll walk you through installing the AWS CLI on a Mac and configuring the necessary “credentials” and “config” files to streamline your access to AWS resources.


Step 1: Install AWS CLI on Your Mac

Option 1: Using Homebrew

The quickest way to install the AWS CLI is by using Homebrew, a popular package manager for macOS. If you don’t have Homebrew installed, you can install it by running the following command in your terminal:

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

Once Homebrew is installed, you can install the AWS CLI with a simple command:

brew install awscli

Option 2: Direct Download

Alternatively, you can download the AWS CLI package directly from the AWS CLI official website if you prefer not to use Homebrew.


Step 2: Verify Installation

After installation, verify that the AWS CLI was successfully installed by running:

aws --version

If everything is set up correctly, this command should output the version number of the AWS CLI you’ve just installed.


Step 3: Set Up AWS Credentials

Now that the AWS CLI is installed, you’ll need to set up your AWS credentials so you can access your AWS account. Follow these steps:

  1. Create or obtain your AWS access keys:
    • Go to the AWS Management Console and navigate to the IAM (Identity and Access Management) section.
    • If you don’t already have a user with programmatic access, create one and assign the necessary permissions.
    • Generate an Access Key ID and a Secret Access Key for the user.
  2. Configure credentials:
    • Open your terminal and run the following command to start the AWS CLI configuration:
    aws configure
  3. You’ll be prompted to input the following:

These values will be stored in the ~/.aws/credentials and ~/.aws/config files.


Step 4: Configure “credentials” and “config” Files Manually

The aws configure command automatically creates the required files for you, but sometimes you may want to edit or set them up manually, especially if you manage multiple AWS profiles.

File Locations:

  • The credentials file is located at:
    ~/.aws/credentials
  • The config file is located at:
    ~/.aws/config

Edit the “credentials” File

To manually configure the credentials file, open it with a text editor:

nano ~/.aws/credentials

You can structure it like this:

javaCopier le code[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

If you want to manage multiple profiles, you can add additional sections:

[profile1]
aws_access_key_id = YOUR_ACCESS_KEY_ID_FOR_PROFILE1
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY_FOR_PROFILE1

Edit the “config” File

Similarly, open the config file for region and output format settings:

bashCopier le codenano ~/.aws/config

Structure it as follows:

makefileCopier le code[default]
region = us-east-1
output = json

For additional profiles:

makefileCopier le code[profile profile1]
region = eu-west-1
output = text

By creating multiple profiles, you can easily switch between them using the --profile flag when running AWS CLI commands:

aws s3 ls --profile profile1

Step 5: Test Your Configuration

After setting up your credentials and config files, it’s essential to ensure that everything is working correctly. One way to do this is by listing your S3 buckets with the following command:

aws s3 ls

If everything is set up correctly, you should see a list of your S3 buckets. If not, review your credentials and region settings for potential errors.


Bonus: Setting Up Environment Variables for Credentials

In addition to configuring credentials files, you can also set AWS credentials as environment variables. This method can be particularly useful for temporary sessions or specific scripts.

To set environment variables in your shell, run:

bashCopier le codeexport AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY
export AWS_DEFAULT_REGION=YOUR_DEFAULT_REGION

These values will apply for the duration of your shell session.


Conclusion

Setting up the AWS CLI on your Mac and configuring your credentials and config files allows you to efficiently manage AWS resources from the command line. By following these steps, you can ensure secure and organized access to your AWS account, with the flexibility to manage multiple profiles and environments.

With the AWS CLI in place, you’ll be able to automate tasks, monitor your AWS resources, and deploy services—all from the comfort of your terminal. Happy cloud managing! 🌩️💻

Leave a Reply

Your email address will not be published. Required fields are marked *