Resolving the Boto3 NoCredentials Error in Python

Mahabubur Rahman
0
The NoCredentialsError is an error encountered  when using the Boto3 library to interface with AWS(Amazon Web Services). Specifically, this error is encountered when your AWS credentials are missing, invalid, or not recognized by your Python script.


Attempting to create a Connection

A NoCredentialsError is encountered while creating a connection to AWS. Let’s have a look at a piece of code which connects to a Boto3 client.



import boto3

client = boto3.client('ce',region_name='ap-southeast-1')

response = client.get_cost_and_usage(
    TimePeriod={
        'Start': '2023-02-01',
        'End': '2023-03-01'
    },
    Granularity='MONTHLY',
    Metrics=[
        'AmortizedCost',
    ]
)

An Issue Is Found 

Let's run the above script and there is an issue with the credentials, the console will printout the following error:


Traceback (most recent call last):
  File "F:\Python\adplaydsp\venv\lib\site-packages\IPython\core\interactiveshell.py", line 3369, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-9aea1c404679>", line 1, in <cell line: 1>
    response = client.get_cost_and_usage(
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\client.py", line 530, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\client.py", line 943, in _make_api_call
    http, parsed_response = self._make_request(
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\client.py", line 966, in _make_request
    return self._endpoint.make_request(operation_model, request_dict)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\endpoint.py", line 119, in make_request
    return self._send_request(request_dict, operation_model)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\endpoint.py", line 198, in _send_request
    request = self.create_request(request_dict, operation_model)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\endpoint.py", line 134, in create_request
    self._event_emitter.emit(
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\hooks.py", line 412, in emit
    return self._emitter.emit(aliased_event_name, **kwargs)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\hooks.py", line 256, in emit
    return self._emit(event_name, kwargs)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\hooks.py", line 239, in _emit
    response = handler(**kwargs)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\signers.py", line 105, in handler
    return self.sign(operation_name, request)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\signers.py", line 189, in sign
    auth.add_auth(request)
  File "F:\Python\adplaydsp\venv\lib\site-packages\botocore\auth.py", line 418, in add_auth
    raise NoCredentialsError()
botocore.exceptions.NoCredentialsError: Unable to locate credentials


In the console error we see a lengthy traceback. After the traceback, we see the final two lines showing "raise NoCredentialsError()" and 
"botocore.exceptions.NoCredentialsError: Unable to locate credentials". This is confirms there is an issue with the AWS credentials which needs to be corrected.

Resolve the NoCredentialsError


To avoid the above error you need to set the correct credentials to your python script. Let's see the bellow python script. Here we set the aws_access_key_id and aws_secret_access_key get from AWS user secret section.


import boto3

client = boto3.client('ce', region_name='ap-southeast-1', aws_access_key_id=YOUR_ACCESS_KEY_ID, aws_secret_access_key=YOUR_ACCESS_KEY)

response = client.get_cost_and_usage(
    TimePeriod={
        'Start': '2023-02-01',
        'End': '2023-03-01'
    },
    Granularity='MONTHLY',
    Metrics=[
        'AmortizedCost',
    ]
)



Now run the above script and you will get the expected information's.










Tags

Post a Comment

0Comments
Post a Comment (0)