How to Fix AWS CLI Credentials Not Found Error
In this tutorial, you'll learn about How to Fix AWS CLI Credentials Not Found Error. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
You run an AWS CLI command and get Unable to locate credentials — the CLI cannot find valid access keys.
Step-by-Step Fix
1. Configure credentials via aws configure
aws configure
Enter the prompted values:
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: json
2. Verify the configuration
aws sts get-caller-identity
Expected output:
{
"UserId": "AIDAIOSFODNN7EXAMPLE",
"Account": "123456789012",
"Arn": "arn:aws:iam::123456789012:user/myuser"
}
3. Use environment variables (alternative)
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export AWS_DEFAULT_REGION=us-east-1
4. Use a named profile
aws configure --profile production
aws s3 ls --profile production
5. Use an IAM role on EC2
Attach an IAM role to the EC2 instance instead of configuring keys:
aws s3 ls
No credential configuration needed — the CLI uses the instance metadata service.
Common Mistakes
| Mistake | Fix |
|---|---|
| Typo in key ID or secret key | Run aws configure again |
| Keys expired | Generate new keys in IAM console |
| Wrong profile selected | Use --profile flag or set AWS_PROFILE |
| Environment variables not exported | Add export to shell config file |
| IAM role not attached | Attach role to EC2 instance |
Prevention
- Store credentials in
~/.aws/credentialsfor persistence. - Use IAM roles on EC2 instead of long-lived keys.
- Rotate access keys every 90 days.
- Never commit credentials to version control.
Common Mistakes with cli credentials
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world AWS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro