changeset 42303:fcb97cb91ff8

automation: detach policies before deleting role You can't delete an IAM role that has attached policies. With this change, the purge-ec2-resources command now works. Differential Revision: https://phab.mercurial-scm.org/D6284
author Gregory Szorc <gregory.szorc@gmail.com>
date Fri, 19 Apr 2019 05:15:43 -0700
parents 730edbd836d8
children dd6a9723ae2b
files contrib/automation/hgautomation/aws.py
diffstat 1 files changed, 4 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/automation/hgautomation/aws.py	Fri Apr 19 05:07:44 2019 -0700
+++ b/contrib/automation/hgautomation/aws.py	Fri Apr 19 05:15:43 2019 -0700
@@ -505,6 +505,10 @@
 
     for role in iamresource.roles.all():
         if role.name.startswith(prefix):
+            for p in role.attached_policies.all():
+                print('detaching policy %s from %s' % (p.arn, role.name))
+                role.detach_policy(PolicyArn=p.arn)
+
             print('removing role %s' % role.name)
             role.delete()