Skip to content

Commit

Permalink
Merge pull request #20357 from gordonbondon/ec2-launchtemplate-remove…
Browse files Browse the repository at this point in the history
…-unnecessary-api-calls

r/aws_instance: launch_template Remove unnecessary api calls
  • Loading branch information
ewbankkit authored Aug 4, 2021
2 parents b530f67 + 4919eab commit d049121
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changelog/20357.txt
Original file line number Diff line number Diff line change
@@ -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`
```
26 changes: 14 additions & 12 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}

Expand Down

0 comments on commit d049121

Please sign in to comment.