boto3 给出 AccessDenied,有没有办法查找缺少的权限?

boto3 giving AccessDenied, is there a way to lookup the missing permissions?

我一直在试图弄清楚为什么我的脚本中的这个步骤不起作用,据我所知,我的用户有权在我们的 AWS 帐户上执行所有操作,所以我有点困惑。我想知道是否有一种方法可以从 botocore 或 boto3 本身获得 return 来识别您的用户缺少的 iam 权限,而不是为您需要自己完成的工作找出正确的权限?

最初我 zone_id 配置 from here

>>> dns_record = connection.change_resource_record_sets(
...     HostedZoneId=zone_id,
...     ChangeBatch={
...         'Changes': [
...             {
...                 'Action': action,
...                 'ResourceRecordSet': {
...                     'Name': name,
...                     'Type': record_type,
...                     'TTL': 60,
...                     'AliasTarget': {
...                         'HostedZoneId': zone_id,
...                         'DNSName': destination,
...                         'EvaluateTargetHealth': eval_health
...                     },
...                 }
...             },
...         ]
...     }
... )

Traceback (most recent call last):
  File "<stdin>", line 14, in <module>
  File "/usr/lib/python2.7/site-packages/botocore/client.py", line 253, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/lib/python2.7/site-packages/botocore/client.py", line 543, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the ChangeResourceRecordSets operation: User: arn:aws:iam::1337:user/rumbles is not authorized to access this resource
>>> 
>>> print connection, zone_id, action, name, record_type, destination, eval_health
<botocore.client.Route53 object at 0x7f9049d64590> Z117KPS5GTRQ2G UPSERT test-rumbles.domain.com A some-elb-1337.us-east-1.elb.amazonaws.com False

如果我将 zone_id 更改为我们 AWS 域的区域,我会得到:

>>> zone_id = "Z2401337RNHANU"
>>> 
>>> try:
...
An error occurred (InvalidInput) when calling the ChangeResourceRecordSets operation: Invalid request
{'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 400, 'RequestId': '2bfde962-0e81-11e7-b312-53961dec3e91', 'HTTPHeaders': {'x-amzn-requestid': '2bfde962-0e81-11e7-b312-53961dec3e91', 'date': 'Tue, 21 Mar 2017 21:56:00 GMT', 'content-length': '259', 'content-type': 'text/xml', 'connection': 'close'}}, 'Error': {'Message': 'Invalid request', 'Code': 'InvalidInput', 'Type': 'Sender'}}

我之前不得不研究这些问题,我知道我只需要为这个特定的工作找到正确的 IAM 许可(假设我在正确的道路上),但我想知道是否有从 botocore returned 获取此信息的某种方式?与其花时间手动查找,不如我得到拒绝我访问的 iam 规则 return 由我的 except 以某种方式编辑?

或者我说的是我还没有 found/created 的功能请求?

您的答案来自无效的区域 ID - HostedZoneId="" 的第一个 boto3 调用需要是您 url 的托管区域 ID,第二个是引用别名目标的托管区域 ID。

如果它们相同,您将被拒绝访问,因为您正在尝试更改 Amazon 拥有的 URL 的记录。

来自文档 http://boto3.readthedocs.io/en/latest/reference/services/route53.html#Route53.Client.change_resource_record_sets

首次引用 HostedZoneId HostedZoneId(字符串)—— [必填]

包含您要更改的资源记录集的托管区域的 ID。

HostedZoneId 的第二个引用 - 嵌套在 AliasTarget

-HostedZoneId(字符串)——[必需] 仅限别名资源记录集:使用的值取决于您要将流量路由到的位置: ELB 负载均衡器 为负载均衡器指定托管区域 ID 的值。使用以下方法获取托管区域 ID:

Elastic Load Balancing table Amazon Web Services General Reference "AWS Regions and Endpoints" 章节中的内容:使用与您创建负载的区域相对应的 "Amazon Route 53 Hosted Zone ID" 列中的值中的平衡器 AWS 管理控制台:转到 Amazon EC2 页面,单击导航窗格中的 Load Balancers,select 负载均衡器,并获取 Description 选项卡上 Hosted zone 字段的值。 Elastic Load Balancing API :使用 DescribeLoadBalancers 获取 CanonicalHostedZoneNameId 的值。有关详细信息,请参阅适用的指南: 经典负载均衡器:DescribeLoadBalancer 应用程序负载均衡器:DescribeLoadBalancer AWS CLI :使用 describe-load-balancers 获取 CanonicalHostedZoneNameID 的值。 配置为静态网站的 Amazon S3 存储桶

指定您在其中创建存储桶的区域的托管区域 ID。有关有效值的更多信息,请参阅亚马逊简单存储服务网站终端节点 table "AWS Regions and Endpoints" 章节亚马逊网络服务一般参考。

你的第二个问题 - 400 来自 zone_id 再次相同 - 所以现在你的第一个 zone_id 是正确的 - 它现在正在寻找 AWS 拥有的 URL你的 url...

试着把它分成这样

dns_record = connection.change_resource_record_sets(
HostedZoneId=my_url_zone_id,
ChangeBatch={
    'Changes': [
        {
            'Action': action,
            'ResourceRecordSet': {
                'Name': name,
                'Type': record_type,
                'AliasTarget': {
                    'HostedZoneId': elb_zone_id,
                    'DNSName': destination,
                    'EvaluateTargetHealth': eval_health
                },
            }
        },
    ]
}

其中 elb_zone_id = elb 的 zone_id 和 其中 my_url_zone_id = 您的特定 URL

的 zone_id