Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to fix "NoCredentialsError" with Boto3 for DynamoDB

I’m currently creating a webapp using Flask and DynamoDB.

However, I ran into a really strange error currently that I hope someone might be able to assist me with.

Currently when I run the web app in Pycharm, everything works fine.
I’m able to request data from DynamoDB without any issues or errors.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However, the moment I run the exact same file on Ubuntu with Python3, I suddenly encounter a "NoCredentialsError".
The exact same file works fine when I run it on Pycharm, but for some reason it’s giving me this error only when I try and test it in Ubuntu.

The function that’s resulting in this error is simply this:


def selectItem(PK, SK):

    dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
    
    table = dynamodb.Table('testDB')

    response = table.get_item(
        Key={
            'PK': PK,
            'SK': SK
        }
    )
    return response

I have the table and region coded in simply for the sake of context and to keep things simple while testing.

Here is the full Traceback for additional details:

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 2095, in call
return self.wsgi_app(environ, start_response)

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 2080, in wsgi_app
response = self.handle_exception(e)

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)

File "/mnt/c/Users/Test/Desktop/Coding/TestCode/mainapp/webapp.py", line 139, in login
session["farm"] = DynamoDB.selectItem("Users", session["email"])

File "/mnt/c/Users/Test/Desktop/Coding/TestCode/mainapp/DataBase/DynamoDB.py", line 147, in selectItem
response = table.get_item(

File "/home/testcode/.local/lib/python3.8/site-packages/boto3/resources/factory.py", line 580, in do_action
response = action(self, *args, **kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/boto3/resources/action.py", line 88, in call
response = getattr(parent.meta.client, operation_name)(*args, **params)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/client.py", line 415, in _api_call
return self._make_api_call(operation_name, kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/client.py", line 731, in _make_api_call
http, parsed_response = self._make_request(

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/client.py", line 751, in _make_request
return self._endpoint.make_request(operation_model, request_dict)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 107, in make_request
return self._send_request(request_dict, operation_model)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 180, in _send_request
request = self.create_request(request_dict, operation_model)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 120, in create_request
self._event_emitter.emit(event_name, request=request,

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/hooks.py", line 358, in emit
return self._emitter.emit(aliased_event_name, **kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/hooks.py", line 229, in emit
return self._emit(event_name, kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/hooks.py", line 212, in _emit
response = handler(**kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/signers.py", line 95, in handler
return self.sign(operation_name, request)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/signers.py", line 167, in sign
auth.add_auth(request)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/auth.py", line 401, in add_auth
raise NoCredentialsError()
botocore.exceptions.NoCredentialsError: Unable to locate credentials

>Solution :

Do you your aws credentials on your ubuntu machine? It should be looking for them at ~/.aws/credentials

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading