boto3 list_role_policies 返回空数组,或者我如何分离所有策略?

boto3 list_role_policies returning empty array, or how do I just detach all policies?

我正在尝试编写一个脚本来获取一个角色中的所有策略并分离所有策略。

这导致我为每个角色使用list_role_policies,调用成功,但总是显示:

{'PolicyNames': [], 'IsTruncated': False, 'ResponseMetadata': {'RequestId': 'xxx', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'xxx', 'content-type': 'text/xml', 'content-length': '323', 'date': 'Thu, 02 Apr 2020 18:49:35 GMT'}, 'RetryAttempts': 0}}

当我通过控制台查看 IAM 时,肯定有附加的策略,一些内联策略但主要是托管策略,这就是它们未显示的原因吗?

此外,如果不需要的话,最终我其实不太关心策略名称,是否有其他一些方法可以分离所有策略?

谢谢!

list_role_policies 将仅列出附加到角色的内联策略。

从响应来看,该角色似乎没有附加任何内联策略。

您必须发出两个不同的 API 请求,一个用于检索内联策略列表,另一个用于托管策略。

使用 list_role_policies() and delete them from the Role using the method delete_role_policy().

获取角色的所有内联 PolicyNames

类似地,使用 list_attached_role_policies() and detach them using detach_role_policy().

获取角色的所有托管 PolicyArns