Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when data.aws_launch_template cannot find launch template #10521

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions aws/data_source_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,33 +399,27 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e

dlt, err := conn.DescribeLaunchTemplates(params)

if isAWSErr(err, ec2.LaunchTemplateErrorCodeLaunchTemplateIdDoesNotExist, "") {
log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
}

// AWS SDK constant above is currently incorrect
if isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") {
log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("Error getting launch template: %s", err)
if isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") ||
isAWSErr(err, "InvalidLaunchTemplateName.NotFoundException", "") {
return fmt.Errorf("Launch Template not found")
}
return fmt.Errorf("Error getting launch template: %w", err)
}

if dlt == nil || len(dlt.LaunchTemplates) == 0 {
log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
return fmt.Errorf("error reading launch template: empty output")
}

anGie44 marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("[DEBUG] Found launch template %s", d.Id())
if len(dlt.LaunchTemplates) > 1 {
return fmt.Errorf("Search returned %d result(s), please revise so only one is returned", len(dlt.LaunchTemplates))
}

lt := dlt.LaunchTemplates[0]
d.SetId(*lt.LaunchTemplateId)

log.Printf("[DEBUG] Found launch template %s", d.Id())

d.Set("name", lt.LaunchTemplateName)
d.Set("latest_version", lt.LatestVersionNumber)
d.Set("default_version", lt.DefaultVersionNumber)
Expand All @@ -451,6 +445,10 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e
return err
}

if dltv == nil || len(dltv.LaunchTemplateVersions) == 0 {
return fmt.Errorf("error reading launch template version (%s) for launch template (%s): empty output", version, d.Id())
}

log.Printf("[DEBUG] Received launch template version %q (version %d)", d.Id(), *lt.LatestVersionNumber)

ltData := dltv.LaunchTemplateVersions[0].LaunchTemplateData
Expand Down