Can’t delete IAM role because policies are attached · Harder to remove IAM than to create it

Views: 755

I am trying to uninstall a role , policy and profile in AWS command line.

I made a script that creates the role, etc but need to make an uninstall script.

~/.local/bin/aws iam create-role –role-name CharmingMachineBashRole –assume-role-policy-document file:///usr/local/sbin/AllowSecGroupCreate.json

~/.local/bin/aws iam attach-role-policy –role-name CharmingMachineBashRole –policy-arn arn:aws:iam::aws:policy/AmazonEC2FullAccess

~/.local/bin/aws iam create-instance-profile –instance-profile-name CharmingMachineBash-Instance-Profile

~/.local/bin/aws iam add-role-to-instance-profile –role-name CharmingMachineBashRole –instance-profile-name CharmingMachineBash-Instance-Profile

For your convenience here is the Json file required

{
“Version”: “2012-10-17”,
“Statement”: [
{
“Effect”: “Allow”,
“Principal”: {
“Service”: “ec2.amazonaws.com”
},
“Action”: “sts:AssumeRole”
}
]
}

Here is the uninstall script that works to undo this

aws iam detach-role-policy –role-name CharmingMachineBashRole –policy-arn arn:aws:iam::aws:policy/AmazonEC2FullAccess

aws iam remove-role-from-instance-profile –instance-profile-name CharmingMachineBash-Instance-Profile –role-name CharmingMachineBashRole

aws iam delete-instance-profile –instance-profile-name CharmingMachineBash-Instance-Profile

aws iam delete-role –role-name CharmingMachineBashRole

 

to get the arn of the offending attached policy, then:

aws iam detach-role-policy  --role-name <your-role-name> --policy-arn <the-arn-from-above>

 

to get the arn of the offending attached policy, then: aws iam detach-role-policy –role-name –policy-arn

Source: Can’t delete IAM role because policies are attached · Issue #2761 · hashicorp/terraform · GitHub

Leave a Reply