Skip to content

Instantly share code, notes, and snippets.

@dgwhited
Last active July 13, 2020 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgwhited/15d68a4d150f4b082f30c533d01ee50e to your computer and use it in GitHub Desktop.
Save dgwhited/15d68a4d150f4b082f30c533d01ee50e to your computer and use it in GitHub Desktop.
import boto3
profile_name = 'dev'
role_name = 'test-role'
session = boto3.Session(profile_name=profile_name)
iam = session.client('iam')
def get_policies_for_role(rolename):
all_policies = {}
# two possible types of policies - inline and managed
inline_policies = iam.list_role_policies(RoleName=rolename)['PolicyNames']
attached_policies = iam.list_attached_role_policies(RoleName=rolename)['AttachedPolicies']
# Get each inline policy
for policy in inline_policies:
all_policies[policy] = iam.get_role_policy(
RoleName=rolename,
PolicyName=policy)['PolicyDocument']
# Get the default VersionId, then use that to retrieve that version of the policy.
for policy in attached_policies:
policy_version = iam.get_policy(
PolicyArn=policy['PolicyArn']
)['Policy']['DefaultVersionId']
all_policies[policy['PolicyArn']] = iam.get_policy_version(
PolicyArn=policy['PolicyArn'],
VersionId=policy_version
)['PolicyVersion']['Document']
return all_policies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment