问题
When trying to connect to an AWS service via Boto3, I occasionally get the following error:
NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV3Handler'] Check your credentials
This is running on an EC2 instance with an IAM Role configured. This error happens rarely.
回答1:
IAM roles provide credentials via the AWS metadata service. Boto3 will connect to this service to get credentials, but this connection can time out. By default, Boto3 will not retry connections to the metadata service, but this can be changed by setting the environment variable AWS_METADATA_SERVICE_NUM_ATTEMPTS
to a number higher than 1.
See the docs:
AWS_METADATA_SERVICE_NUM_ATTEMPTS
When attempting to retrieve credentials on an EC2 instance that has been configured with an IAM role, boto3 will only make one attempt to retrieve credentials from the instance metadata service before giving up. If you know your code will be running on an EC2 instance, you can increase this value to make boto3 retry multiple times before giving up.
来源:https://stackoverflow.com/questions/59312799/boto3-intermittent-noauthhandlerfound-errors