The AWS CLI supports configuring and switching between multiple profiles. This feature allows you to manage different AWS accounts or user credentials without needing to reconfigure your setup each time. By setting up named profiles, you can quickly switch between them when executing AWS CLI commands.

When multiple profiles are configured, the AWS CLI uses these profiles to access different accounts or roles. Each profile is stored in your system's configuration files and can be referenced by name. This method simplifies managing access across various environments, particularly for developers and administrators who work with multiple AWS accounts.

Switching between profiles in the AWS CLI is straightforward. You simply specify the desired profile when running a command. This ensures the command is executed with the correct permissions and credentials associated with that profile. Understanding how to use this feature effectively can streamline your workflow and reduce configuration errors.

Steps to switch accounts on AWS CLI:

  1. Configure multiple accounts or profiles for AWS CLI.
  2. List configured named profiles.
    $ cat .aws/credentials 
    [default]
    aws_access_key_id = AKIAJ3TE4LUDC4I6SQDQ
    aws_secret_access_key = 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
    [second_user]
    aws_access_key_id = AKIAJF4JT25ZZYGCTTVA
    aws_secret_access_key = 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
  3. Run the required command using the desired profile.
    $ aws s3 ls --profile second_user
    
    An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied

    The IAM user configured for the profile does not have S3 access, so the error is expected.

Discuss the article:

Comment anonymously. Login not required.