The NoCredentialsError
is an error encountered when using the Boto3 library to interface with Amazon Web Services (AWS). Specifically, this error is encountered when your AWS credentials are missing, invalid, or cannot be located by your Python script. These credentials are stored by default at ~/.aws/credentials
which contains your access key and secret access key for using AWS services, along with other configuration details such as your region code. Think of this file as your login and password for the service.
Attempting a Connection
A NoCredentialsError
is encountered while creating a connection to AWS, which is the first step for any program using these web services. Let’s have a look at a piece of code which connects to a Boto3 resource and attempts to print out the resource's name.
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
It’s important to note that there is no issue with this code itself. The issue is that when Boto3 attempts to make a connection to this resource, it attempts to reference the credentials file to make sure you are authorized to access said resource. If the credentials are valid, the program proceeds without issue. However, if there is a problem, the developer will encounter an error.
An Issue Is Found
Let’s say the above script is run and there is an issue with the credentials, the console will print out the following error:
Traceback (most recent call last):
File "C:/code/Python/NoCred/main.py", line 5, in <module>
for bucket in s3.buckets.all():
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\boto3\resources\collection.py", line 81, in __iter__
for page in self.pages():
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\boto3\resources\collection.py", line 166, in pages
pages = [getattr(client, self._py_operation_name)(**params)]
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\client.py", line 508, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\client.py", line 899, in _make_api_call
operation_model, request_dict, request_context
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\client.py", line 921, in _make_request
return self._endpoint.make_request(operation_model, request_dict)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\endpoint.py", line 119, in make_request
return self._send_request(request_dict, operation_model)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\endpoint.py", line 198, in _send_request
request = self.create_request(request_dict, operation_model)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\endpoint.py", line 137, in create_request
operation_name=operation_model.name,
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\hooks.py", line 412, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\hooks.py", line 256, in emit
return self._emit(event_name, kwargs)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\hooks.py", line 239, in _emit
response = handler(**kwargs)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\signers.py", line 103, in handler
return self.sign(operation_name, request)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\signers.py", line 187, in sign
auth.add_auth(request)
File "C:\code\Python\NoCred\venv\3K\lib\site-packages\botocore\auth.py", line 407, in add_auth
raise NoCredentialsError()
botocore.exceptions.NoCredentialsError: Unable to locate credentials
The console shows a lengthy traceback section showing the set of methods being used internally by the Boto3 library to attempt to verify your credentials. After the traceback, we see the final two lines showing "raise NoCredentialsError()" and "botocore.exceptions.NoCredentialsError: Unable to locate credentials."
This confirms there is an issue with the AWS credentials which needs to be corrected.
Avoiding a NoCredentialsError
The first step to correcting a NoCredentialsError
is to confirm that the credential file does in fact exist on your computer. The credentials file is by default stored at ~/.aws/credentials.
Begin by navigating to this location and confirming the file exists. When opening the file in a text editor, you should see an access key and a secret access key. Note that further configurations may also be shown, but these first two are required. If one or both of these are missing or incorrect, they can be manually added to the file. The values of these keys are provided by Amazon directly and should be obtained through your AWS account.
If you have not created credentials, you can generate them using the AWS Management Console, which is available at https://console.aws.amazon.com/iam/. The process of creating a new AWS user goes beyond the scope of this article, but more details can be found in Amazon’s AWS documentation at https://docs.aws.amazon.com/.
Managing Deeper Issues
When using a web service like AWS, having up-to-date and properly configured credentials is absolutely crucial. Unfortunately, encountering a NoCredentialsError
is evidence that this may not be the case. If after following the steps outlined above, the issue persists, it may be best to reach out to Amazon directly as your account is not active or not in good standing. Luckily, in most cases though, this problem simply comes down to locating and editing the file as directed above.
Track, Analyze and Manage Errors With Rollbar
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Python errors easier than ever. Sign Up Today!