Skip to content

Commit 6c5520d

Browse files
authored
Merge pull request #10425 from joelthompson/emr_ig_destroy
Fix AWS EMR Instance Group Deletion Errors
2 parents 56cfd57 + bfb2dea commit 6c5520d

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

aws/resource_aws_emr_cluster_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77
"regexp"
88
"testing"
9+
"time"
910

1011
"github.com/aws/aws-sdk-go/aws"
1112
"github.com/aws/aws-sdk-go/aws/awserr"
@@ -128,6 +129,26 @@ func TestAccAWSEMRCluster_additionalInfo(t *testing.T) {
128129
})
129130
}
130131

132+
func TestAccAWSEMRCluster_disappears(t *testing.T) {
133+
var cluster emr.Cluster
134+
r := acctest.RandInt()
135+
resource.ParallelTest(t, resource.TestCase{
136+
PreCheck: func() { testAccPreCheck(t) },
137+
Providers: testAccProviders,
138+
CheckDestroy: testAccCheckAWSEmrDestroy,
139+
Steps: []resource.TestStep{
140+
{
141+
Config: testAccAWSEmrClusterConfig(r),
142+
Check: resource.ComposeTestCheckFunc(
143+
testAccCheckAWSEmrClusterExists("aws_emr_cluster.tf-test-cluster", &cluster),
144+
testAccCheckAWSEmrClusterDisappears(&cluster),
145+
),
146+
ExpectNonEmptyPlan: true,
147+
},
148+
},
149+
})
150+
}
151+
131152
func TestAccAWSEMRCluster_configurationsJson(t *testing.T) {
132153
var cluster emr.Cluster
133154
r := acctest.RandInt()
@@ -1471,6 +1492,64 @@ func testAccCheckAWSEmrClusterExists(n string, v *emr.Cluster) resource.TestChec
14711492
}
14721493
}
14731494

1495+
func testAccCheckAWSEmrClusterDisappears(cluster *emr.Cluster) resource.TestCheckFunc {
1496+
return func(s *terraform.State) error {
1497+
conn := testAccProvider.Meta().(*AWSClient).emrconn
1498+
id := aws.StringValue(cluster.Id)
1499+
1500+
terminateJobFlowsInput := &emr.TerminateJobFlowsInput{
1501+
JobFlowIds: []*string{cluster.Id},
1502+
}
1503+
1504+
_, err := conn.TerminateJobFlows(terminateJobFlowsInput)
1505+
1506+
if err != nil {
1507+
return err
1508+
}
1509+
1510+
input := &emr.ListInstancesInput{
1511+
ClusterId: cluster.Id,
1512+
}
1513+
var output *emr.ListInstancesOutput
1514+
var instanceCount int
1515+
1516+
err = resource.Retry(20*time.Minute, func() *resource.RetryError {
1517+
var err error
1518+
output, err = conn.ListInstances(input)
1519+
1520+
if err != nil {
1521+
return resource.NonRetryableError(err)
1522+
}
1523+
1524+
instanceCount = countEMRRemainingInstances(output, id)
1525+
1526+
if instanceCount != 0 {
1527+
return resource.RetryableError(fmt.Errorf("EMR Cluster (%s) has (%d) Instances remaining", id, instanceCount))
1528+
}
1529+
1530+
return nil
1531+
})
1532+
1533+
if isResourceTimeoutError(err) {
1534+
output, err = conn.ListInstances(input)
1535+
1536+
if err == nil {
1537+
instanceCount = countEMRRemainingInstances(output, id)
1538+
}
1539+
}
1540+
1541+
if instanceCount != 0 {
1542+
return fmt.Errorf("EMR Cluster (%s) has (%d) Instances remaining", id, instanceCount)
1543+
}
1544+
1545+
if err != nil {
1546+
return fmt.Errorf("error waiting for EMR Cluster (%s) Instances to drain: %s", id, err)
1547+
}
1548+
1549+
return nil
1550+
}
1551+
}
1552+
14741553
func testAccCheckAWSEmrClusterNotRecreated(i, j *emr.Cluster) resource.TestCheckFunc {
14751554
return func(s *terraform.State) error {
14761555
if aws.StringValue(i.Id) != aws.StringValue(j.Id) {

aws/resource_aws_emr_instance_group.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ func resourceAwsEMRInstanceGroupRead(d *schema.ResourceData, meta interface{}) e
200200
return fmt.Errorf("error reading EMR Instance Group (%s): %s", d.Id(), err)
201201
}
202202

203+
if ig.Status != nil {
204+
switch aws.StringValue(ig.Status.State) {
205+
case emr.InstanceGroupStateTerminating:
206+
fallthrough
207+
case emr.InstanceGroupStateTerminated:
208+
log.Printf("[DEBUG] EMR Instance Group (%s) terminated, removing", d.Id())
209+
d.SetId("")
210+
return nil
211+
}
212+
}
213+
203214
switch {
204215
case len(ig.Configurations) > 0:
205216
configOut, err := flattenConfigurationJson(ig.Configurations)

aws/resource_aws_emr_instance_group_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,32 @@ func TestAccAWSEMRInstanceGroup_InstanceCount(t *testing.T) {
201201
})
202202
}
203203

204+
// Regression test for https://github.com/terraform-providers/terraform-provider-aws/issues/1355
205+
func TestAccAWSEMRInstanceGroup_EmrClusterDisappears(t *testing.T) {
206+
var cluster emr.Cluster
207+
var ig emr.InstanceGroup
208+
rInt := acctest.RandInt()
209+
emrClusterResourceName := "aws_emr_cluster.tf-test-cluster"
210+
resourceName := "aws_emr_instance_group.task"
211+
212+
resource.ParallelTest(t, resource.TestCase{
213+
PreCheck: func() { testAccPreCheck(t) },
214+
Providers: testAccProviders,
215+
CheckDestroy: testAccCheckAWSEmrInstanceGroupDestroy,
216+
Steps: []resource.TestStep{
217+
{
218+
Config: testAccAWSEmrInstanceGroupConfig_basic(rInt),
219+
Check: resource.ComposeTestCheckFunc(
220+
testAccCheckAWSEmrClusterExists(emrClusterResourceName, &cluster),
221+
testAccCheckAWSEmrInstanceGroupExists(resourceName, &ig),
222+
testAccCheckAWSEmrClusterDisappears(&cluster),
223+
),
224+
ExpectNonEmptyPlan: true,
225+
},
226+
},
227+
})
228+
}
229+
204230
func TestAccAWSEMRInstanceGroup_EbsConfig_EbsOptimized(t *testing.T) {
205231
var ig emr.InstanceGroup
206232
rInt := acctest.RandInt()

0 commit comments

Comments
 (0)