From c19023f0be936d06fb40460d5def2423279d8cca Mon Sep 17 00:00:00 2001 From: Artem Yarmoliuk Date: Thu, 29 Jul 2021 20:24:55 +0800 Subject: [PATCH 1/2] r/aws_instance: launch_template Remove unnecessary api calls --- aws/resource_aws_instance.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_instance.go b/aws/resource_aws_instance.go index 6e50473399f..ecedf0ca447 100644 --- a/aws/resource_aws_instance.go +++ b/aws/resource_aws_instance.go @@ -639,7 +639,9 @@ func resourceAwsInstance() *schema.Resource { CustomizeDiff: customdiff.All( SetTagsDiff, func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error { - if diff.HasChange("launch_template.0.version") { + _, ok := diff.GetOk("launch_template") + + if diff.Id() != "" && diff.HasChange("launch_template.0.version") && ok { conn := meta.(*AWSClient).ec2conn stateVersion := diff.Get("launch_template.0.version") @@ -3095,7 +3097,7 @@ func getAwsInstanceLaunchTemplate(conn *ec2.EC2, d *schema.ResourceData) ([]map[ name, defaultVersion, latestVersion, err := getAwsLaunchTemplateSpecification(conn, id) if err != nil { - if isAWSErr(err, "InvalidLaunchTemplateId.Malformed", "") { + if isAWSErr(err, "InvalidLaunchTemplateId.Malformed", "") || isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") { // Instance is tagged with non existent template just set it to nil log.Printf("[WARN] Launch template %s not found, removing from state", id) return nil, nil @@ -3106,42 +3108,42 @@ func getAwsInstanceLaunchTemplate(conn *ec2.EC2, d *schema.ResourceData) ([]map[ attrs["id"] = id attrs["name"] = name - version, err := getAwsInstanceLaunchTemplateVersion(conn, d.Id()) + liveVersion, err := getAwsInstanceLaunchTemplateVersion(conn, d.Id()) if err != nil { return nil, err } dltvi := &ec2.DescribeLaunchTemplateVersionsInput{ LaunchTemplateId: aws.String(id), - Versions: []*string{aws.String(version)}, + Versions: []*string{aws.String(liveVersion)}, } if _, err := conn.DescribeLaunchTemplateVersions(dltvi); err != nil { if isAWSErr(err, "InvalidLaunchTemplateId.VersionNotFound", "") { // Instance is tagged with non existent template version, just don't set it - log.Printf("[WARN] Launch template %s version %s not found, removing from state", id, version) + log.Printf("[WARN] Launch template %s version %s not found, removing from state", id, liveVersion) result = append(result, attrs) return result, nil } return nil, fmt.Errorf("error reading Launch Template Version: %s", err) } - if v, ok := d.GetOk("launch_template.0.version"); ok { - switch v { + if stateVersion, ok := d.GetOk("launch_template.0.version"); ok { + switch stateVersion { case "$Default": - if version == defaultVersion { + if liveVersion == defaultVersion { attrs["version"] = "$Default" } else { - attrs["version"] = version + attrs["version"] = liveVersion } case "$Latest": - if version == latestVersion { + if liveVersion == latestVersion { attrs["version"] = "$Latest" } else { - attrs["version"] = version + attrs["version"] = liveVersion } default: - attrs["version"] = version + attrs["version"] = liveVersion } } From 4919eabb953073f1fc4a099608f4cd966e37040b Mon Sep 17 00:00:00 2001 From: Artem Yarmoliuk Date: Mon, 2 Aug 2021 19:29:06 +0800 Subject: [PATCH 2/2] Add changelog --- .changelog/20357.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changelog/20357.txt diff --git a/.changelog/20357.txt b/.changelog/20357.txt new file mode 100644 index 00000000000..af9f5f14525 --- /dev/null +++ b/.changelog/20357.txt @@ -0,0 +1,7 @@ +```release-note:bug +aws/resource_aws_instance: Fix state refresh when launch template was deleted +``` + +```release-note:bug +aws/resource_aws_instance: Fix running `terraform plan` with with `skip_credentials_validation=true` +```