Skip to content

Instantly share code, notes, and snippets.

@junaidk
Created September 19, 2019 06:40
Show Gist options
  • Save junaidk/fa886a92d0fe7d2f07c2f4c225928340 to your computer and use it in GitHub Desktop.
Save junaidk/fa886a92d0fe7d2f07c2f4c225928340 to your computer and use it in GitHub Desktop.
for ROLEIN in $(cat remaining-roles-2 | jq -r ".Roles[] | .RoleName"); do
## filter role on pattern
ROLE=$(echo $ROLEIN | grep -E "\-SR|\-MR|M-CP-X|M-CP-Y")
if [ -z "$ROLE" ]
then
echo ""
else
echo role ${ROLE}
POLICY_ARN=$(aws iam list-attached-role-policies --role-name ${ROLE})
echo pArn ${POLICY_ARN}
INST_PROFILE=$(aws iam list-instance-profiles-for-role --role-name ${ROLE} | jq -r ".InstanceProfiles[] | .InstanceProfileName")
echo instProfile ${INST_PROFILE}
for POLICY in $(echo $POLICY_ARN | jq -r ".AttachedPolicies[] | .PolicyArn"); do
aws iam detach-role-policy --role-name ${ROLE} --policy-arn ${POLICY}
aws iam delete-policy --policy-arn ${POLICY}
done
aws iam remove-role-from-instance-profile --instance-profile-name ${INST_PROFILE} --role-name ${ROLE}
aws iam delete-instance-profile --instance-profile-name ${INST_PROFILE}
aws iam delete-role --role-name ${ROLE}
echo "#-----------#"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment