Skip to content

Commit

Permalink
resource/aws_ssm_parameter: Handle data_type retries after creation f…
Browse files Browse the repository at this point in the history
…or asynchronous validation process (#14514)

Reference: #14513

Previously (depending on relative distance and asynchronous validation timing of SSM API):

```
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
    TestAccAWSSSMParameter_DataType_AwsEc2Image: testing.go:684: Step 0 error: errors during apply:

        Error: error reading SSM Parameter (tf-acc-test-7552804317262985734) after creation: this can indicate that the provided parameter value could not be validated by SSM
```

Now consistently:

```console
$ TF_ACC=1 go test ./aws -v -count 10 -timeout 120m -parallel 20 -run='TestAccAWSSSMParameter_DataType_AwsEc2Image'
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (8.37s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (8.66s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (10.05s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (7.75s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (9.09s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (8.09s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (8.31s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (8.14s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (8.25s)
=== RUN   TestAccAWSSSMParameter_DataType_AwsEc2Image
=== PAUSE TestAccAWSSSMParameter_DataType_AwsEc2Image
=== CONT  TestAccAWSSSMParameter_DataType_AwsEc2Image
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (7.64s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	85.316s
```

Output from acceptance testing:

```
--- PASS: TestAccAWSSSMParameter_disappears (5.12s)
--- PASS: TestAccAWSSSMParameter_basic (7.31s)
--- PASS: TestAccAWSSSMParameter_secure (8.34s)
--- PASS: TestAccAWSSSMParameter_changeNameForcesNew (12.40s)
--- PASS: TestAccAWSSSMParameter_updateDescription (12.63s)
--- PASS: TestAccAWSSSMParameter_overwrite (13.83s)
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (15.06s)
--- PASS: TestAccAWSSSMParameter_fullPath (17.87s)
--- PASS: TestAccAWSSSMParameter_Tier (18.01s)
--- PASS: TestAccAWSSSMParameter_secure_keyUpdate (19.02s)
--- PASS: TestAccAWSSSMParameter_secure_with_key (22.61s)
--- PASS: TestAccAWSSSMParameter_updateType (24.92s)
--- PASS: TestAccAWSSSMParameter_tags (38.88s)
```
  • Loading branch information
bflad authored Aug 18, 2020
1 parent 748fc23 commit 8313547
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions aws/resource_aws_ssm_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

const (
// Maximum amount of time to wait for asynchronous validation on SSM Parameter creation.
ssmParameterCreationValidationTimeout = 2 * time.Minute
)

func resourceAwsSsmParameter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmParameterPut,
Expand Down Expand Up @@ -108,13 +115,29 @@ func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error

log.Printf("[DEBUG] Reading SSM Parameter: %s", d.Id())

resp, err := ssmconn.GetParameter(&ssm.GetParameterInput{
input := &ssm.GetParameterInput{
Name: aws.String(d.Id()),
WithDecryption: aws.Bool(true),
}

var resp *ssm.GetParameterOutput
err := resource.Retry(ssmParameterCreationValidationTimeout, func() *resource.RetryError {
var err error
resp, err = ssmconn.GetParameter(input)

if isAWSErr(err, ssm.ErrCodeParameterNotFound, "") && d.IsNewResource() && d.Get("data_type").(string) == "aws:ec2:image" {
return resource.RetryableError(fmt.Errorf("error reading SSM Parameter (%s) after creation: this can indicate that the provided parameter value could not be validated by SSM", d.Id()))
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if isAWSErr(err, ssm.ErrCodeParameterNotFound, "") && d.IsNewResource() && d.Get("data_type").(string) == "aws:ec2:image" {
return fmt.Errorf("error reading SSM Parameter (%s) after creation: this can indicate that the provided parameter value could not be validated by SSM", d.Id())
if isResourceTimeoutError(err) {
resp, err = ssmconn.GetParameter(input)
}

if isAWSErr(err, ssm.ErrCodeParameterNotFound, "") && !d.IsNewResource() {
Expand Down

0 comments on commit 8313547

Please sign in to comment.