AWS Credentials File: Configure and Use ~/.aws/credentials
Learn how to configure AWS CLI and SDK profiles in ~/.aws/credentials, use temporary and role-based credentials, select profiles, verify identities, and protect secrets.
The AWS shared credentials file lets the AWS CLI and many AWS SDKs find credentials for local development and command-line work. Its usual location is ~/.aws/credentials on macOS and Linux, or %UserProfile%\.aws\credentials on Windows.
This lesson explains profiles, the companion config file, credential provider precedence, temporary credentials, IAM Identity Center, role assumption, troubleshooting, and secure lifecycle management.
What AWS Credentials Do
AWS credentials are values or tokens used to authenticate requests to AWS. They identify an IAM principal, such as an IAM user or an assumed IAM role, and allow the AWS CLI or SDK to sign requests to AWS services.
Authentication answers “Who is making this request?” Authorization answers “What is that principal allowed to do?” Credentials help establish identity, while IAM policies, resource policies, permission boundaries, service control policies, and explicit denies determine authorization.
- Access key ID: The public identifier portion of an access key.
- Secret access key: The confidential value used to sign AWS requests. Never disclose it.
- Session token: An additional value required with temporary credentials.
A request made with an invalid identity can fail authentication. A request made with a valid identity that lacks permission can fail with AccessDenied.
Credential Types and Recommended Choices
Prefer credentials that are short-lived and supplied by an IAM role or a sign-in system. Long-term access keys should be a fallback for specific cases, not the default for human or workload access.
| Credential type | Typical source | Lifetime | Session token required | Recommended use | Security considerations |
|---|---|---|---|---|---|
| IAM user access keys | IAM user and shared credentials file | Long-term until rotated or deactivated | No | Limited legacy or special-purpose automation when no better option exists | Harder to control and easy to expose; rotate and restrict carefully |
| Temporary IAM role or AWS STS credentials | AssumeRole, workload role, or federated access | Time-limited | Yes | Human sessions, cross-account access, and workloads | Reduced exposure; refresh before expiration |
| IAM Identity Center credentials | Interactive workforce sign-in and cached tokens | Time-limited | Managed by the tooling | Human access to multiple accounts and roles | Use sign-in and refresh flows rather than copying tokens into permanent key fields |
| Environment-provided role credentials | EC2 instance profile, ECS task role, or another workload identity | Rotated automatically by the platform | Managed by the provider | Applications running on AWS compute | Avoid embedding secrets; grant the role least privilege |
Do not create or use root user access keys for normal work. Protect the root user separately and use IAM roles or delegated identities for administration.
The Shared AWS Credentials File
The shared credentials file is an INI-style text file containing profile sections. A profile is a named set of credential and configuration settings. The default profile is selected when no other profile is specified.
Default locations are:
- macOS and Linux:
~/.aws/credentials - Windows:
%UserProfile%\.aws\credentials
A standard credentials file can contain a default profile and named profiles:
[default]
aws_access_key_id = AKIAEXAMPLE
aws_secret_access_key = example-secret-value
[development]
aws_access_key_id = AKIADEVEXAMPLE
aws_secret_access_key = example-development-secret
[temporary]
aws_access_key_id = ASIAEXAMPLE
aws_secret_access_key = example-temporary-secret
aws_session_token = example-session-tokenThe values beginning with example- are placeholders, not usable credentials. In a real file, the secret access key and session token are sensitive.
Credentials File Field Reference
| Setting | File or source | Purpose | Example value | Sensitive |
|---|---|---|---|---|
aws_access_key_id | Credentials file or environment | Identifies the access key | AKIAEXAMPLE | Yes, although it is not the signing secret |
aws_secret_access_key | Credentials file or environment | Signs requests | example-secret-value | Yes |
aws_session_token | Credentials file or environment | Completes a temporary credential set | example-session-token | Yes |
region | Config file or environment | Default AWS Region | us-east-1 | No |
output | Config file or command-line option | Default CLI output format | json | No |
Profile names in the credentials file do not use the profile prefix. Use [development], not [profile development].
The AWS Config File
The companion config file is normally ~/.aws/config on macOS and Linux, or %UserProfile%\.aws\config on Windows. It stores non-secret settings such as Region and output format, as well as role and IAM Identity Center configuration.
Its profile naming convention differs from the credentials file:
- Default profile:
[default] - Named profile:
[profile development] - Credentials file named profile:
[development]
[default]
region = us-east-1
output = json
[profile development]
region = us-west-2
output = json
[profile production-role]
role_arn = arn:aws:iam::123456789012:role/ReadOnlyRole
source_profile = development
role_session_name = local-cli-session
region = us-east-1When the CLI or SDK selects development, it combines credentials from [development] in the credentials file with settings from [profile development] in the config file. Keep credentials in the credentials file and general settings in the config file unless a supported authentication method requires another arrangement.
Creating and Selecting Profiles
The interactive commands create or update profiles without requiring manual editing:
aws configure
aws configure --profile developmentThe first command configures the default profile. The second configures a named profile. You can then edit the config file to set a Region and output format, or use the CLI configuration commands for those settings.
Use a profile explicitly for one command:
aws sts get-caller-identity --profile development
aws s3 ls --profile developmentUse a profile for the current shell session:
export AWS_PROFILE=development
aws sts get-caller-identityOn Windows PowerShell, the equivalent session variable is:
$env:AWS_PROFILE = "development"Use distinct profiles for separate AWS accounts, development and production environments, or different job roles. Explicit selection helps prevent an operation intended for one account from running in another.
Profile Selection Methods
| Method | Example | Scope | When to use | Potential conflict or precedence concern |
|---|---|---|---|---|
| Command option | --profile development | One command | High-risk or one-off operations | Usually overrides AWS_PROFILE for that command |
AWS_PROFILE | export AWS_PROFILE=development | Current shell and child processes | Working with one profile for a session | Can cause surprising results if left set |
| Default profile | No profile option or variable | Current user and normal configuration | Simple local use | Used when no other profile is selected |
Credential Provider Precedence
The AWS CLI and SDKs use a credential provider chain: an ordered collection of locations and mechanisms consulted for credentials. Exact ordering and supported providers vary by CLI or SDK version, but common sources include:
- Command-line settings that select a profile or configure a command.
- Environment credentials such as
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY, andAWS_SESSION_TOKEN. - The shared credentials and config files.
- IAM Identity Center profile configuration and its cached sign-in tokens.
- Process-based credential providers configured to obtain credentials from an external command.
- Instance, container, or other workload role credentials supplied by the runtime.
Environment variables can unexpectedly override values you believe are coming from a profile. A shell, CI runner, IDE, container, or parent process may already define them. Always verify the effective identity instead of assuming which provider won.
aws configure list
aws sts get-caller-identityVerifying the Active Identity
AWS STS GetCallerIdentity reports the account and principal associated with the credentials currently in use. It is a safe first check after changing profiles or credential sources:
aws sts get-caller-identity --profile developmentThe response includes an account ID, an ARN, and a user ID. Common ARN forms include:
arn:aws:iam::123456789012:user/aliceidentifies an IAM user.arn:aws:sts::123456789012:assumed-role/ReadOnlyRole/session-nameidentifies temporary credentials from an assumed role.- A federated identity ARN indicates access obtained through a federation or sign-in workflow.
For a sensitive command, verify the identity immediately before running it and confirm both the account ID and role or user name.
Temporary Credentials and Expiration
Temporary credentials have an expiration time. They commonly consist of an access key ID, secret access key, and session token. The session token must travel with the other two values; a manually copied temporary key without its token is incomplete.
Expired credentials commonly produce ExpiredToken, token-related authentication errors, or failures after a session worked earlier. Refresh depends on the source:
- IAM Identity Center: run
aws sso login --profile workforceagain. - Assume-role workflows: renew the source credentials or create a new role session.
- External credential tools: run the tool's approved refresh or login workflow.
- Manually copied temporary values: obtain and copy a new complete set before expiration.
Assume-Role Profiles
An assumed role is a temporary identity obtained through AWS STS. A role profile normally belongs in the config file and names a role_arn and a source_profile. The source profile supplies credentials that are allowed to assume the target role.
[development]
aws_access_key_id = AKIADEVEXAMPLE
aws_secret_access_key = example-development-secret
[profile production-role]
role_arn = arn:aws:iam::123456789012:role/ReadOnlyRole
source_profile = development
role_session_name = local-cli-session
region = us-east-1Use the role profile like this:
aws sts get-caller-identity --profile production-role
aws s3 ls --profile production-roleFor cross-account access, the source principal belongs to one account and assumes a role trusted by the target account. The target role's trust policy and the source principal's permissions must both allow the operation. The resulting credentials are temporary and identify the assumed role, not the source user.
IAM Identity Center Integration
IAM Identity Center is a preferred interactive workforce authentication method. It lets a person sign in and receive access to assigned AWS accounts and roles without managing long-term IAM user keys locally.
aws configure sso
aws sso login --profile workforce
aws sts get-caller-identity --profile workforceThe CLI caches sign-in tokens locally so supported commands can obtain temporary credentials. Do not copy those tokens into the shared credentials file as permanent access keys. Sign in again when the cached session expires or when the CLI requests renewal.
Environment Variables and Alternate File Locations
Environment variables are useful for CI jobs, isolated test environments, and temporary command sessions:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKENAWS_PROFILEAWS_DEFAULT_REGIONAWS_SHARED_CREDENTIALS_FILEAWS_CONFIG_FILE
For example, use custom files in an isolated environment:
export AWS_SHARED_CREDENTIALS_FILE="$HOME/.config/aws/credentials"
export AWS_CONFIG_FILE="$HOME/.config/aws/config"In CI, prefer the platform's secure secret mechanism, workload identity, or an OIDC-to-role workflow. Never put secrets directly in shell history, application source code, committed .env files, build logs, or command lines that other users can inspect.
Security and Operational Hygiene
- Apply least privilege: grant only the permissions needed for the task.
- Prefer IAM roles and short-lived credentials over long-term access keys.
- Restrict local file permissions. On macOS and Linux, use
chmod 600 ~/.aws/credentialsandchmod 600 ~/.aws/config. - Do not use credential files on shared workstations or home directories with broad access unless the environment is properly isolated.
- Keep credential files out of source control and broadly accessible backups.
- Use secret scanning and appropriate AWS monitoring services to detect exposure and suspicious activity.
- Never publish credentials in documentation, support tickets, logs, screenshots, or terminal recordings.
- If a key is exposed, deactivate or delete it immediately, investigate its use, and replace it with a safer approved mechanism.
Credential Lifecycle Management
- Create a long-term access key only when a documented need exists.
- Record its owner, purpose, system, and expected lifetime.
- Apply the smallest practical IAM permissions.
- Rotate keys on the organization's schedule and immediately after suspected exposure.
- Delete unused keys and obsolete profiles.
- When an employee, project, or account changes, revoke access promptly and review role trust relationships and automation.
Practical Workflows
Default Local CLI Profile
Run aws configure, place the resulting credentials in the default profile, and set region = us-east-1 and output = json in the default config section. Then verify:
aws sts get-caller-identityDevelopment and Production Profiles
Create separate named profiles, select them explicitly, and verify the account before sensitive actions:
aws sts get-caller-identity --profile development
aws sts get-caller-identity --profile production-roleUse separate roles and least-privilege policies so that development credentials cannot accidentally perform production administration.
Isolated CI Task
Supply short-lived credentials through the CI platform's secure mechanism or attach a workload role. If custom paths are required, set AWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE only within the job environment, and remove temporary files after the task.
Troubleshooting
| Symptom or error | Likely cause | How to confirm | Resolution |
|---|---|---|---|
Unable to locate credentials | No source, missing profile, wrong file path, or different user/home directory | Run aws configure list; inspect selected profile and file path variables | Configure an approved profile or role source, then verify with STS |
| Wrong account or role | AWS_PROFILE or environment credentials override expectations; profile option omitted; role source is wrong | Run aws sts get-caller-identity and inspect environment variables | Select the intended profile explicitly and remove conflicting variables where appropriate |
ExpiredToken or token errors | Temporary credentials expired or session token is missing | Check the originating login or role session | Refresh the login or role session and use the complete credential set |
InvalidClientTokenId, SignatureDoesNotMatch, or invalid key errors | Wrong key pair, deactivated key, formatting problem, or missing temporary token | Inspect the active source without displaying secrets; check key status if authorized | Replace with a valid approved source and revoke exposed values |
AccessDenied | Missing permission, explicit deny, policy boundary, resource policy, or wrong identity | Verify identity and review the denied action and resource | Use the intended role or grant only the required permission through the correct policy |
| Credentials were exposed | Committed file, log, ticket, screenshot, code, or environment file | Identify the key, search copies, and review activity | Deactivate or delete immediately, remove copies, replace the mechanism, and investigate |
Key Exam Notes
- The credentials file normally stores
aws_access_key_id,aws_secret_access_key, and, for temporary credentials,aws_session_token. - Credentials profile sections use
[name]; named config sections use[profile name]. --profileselects one command, whileAWS_PROFILEaffects the current process environment.- Environment credentials can override profile-based credentials, so verify the effective identity.
aws sts get-caller-identityreveals the account and principal currently in use.- Temporary credentials expire and require a session token; refresh the originating login or role session.
- Use IAM roles, IAM Identity Center, and workload credentials instead of root keys or unnecessary long-term IAM user keys.
For continued reference, see AWS credentials file configuration.