Skip to content

Instantly share code, notes, and snippets.

@massenz
Last active July 25, 2021 20:44
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 massenz/88483c6ce78e0bf212aa6c928478af9e to your computer and use it in GitHub Desktop.
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.
#!/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