Skip to content

Commit

Permalink
Merge pull request #38969 from hashicorp/b-cfn-crash
Browse files Browse the repository at this point in the history
r/aws_cloudformation_stack_set_instance: fix crash during ID construction
  • Loading branch information
jar-b authored Aug 22, 2024
2 parents ecfff50 + afcd111 commit bbbac07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/38969.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_cloudformation_stack_set_instance: Fix crash during construction of the `id` attribute when `deployment_targets` does not include organizational unit IDs.
```
15 changes: 12 additions & 3 deletions internal/service/cloudformation/stack_set_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
Expand All @@ -32,6 +33,8 @@ import (
)

const (
ResNameStackSetInstance = "Stack Set Instance"

stackSetInstanceResourceIDPartCount = 3
)

Expand Down Expand Up @@ -249,7 +252,9 @@ func resourceStackSetInstanceCreate(ctx context.Context, d *schema.ResourceData,

if v, ok := d.GetOk("deployment_targets"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
dt := expandDeploymentTargets(v.([]interface{}))
accountOrOrgID = strings.Join(dt.OrganizationalUnitIds, "/")
if len(dt.OrganizationalUnitIds) > 0 {
accountOrOrgID = strings.Join(dt.OrganizationalUnitIds, "/")
}
input.DeploymentTargets = dt
} else {
d.Set(names.AttrAccountID, accountID)
Expand All @@ -269,8 +274,12 @@ func resourceStackSetInstanceCreate(ctx context.Context, d *schema.ResourceData,
input.OperationPreferences = expandOperationPreferences(v.([]interface{})[0].(map[string]interface{}))
}

id := errs.Must(flex.FlattenResourceId([]string{stackSetName, accountOrOrgID, region}, stackSetInstanceResourceIDPartCount, false))
_, err := tfresource.RetryWhen(ctx, propagationTimeout,
id, err := flex.FlattenResourceId([]string{stackSetName, accountOrOrgID, region}, stackSetInstanceResourceIDPartCount, false)
if err != nil {
return create.AppendDiagError(diags, names.CloudFormation, create.ErrActionFlatteningResourceId, ResNameStackSetInstance, id, err)
}

_, err = tfresource.RetryWhen(ctx, propagationTimeout,
func() (interface{}, error) {
input.OperationId = aws.String(sdkid.UniqueId())

Expand Down

0 comments on commit bbbac07

Please sign in to comment.