Skip to content

Instantly share code, notes, and snippets.

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