Introduction
Navigating through multiple AWS accounts via the Command Line Interface (CLI) can streamline operations and enhance security by segregating work environments. This guide will walk you through the process of adding new profiles to your AWS CLI setup and executing commands under different profiles, ensuring you can switch contexts effortlessly and securely.
Setting Up and Managing AWS CLI Profiles
Understanding how to manage multiple AWS CLI profiles is essential for developers and administrators working with various AWS environments. Below, we detail the steps to view existing credentials, add new profiles, and utilize them effectively.
Step 1: Accessing AWS CLI Configuration Files
First, navigate to the AWS CLI configuration directory and inspect your current setup:
Open your terminal and run:
cd ~/.aws
View your registered account’s credentials and configuration by using:
cat credentials
cat config
Step 2: Adding a New Profile
To add a new profile, follow these steps to configure it with necessary credentials and settings:
Begin configuration for your default profile or a new one:
aws configure
Enter the credentials for your default profile (access key, secret access key).
Configure a secondary profile:
aws configure --profile my-other-aws-account
Then, when prompted for the access key and it shows “none”:
- enter the access key for the new AWS account.
- Do the same for the secret access key.
- Specify a default region name.
- Optionally, set a default output format (e.g., YAML).
- Verify the new profile setup: After configuration, inspect the
credentials
andconfig
files again to ensure they contain both your default and new profile information.
Step 3: Executing Commands Using Specific Profiles
To operate under a specific profile, use the --profile
option in your AWS CLI commands. This allows you to switch between accounts seamlessly:
To list S3 buckets using your default profile:
aws s3 ls
To execute commands from another account:
aws s3 ls --profile my-other-aws-account
Summary
Managing multiple AWS CLI profiles enables a flexible, secure, and efficient way to handle various AWS accounts and services. By setting up individual profiles for different accounts or environments, you can easily switch between them, ensuring that operations are not only streamlined but also compartmentalized for better security and organization. Whether you are managing resources across several projects or ensuring that your development and production environments remain separate, mastering AWS CLI profiles is an invaluable skill for any AWS practitioner.
Stay tuned for further tutorials that delve deeper into optimizing your AWS CLI experiences and broadening your AWS management capabilities.