I am running into an error using boto3

0

I am using the following script to get the list of ec2 instances but running into [1]. I am able to use the same creds with session.get_available_regions(service) and session.client('sts') but it doesn't work with session.resource('ec2', region_name=region). I've verified the clock on the machine and it is synced. I would appreciate any help!

session = boto3.Session(
        aws_access_key_id=key,
        aws_secret_access_key=secret,
)
ec2_instance = session.resource('ec2', region_name=region)
for instance in ec2_instance.instances.all():
        print(instance)

[1]

Failed to run listener function (error: An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/ops/listener/thread_runner.py", line 120, in run_ack_function_asynchronously
    listener.run_ack_function(request=request, response=response)
  File "/usr/local/lib/python3.10/dist-packages/ops/listener/custom_listener.py", line 50, in run_ack_function
    return self.ack_function(
  File "/home/ops/codeserver/awsops.py", line 439, in get_ec2_instances
    for instance in ec2_instance.instances.all():
  File "/usr/local/lib/python3.10/dist-packages/boto3/resources/collection.py", line 81, in __iter__
    for page in self.pages():
  File "/usr/local/lib/python3.10/dist-packages/boto3/resources/collection.py", line 171, in pages
    for page in pages:
  File "/usr/local/lib/python3.10/dist-packages/botocore/paginate.py", line 269, in __iter__
    response = self._make_request(current_kwargs)
  File "/usr/local/lib/python3.10/dist-packages/botocore/paginate.py", line 357, in _make_request
    return self._method(**current_kwargs)
  File "/usr/local/lib/python3.10/dist-packages/botocore/client.py", line 530, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/botocore/client.py", line 960, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials
1 Antwort
1
Akzeptierte Antwort

From the below error, it indicates your access key/secret access key/region is invalid. Kindly validate this via CLI. You may need regenerate new access key/secret access key.

  • botocore.exceptions.ClientError: An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials
beantwortet vor einem Jahr
  • I am able to use the same creds with session.get_available_regions(service) and session.client('sts') but it doesn't work with session.resource('ec2', region_name=region)

  • The APIs you listed may work without any authenication. Let us explore further in two directions.

    1. Are you using IAM user? If so, kindly check the IAM policy/role for this user.
    2. Install the aws cli and try to issue some test cmd "aws s3 ls". Refer to the following link to install aws cli.

    https://aws.amazon.com/cli/

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen