How do I set the name of the default profile in AWS CLI?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Setting the name of the default profile in the AWS Command Line Interface (CLI) is a crucial step for users managing multiple AWS environments or projects. Having distinct profiles allows you to seamlessly switch between various configurations like access keys, regions, and output formats without manually re-entering these details. Here's a comprehensive guide on how to set and manage your AWS CLI profiles, specifically focusing on configuring the default profile.
Understanding AWS CLI Profiles
AWS CLI supports multiple named profiles stored in a configuration file. When you execute an AWS CLI command, the tool looks for the required configuration and credentials under the specified profile. If a profile isn't specified in a command, the AWS CLI falls back to the default profile. This fallback mechanism makes setting the default profile vital for a smoother command-line experience.
Configuration Files
AWS CLI configuration is stored in two files located at ~/.aws/ (on Unix-based systems, or %USERPROFILE%\.aws\ on Windows):
config: Contains configuration settings such as region and output format.credentials: Stores access keys for authentication.
Setting the Default Profile
To set the default profile, ensure that the [default] section exists in both your config and credentials files.
Here’s an example:
~/.aws/config
~/.aws/credentials
Switching Profiles
To temporarily switch to a different profile, set the environment variable AWS_PROFILE. This approach overrides the default profile setting:
After setting this variable, any AWS CLI command uses the work profile configuration. To revert to the default profile, unset the AWS_PROFILE environment variable:
Alternatively, specify the profile directly in the command:
Persistent Profile Switching
For users frequently toggling profiles, consider using shell aliases or scripts. This strategy expedites switching and reduces manual configuration:
Best Practices and Recommendations
- Security: Avoid hardcoding sensitive information. Use AWS IAM roles with MFA for enhanced security.
- Profile Naming: Use descriptive names (e.g.,
work,personal-prod,test) to easily identify profiles. - Environment Variables: Use environment variables sparingly for production scripts to prevent reliance on system-specific configuration.
Troubleshooting Common Issues
- Missing Profile: Ensure profiles are correctly defined in both files, and avoid leading/trailing spaces in keys.
- Access Issues: Verify AWS credentials' validity and permissions.
- Region Errors: Define a region for each profile to avoid errors related to unspecified regions.
Summary
Here's a concise overview of managing AWS CLI profiles:
| Key Operation | Command/Action |
| Default Profile Setting | Define [default] profile in config and credentials files. |
| Switch Profile Temporarily | export AWS_PROFILE=profile_name |
| Command-Specific Profile | aws command --profile profile_name |
| Revert to Default Profile | unset AWS_PROFILE or omit --profile in commands |
| Persistent Profile Switching | Use aliases like alias awswork="export AWS_PROFILE=work" |
By effectively managing AWS CLI profiles, you can streamline your workflow, ensure the security of your credentials, and efficiently manage multiple AWS environments.

