我在使用boto3时遇到了错误。

0

【以下的问题经过翻译处理】 我正在使用以下脚本获取ec2实例列表,但遇到了问题[1]。我能够使用相同的凭据与session.get_available_regions(service)和session.client('sts'),但是在session.resource('ec2', region_name=region)中无法使用。我已经验证了机器上的时钟,并进行了同步。

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
profile picture
专家
已提问 5 个月前29 查看次数
1 回答
0

【以下的回答经过翻译处理】 从以下错误信息可以看出,您的access key/secret access key/region是无效的。请 确认是否正确。您可能需要重新生成新的访问密钥/秘密访问密钥。

  • botocore.exceptions.ClientError: An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials
profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则