This answer https://stackoverflow.com/a/77985652/9499392 helped me in 2025 . Here is a little detail you can follow.
The issue occurred because the AWS CLI gives priority to environment variables (AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
, etc.) over the ~/.aws/credentials
and ~/.aws/config
files.
In my case, I had set AWS credentials in the Windows environment variables earlier and completely forgot about it. So even after updating ~/.aws/credentials
with the new access key and secret key, the AWS CLI and boto3 kept picking up the old values from the environment.
NOTE : I already have aws configured
First, I checked if any AWS environment variables were set:
Get-ChildItem Env:AWS*
This showed old credentials still lingering.
I removed them from the current PowerShell session:
Remove-Item Env:AWS_ACCESS_KEY_ID
Remove-Item Env:AWS_SECRET_ACCESS_KEY
Remove-Item Env:AWS_SESSION_TOKEN
Remove-Item Env:AWS_DEFAULT_REGION
This updates ~/.aws/credentials
and ~/.aws/config
.
Now, verify:
aws sts get-caller-identity