How to temporarily switch profiles for AWS CLI?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
AWS Command Line Interface (CLI) is a powerful tool for managing AWS services, allowing users to execute commands in a terminal or command prompt. It becomes increasingly important when dealing with multiple AWS accounts or roles within a single account. One common task involves temporarily switching between different AWS CLI profiles. This can be easily managed without altering your AWS configuration files permanently.
What are AWS CLI Profiles?
AWS CLI profiles are defined sets of configuration entries that allow CLI users to generate requests to different AWS accounts with different credentials and configurations. Typically, AWS CLI configurations are stored in two main files located in the ~/.aws directory:
credentials: Contains access keys and secret keys associated with different profiles.config: Specifies additional configuration options, such as the default region and output format.
Here's an example of how these files might look:
credentials file:
config file:
Switching Temporarily Between Profiles
Using the AWS_PROFILE Environment Variable
One of the simplest ways to switch AWS profiles temporarily is to use the AWS_PROFILE environment variable. This method allows you to set a specific profile for a single session or a specific command:
- Set the Environment Variable for a Session:On a Unix-like system:
On Windows:
- Run your AWS CLI command:
This command will be executed using the profile1 credentials and configuration.
- Change or unset the
AWS_PROFILEvariable as needed:To unset it in a Unix-like environment:
On Windows:
Specifying the Profile Directly in the CLI Command
Another way to temporarily switch profiles is to specify the profile directly when executing the command using the --profile flag:
This command will list S3 buckets using the credentials and configurations defined under profile2.
Comparison Table of Profile Switching Methods
| Method | Description | Scope |
AWS_PROFILE environment variable | Sets the profile for the entire terminal session | Session-wide |
--profile flag in CLI command | Uses the specified profile for a single command | Per-command |
Best Practices for Profile Management
- Descriptive Profile Names: Use meaningful names for profiles in the configuration files, especially when managing multiple accounts or environments such as dev, test, and prod.
- Secure Storage of Credentials: Use AWS config overrides or IAM role switching where possible to enhance security.
- Use of MFA: Consider enforcing Multi-Factor Authentication (MFA) for enhanced security when switching profiles.
- Script Automation: For regular task automation, incorporate the
--profileflag in scripts to ensure that they execute with the appropriate credentials.
By following these practices and using the methods outlined, users can efficiently and securely manage different AWS accounts with the CLI, enhancing both productivity and security posture.
Related reading
- How to test credentials for AWS Command Line Tools
- How to transfer data from S3 bucket to Kafka
- How to transfer files between AWS s3 and AWS ec2
- How to trigger azure function when there is a message in Kafka topic
- How to turn off the pager for AWS CLI return value?
- How to uninstall aws-cli
- How to unload a table on RedShift to a single CSV file?
- How to update a Map or a List on AWS DynamoDB document API?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.