Skip to content

Commit

Permalink
resource/aws_appautoscaling_target: Prevent state removal at creation (
Browse files Browse the repository at this point in the history
…#11819)

References
* #11811

The AWS application-autoscaling service has eventual consistency
considerations. The `aws_appautoscaling_target` resource immediately
tries to read a scaling target after creation. If the target is not
found, the application-autoscaling service returns a 200 OK with an
empty list of scaling targets. Since no scaling target is present, the
`aws_appautoscaling_target` resource removes the created resource from
state, leading to a "produced an unexpected new value for was present,
but now absent" error.

With the changes in this commit, the empty list of scaling targets in
the response for the newly created resource will result in a
NotFoundError being returned and a retry of the read request. A
subsequent retry should hopefully be successful, leading to the state
being preserved.

Output from acceptance testing:

```
> make testacc TEST=./aws TESTARGS='-run=TestAccAWSAppautoScalingTarget_'
...
--- PASS: TestAccAWSAppautoScalingTarget_multipleTargets (30.77s)
--- PASS: TestAccAWSAppautoScalingTarget_optionalRoleArn (32.50s)
--- PASS: TestAccAWSAppautoScalingTarget_spotFleetRequest (77.50s)
--- PASS: TestAccAWSAppautoScalingTarget_basic (107.66s)
--- PASS: TestAccAWSAppautoScalingTarget_emrCluster (783.20s)
```
  • Loading branch information
camlow325 authored Jan 31, 2020
1 parent 9ad6c87 commit b069a0b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion aws/resource_aws_appautoscaling_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,28 @@ func resourceAwsAppautoscalingTargetPut(d *schema.ResourceData, meta interface{}
}

func resourceAwsAppautoscalingTargetRead(d *schema.ResourceData, meta interface{}) error {
var t *applicationautoscaling.ScalableTarget

conn := meta.(*AWSClient).appautoscalingconn

namespace := d.Get("service_namespace").(string)
dimension := d.Get("scalable_dimension").(string)
t, err := getAwsAppautoscalingTarget(d.Id(), namespace, dimension, conn)

err := resource.Retry(2*time.Minute, func() *resource.RetryError {
var err error
t, err = getAwsAppautoscalingTarget(d.Id(), namespace, dimension, conn)
if err != nil {
return resource.NonRetryableError(err)
}
if d.IsNewResource() && t == nil {
return resource.RetryableError(&resource.NotFoundError{})
}
return nil
})
if isResourceTimeoutError(err) {
t, err = getAwsAppautoscalingTarget(d.Id(), namespace, dimension, conn)
}

if err != nil {
return err
}
Expand Down

0 comments on commit b069a0b

Please sign in to comment.