When you launch new EC2 instance of Windows Server, then you try to execute AWS CLI and you get error message “Unable to locate credentials”. I will help you to address the cause and how to fix it.
“Unable to locate credentials”
There are many cases that can let you get into this error, you should double check some basic requirements such as:
- Did you set the AWS credential into your instance with “aws configure” command?
- Did you attach a IAM role into your instance?
- And do not forget to make sure your clientID/clientSecret are right.
If you finished on the double check step and still receive the error, go to next step. Answer below questions:
- Is it running Windows Server?
- Which AMI that you used to launch the instance? Is it a custom AMI? (Not the original one)
- Did you launch instance on a different VPC’s CIDR from the original AMI where it was created?
- You cannot ping/access to http://169.254.169.254/latest/meta-data/iam/security-credentials from your instance, right?
If four answers of above questions are YES, follow my below solution to fix it.
- Open your PowerShell, execute command: “Get-NetRoute“
- You can see the list of 169.254.169.XXX/32 IP addresses. The cause of error “Unable to locate credentials” is you cannot access to 169.254.169.XXX/32 to get the metadata of instance.
- So we must fix it by fix these netroutes. You can use remove-netroute command to remove the wrong netroutes and set-netroute command to add new netroutes by yourself.
- Similarity, you can run this script to auto fix the netroutes: on Github
- Now you can check if you can access http://169.254.169.254/latest/meta-data/iam/security-credentials. If still NOT, go to next step.
- You must create new AMI of the current instance, then re-launch new instance from the newly created AMI. The cause of you must do this step because in the first time instance launch, it runs some user-data scripts to register the instance’s metadata.
- After that, you attach the IAM Role to the instance.
Hola! Now you can use AWS CLI command on the newly instance.