Created
December 6, 2022 02:21
-
-
Save dazza-codes/4e5688ea808c22d6477a097805a5e7ca to your computer and use it in GitHub Desktop.
Get AWS IAM Role Documents
This file contains 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
#!/usr/bin/env bash | |
mkdir -p iam_roles | |
if [ ! -f roles.json ]; then | |
aws iam list-roles --output json > iam_roles/roles.json | |
fi | |
roles=$(jq '.Roles[].RoleName' iam_roles/roles.json | sed -n 's/"//gp') | |
for role in ${roles}; do | |
echo "$role" | |
aws iam get-role --role-name "$role" --output json > "iam_roles/${role}.json" | |
aws iam list-attached-role-policies --role-name "$role" --output json > "iam_roles/${role}_policies.json" | |
rm -f "iam_roles/${role}_policy_docs.json" | |
rm -f "iam_roles/${role}_policy_docs.yaml" | |
policies=$(jq '.AttachedPolicies[].PolicyArn' "iam_roles/${role}_policies.json" | sed 's/"//g') | |
for policy in $policies; do | |
echo " $policy" | |
policy_ver=$(aws iam get-policy --policy-arn "$policy" | jq '.Policy.DefaultVersionId' | sed 's/"//g') | |
aws iam get-policy-version \ | |
--policy-arn "$policy" --version-id "$policy_ver" \ | |
--output json >> "iam_roles/${role}_policy_docs.json" | |
echo "--- " >> "iam_roles/${role}_policy_docs.yaml" | |
aws iam get-policy-version \ | |
--policy-arn "$policy" --version-id "$policy_ver" \ | |
--output yaml >> "iam_roles/${role}_policy_docs.yaml" | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment