DEV Community

Mudasir H
Mudasir H

Posted on

Use Multiple AWS profiles - AWS-CLI

Image description

To use multiple AWS profiles with the AWS CLI, you can follow these steps:

  • 1. Create AWS IAM users and credentials: You'll need to create IAM users for each of your AWS accounts with the appropriate permissions and generate an access key and secret access key for each user.
  • 2. Install AWS CLI: You can install AWS CLI on your machine by following the instructions on the official AWS documentation.
  • 3. Configure AWS profiles: You can configure AWS profiles by running the following command:
aws configure --profile new_profile
Enter fullscreen mode Exit fullscreen mode

Image description

Replace "new_profile" with the name of the profile you want to create. This will prompt you to enter the access key and secret access key for the IAM user you created in step 1.

  • 4. Use AWS profiles: You can use AWS profiles by running the following command:
aws s3 ls --profile new_profile
Enter fullscreen mode Exit fullscreen mode

This will execute the "ls" command on your S3 bucket associated with the "new_profile" profile.

You can repeat steps 3-4 for each profile you want to create. To switch between profiles, you can use the "AWS_PROFILE" environment variable. For example, to switch to the "new_profile" profile, you can run:

setx AWS_PROFILE new_profile

# Linux or macOS
export AWS_PROFILE=new_profile

# Make sure to restart your CLI after setting env.
Enter fullscreen mode Exit fullscreen mode

This will set the "AWS_PROFILE" environment variable to "new_profile", which will cause the AWS CLI to use the credentials associated with that profile for subsequent commands.

If you want to view the list of profiles you have configured, you can run the following command:

aws configure list-profiles
Enter fullscreen mode Exit fullscreen mode

Image description

This will display a list of all the profiles you have configured.

You can also manually add new profiles to your AWS configuration files located in the "~/.aws/" directory on Linux or macOS and "C:\Users\YOUR_USER.aws" directory on Windows. You can add the profiles in the "config" and "credentials" files respectively.

References:
https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

I hope this provides more comprehensive information on using multiple AWS profiles with the AWS CLI. Leave a comment if you have any further questions or concerns.

Top comments (0)