Created
November 13, 2022 13:06
-
-
Save dacci/7dc8cc0f6257d12f3ea8f4d3799a9bba to your computer and use it in GitHub Desktop.
dump IAM policies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
iam = boto3.client("iam") | |
list_policies = iam.get_paginator("list_policies") | |
for page in list_policies.paginate(Scope="Local"): | |
for policy in page["Policies"]: | |
tags = iam.list_policy_tags(Arn=policy["Arn"])["Tags"] | |
tags = {x["Key"]: x["Value"] for x in tags} | |
if tags.get("Group", "") != "primary": | |
continue | |
version = iam.get_policy_version( | |
PolicyArn=policy["Arn"], VersionId=policy["DefaultVersionId"] | |
) | |
with open( | |
"{PolicyName}.json".format_map(policy), "w", encoding="UTF-8" | |
) as file: | |
json.dump( | |
version["PolicyVersion"]["Document"], | |
file, | |
ensure_ascii=False, | |
indent=4, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment