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

Viewed 588

I'm trying to write a script to get all the policies in one role and detach all policies.

Which lead me to use list_role_policies for each role, the call was successfully made, but it always shows:

{'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}}

When I looked into the IAM via console, there are definitely policies attached, some Inline Poliicies but mostly Managed Policies, is this why they are not shown?

Also, ultimately I actually don't care too much about the policy names if it is not required, could there be some other ways that just detach all policies?

Thanks!

1 Answers

list_role_policies will list only the inline policies attached to the Role.

From the response, it looks like the role does not have any inline policies attached to it.

You have to make two different API requests, one for retrieving the list of Inline Policies and another for the Managed Policies.

Get all the inline PolicyNames for a Role using list_role_policies() and delete them from the Role using the method delete_role_policy().

Similarly, get all the managed PolicyArns for a Role using list_attached_role_policies() and detach them using detach_role_policy().

Related