Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shaposhnikoff/d2cba33198f1745fe39f629ec894e80d to your computer and use it in GitHub Desktop.
Save shaposhnikoff/d2cba33198f1745fe39f629ec894e80d to your computer and use it in GitHub Desktop.
Ophran IAM Policies ( Not used in any role )
import boto3,os,shutil,re
session = boto3.session.Session(profile_name='default')
iam = session.client('iam')
def list_policy_arns():
policy_arns = []
paginator = iam.get_paginator('list_policies')
for response in paginator.paginate(Scope='Local'):
for policy in response['Policies']:
policy_arns.append(policy['Arn'])
return policy_arns
def policy_role(policy_arn):
response = iam.list_entities_for_policy(PolicyArn=policy_arn,EntityFilter='Role')
return response['PolicyRoles']
unused_policies = []
for policy_arn in list_policy_arns():
list_length = len(policy_role(policy_arn))
if list_length == 0:
unused_policies.append(policy_arn)
regexp = re.compile(r'rbua-data-*')
for up in unused_policies:
if regexp.match(up.split('/')[-1]):
print(up.split('/')[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment