Created
November 7, 2019 09:27
-
-
Save junaidk/f9b2b0948a8396d34da40dc23da94d42 to your computer and use it in GitHub Desktop.
Delete Roles in AWS
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
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