diff --git a/aws/data_source_aws_launch_template.go b/aws/data_source_aws_launch_template.go index f5d6c68a73d..a8cc54c2d74 100644 --- a/aws/data_source_aws_launch_template.go +++ b/aws/data_source_aws_launch_template.go @@ -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") } - 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) @@ -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