Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion aws/resource_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func resourceAwsIamRole() *schema.Resource {
Update: resourceAwsIamRoleUpdate,
Delete: resourceAwsIamRoleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceAwsIamRoleImport,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -95,6 +95,12 @@ func resourceAwsIamRole() *schema.Resource {
ValidateFunc: validateJsonString,
},

"force_detach_policies": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"create_date": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -103,6 +109,12 @@ func resourceAwsIamRole() *schema.Resource {
}
}

func resourceAwsIamRoleImport(
d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.Set("force_detach_policies", false)
return []*schema.ResourceData{d}, nil
}

func resourceAwsIamRoleCreate(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn

Expand Down Expand Up @@ -254,6 +266,27 @@ func resourceAwsIamRoleDelete(d *schema.ResourceData, meta interface{}) error {
}
}

if d.Get("force_detach_policies").(bool) {
policiesResp, err := iamconn.ListAttachedRolePolicies(&iam.ListAttachedRolePoliciesInput{
RoleName: aws.String(d.Id()),
})
if err != nil {
return fmt.Errorf("Error listing Policies for IAM Role (%s) when trying to delete: %s", d.Id(), err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the descriptive error message here ❤️

}
// Loop and remove the Policies from the Role
if len(policiesResp.AttachedPolicies) > 0 {
for _, i := range policiesResp.AttachedPolicies {
_, err := iamconn.DetachRolePolicy(&iam.DetachRolePolicyInput{
PolicyArn: i.PolicyArn,
RoleName: aws.String(d.Id()),
})
if err != nil {
return fmt.Errorf("Error deleting IAM Role %s: %s", d.Id(), err)
}
}
}
}

request := &iam.DeleteRoleInput{
RoleName: aws.String(d.Id()),
}
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/iam_role.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The following arguments are supported:
* `name` - (Optional, Forces new resource) The name of the role. If omitted, Terraform will assign a random, unique name.
* `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with `name`.
* `assume_role_policy` - (Required) The policy that grants an entity permission to assume the role.
* `force_detach_policies` - (Optional) Specifies to force detaching any policies the role has before destroying it. Defaults to `false`.

~> **NOTE:** This `assume_role_policy` is very similar but slightly different than just a standard IAM policy and cannot use an `aws_iam_policy` resource. It _can_ however, use an `aws_iam_policy_document` [data source](https://www.terraform.io/docs/providers/aws/d/iam_policy_document.html), see example below for how this could work.

Expand Down