diff --git a/aws/resource_aws_appautoscaling_policy.go b/aws/resource_aws_appautoscaling_policy.go index 7e6e0492a52..2a797167676 100644 --- a/aws/resource_aws_appautoscaling_policy.go +++ b/aws/resource_aws_appautoscaling_policy.go @@ -40,6 +40,7 @@ func resourceAwsAppautoscalingPolicy() *schema.Resource { "resource_id": { Type: schema.TypeString, Required: true, + ForceNew: true, }, "scalable_dimension": { Type: schema.TypeString, @@ -368,17 +369,29 @@ func resourceAwsAppautoscalingPolicyDelete(d *schema.ResourceData, meta interfac log.Printf("[DEBUG] Deleting Application AutoScaling Policy opts: %#v", params) err = resource.Retry(2*time.Minute, func() *resource.RetryError { _, err = conn.DeleteScalingPolicy(¶ms) + + if isAWSErr(err, applicationautoscaling.ErrCodeFailedResourceAccessException, "") { + return resource.RetryableError(err) + } + + if isAWSErr(err, applicationautoscaling.ErrCodeObjectNotFoundException, "") { + return nil + } + if err != nil { - if isAWSErr(err, applicationautoscaling.ErrCodeFailedResourceAccessException, "") { - return resource.RetryableError(err) - } return resource.NonRetryableError(err) } return nil }) + + if isResourceTimeoutError(err) { + _, err = conn.DeleteScalingPolicy(¶ms) + } + if err != nil { return fmt.Errorf("Failed to delete scaling policy: %s", err) } + return nil } diff --git a/aws/resource_aws_appautoscaling_policy_test.go b/aws/resource_aws_appautoscaling_policy_test.go index b4499a96ee7..c15ba00b475 100644 --- a/aws/resource_aws_appautoscaling_policy_test.go +++ b/aws/resource_aws_appautoscaling_policy_test.go @@ -43,6 +43,28 @@ func TestAccAWSAppautoScalingPolicy_basic(t *testing.T) { }) } +func TestAccAWSAppautoScalingPolicy_disappears(t *testing.T) { + var policy applicationautoscaling.ScalingPolicy + resourceName := "aws_appautoscaling_policy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAppautoscalingPolicyConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAppautoscalingPolicyExists(resourceName, &policy), + testAccCheckAWSAppautoscalingPolicyDisappears(&policy), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func TestAccAWSAppautoScalingPolicy_scaleOutAndIn(t *testing.T) { var policy applicationautoscaling.ScalingPolicy @@ -212,6 +234,40 @@ func TestAccAWSAppautoScalingPolicy_multiplePoliciesSameResource(t *testing.T) { }) } +// Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/7963 +func TestAccAWSAppautoScalingPolicy_ResourceId_ForceNew(t *testing.T) { + var policy applicationautoscaling.ScalingPolicy + appAutoscalingTargetResourceName := "aws_appautoscaling_target.test" + resourceName := "aws_appautoscaling_policy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAppautoscalingPolicyConfigResourceIdForceNew1(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAppautoscalingPolicyExists(resourceName, &policy), + resource.TestCheckResourceAttrPair(resourceName, "resource_id", appAutoscalingTargetResourceName, "resource_id"), + resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", appAutoscalingTargetResourceName, "scalable_dimension"), + resource.TestCheckResourceAttrPair(resourceName, "service_namespace", appAutoscalingTargetResourceName, "service_namespace"), + ), + }, + { + Config: testAccAWSAppautoscalingPolicyConfigResourceIdForceNew2(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAppautoscalingPolicyExists(resourceName, &policy), + resource.TestCheckResourceAttrPair(resourceName, "resource_id", appAutoscalingTargetResourceName, "resource_id"), + resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", appAutoscalingTargetResourceName, "scalable_dimension"), + resource.TestCheckResourceAttrPair(resourceName, "service_namespace", appAutoscalingTargetResourceName, "service_namespace"), + ), + }, + }, + }) +} + func testAccCheckAWSAppautoscalingPolicyExists(n string, policy *applicationautoscaling.ScalingPolicy) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -221,8 +277,10 @@ func testAccCheckAWSAppautoscalingPolicyExists(n string, policy *applicationauto conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn params := &applicationautoscaling.DescribeScalingPoliciesInput{ - ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), - PolicyNames: []*string{aws.String(rs.Primary.ID)}, + PolicyNames: []*string{aws.String(rs.Primary.ID)}, + ResourceId: aws.String(rs.Primary.Attributes["resource_id"]), + ScalableDimension: aws.String(rs.Primary.Attributes["scalable_dimension"]), + ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), } resp, err := conn.DescribeScalingPolicies(params) if err != nil { @@ -232,6 +290,8 @@ func testAccCheckAWSAppautoscalingPolicyExists(n string, policy *applicationauto return fmt.Errorf("ScalingPolicy %s not found", rs.Primary.ID) } + *policy = *resp.ScalingPolicies[0] + return nil } } @@ -258,6 +318,23 @@ func testAccCheckAWSAppautoscalingPolicyDestroy(s *terraform.State) error { return nil } +func testAccCheckAWSAppautoscalingPolicyDisappears(policy *applicationautoscaling.ScalingPolicy) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn + + input := &applicationautoscaling.DeleteScalingPolicyInput{ + PolicyName: policy.PolicyName, + ResourceId: policy.ResourceId, + ScalableDimension: policy.ScalableDimension, + ServiceNamespace: policy.ServiceNamespace, + } + + _, err := conn.DeleteScalingPolicy(input) + + return err + } +} + func testAccAWSAppautoscalingPolicyConfig(rName string) string { return fmt.Sprintf(` resource "aws_ecs_cluster" "test" { @@ -676,3 +753,142 @@ resource "aws_appautoscaling_policy" "foobar_in" { } `, randClusterName, randPolicyNamePrefix, randPolicyNamePrefix) } + +func testAccAWSAppautoscalingPolicyConfigResourceIdForceNewBase(rName string) string { + return fmt.Sprintf(` +resource "aws_ecs_cluster" "test" { + name = %[1]q +} + +resource "aws_ecs_task_definition" "test" { + family = %[1]q + container_definitions = <