From d094bf51348c88285dde763d7f6bab2326b282f7 Mon Sep 17 00:00:00 2001 From: Adrian Petrescu Date: Tue, 29 Dec 2020 17:43:20 -0500 Subject: [PATCH 1/2] resource/aws_emr_cluster: fix error on missing cluster After a certain amount of time, destroyed EMR clusters completely disappear from AWS API output. They don't just display some deleted state, they're just gone. Currently, if you have one of these clusters in a statefile and you never update it in between the time when it was deleted and when it is purged from the API, terraform crashes the next time you try to run it. This patch fixes that. --- aws/resource_aws_emr_cluster.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/aws/resource_aws_emr_cluster.go b/aws/resource_aws_emr_cluster.go index 0f6810c7660..1df792fe98e 100644 --- a/aws/resource_aws_emr_cluster.go +++ b/aws/resource_aws_emr_cluster.go @@ -978,6 +978,11 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error { resp, err := emrconn.DescribeCluster(req) if err != nil { + if isAWSErr(err, emr.ErrCodeInvalidRequestException, "is not valid") { + log.Printf("[DEBUG] EMR Cluster (%s) not found", d.Id()) + d.SetId("") + return nil + } return fmt.Errorf("Error reading EMR cluster: %s", err) } From d2f821a7aac22923deb6cdbabd5ab68104d434cd Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 5 Jan 2021 14:06:31 -0500 Subject: [PATCH 2/2] Update aws/resource_aws_emr_cluster.go --- aws/resource_aws_emr_cluster.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/aws/resource_aws_emr_cluster.go b/aws/resource_aws_emr_cluster.go index 1df792fe98e..08936cba639 100644 --- a/aws/resource_aws_emr_cluster.go +++ b/aws/resource_aws_emr_cluster.go @@ -978,6 +978,13 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error { resp, err := emrconn.DescribeCluster(req) if err != nil { + // After a Cluster has been terminated for an indeterminate period of time, + // the EMR API will return this type of error: + // InvalidRequestException: Cluster id 'j-XXX' is not valid. + // If this causes issues with masking other legitimate request errors, the + // handling should be updated for deeper inspection of the special error type + // which includes an accurate error code: + // ErrorCode: "NoSuchCluster", if isAWSErr(err, emr.ErrCodeInvalidRequestException, "is not valid") { log.Printf("[DEBUG] EMR Cluster (%s) not found", d.Id()) d.SetId("")