Last active
July 25, 2021 20:44
-
-
Save massenz/88483c6ce78e0bf212aa6c928478af9e to your computer and use it in GitHub Desktop.
To delete an AWS IAM Role, Policies need to be detached first: this script automates the tedious necessary steps.
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
#!/usr/bin/env bash | |
# | |
set -eu | |
ROLE=${1:-} | |
if [[ -z ${ROLE} ]]; then | |
printf "Usage: delete-iam-role ROLE\n\nERROR: ROLE must be specified\n" | |
exit 1 | |
fi | |
POLICIES=$(aws iam list-attached-role-policies \ | |
--role-name ${ROLE} | \ | |
jq -r ".AttachedPolicies[].PolicyArn") | |
for policy in ${POLICIES}; do | |
aws iam detach-role-policy \ | |
--role-name ${ROLE} \ | |
--policy-arn ${policy} | |
done | |
aws iam delete-role --role-name ${ROLE} | |
echo "SUCCESS: Done, ${ROLE} deleted" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment