Skip to content

Commit

Permalink
ec2/launch_template: Add eventual consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
YakDriver committed Nov 11, 2024
1 parent ac141a2 commit 776b440
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions internal/service/ec2/ec2_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"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"
Expand Down Expand Up @@ -1018,6 +1019,10 @@ func resourceLaunchTemplateCreate(ctx context.Context, d *schema.ResourceData, m
return sdkdiag.AppendErrorf(diags, "creating EC2 Launch Template (%s): %s", name, err)
}

if _, err := waitLaunchTemplateReady(ctx, conn, name, true, d.Timeout(schema.TimeoutCreate)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EC2 Launch Template (%s) to be ready: %s", name, err)
}

d.SetId(aws.ToString(output.LaunchTemplate.LaunchTemplateId))

return append(diags, resourceLaunchTemplateRead(ctx, d, meta)...)
Expand Down Expand Up @@ -1151,6 +1156,10 @@ func resourceLaunchTemplateUpdate(ctx context.Context, d *schema.ResourceData, m
}
}

if _, err := waitLaunchTemplateReady(ctx, conn, d.Id(), false, d.Timeout(schema.TimeoutUpdate)); err != nil {

This comment has been minimized.

Copy link
@ryanrupp

ryanrupp Nov 15, 2024

@YakDriver

I hit this issue on an older version (without this change) during a launch template update so was looking if this was fixed and found this issue. On an update though is this handling that a new version was added? Seems like maybe it's just reading that the launch template exists (by ID or name) but in the update case it already exists just a new version was added.

Issue I'm running into for instance was going from launch template v1 -> v2, the final line of the update tries to read back the launch template which has a version check here but then will intermittently fail i.e.

Error: reading EC2 Launch Template (lt-09f5c6bcb89b3299f) Version (2): couldn't find resource

Thanks for the change though!

return sdkdiag.AppendErrorf(diags, "waiting for EC2 Launch Template (%s) to be ready: %s", d.Id(), err)
}

return append(diags, resourceLaunchTemplateRead(ctx, d, meta)...)
}

Expand All @@ -1174,6 +1183,52 @@ func resourceLaunchTemplateDelete(ctx context.Context, d *schema.ResourceData, m
return diags
}

const (
LaunchTemplateFound = "Found"
)

func statusLaunchTemplate(ctx context.Context, conn *ec2.Client, id string, idIsName bool) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
var output *awstypes.LaunchTemplate
var err error
if idIsName {
output, err = findLaunchTemplateByName(ctx, conn, id)
} else {
output, err = findLaunchTemplateByID(ctx, conn, id)
}

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, LaunchTemplateFound, nil
}
}

func waitLaunchTemplateReady(ctx context.Context, conn *ec2.Client, id string, idIsName bool, timeout time.Duration) (*awstypes.LaunchTemplate, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{""},
Target: enum.Slice(LaunchTemplateFound),
Refresh: statusLaunchTemplate(ctx, conn, id, idIsName),
Timeout: timeout,
Delay: 5 * time.Second,
NotFoundChecks: 5,
ContinuousTargetOccurence: 3,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if output, ok := outputRaw.(*awstypes.LaunchTemplate); ok {
return output, err
}

return nil, err
}

func expandRequestLaunchTemplateData(ctx context.Context, conn *ec2.Client, d *schema.ResourceData) (*awstypes.RequestLaunchTemplateData, error) {
apiObject := &awstypes.RequestLaunchTemplateData{
// Always set at least one field.
Expand Down

0 comments on commit 776b440

Please sign in to comment.