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

r/aws_instance: Set public_dns and public_ip as newly Computed if instance is stopped/started #40710

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .changelog/40710.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_instance: Set new computed value for `public_dns` and `public_ip` attributes when changing `instance_type`, `user_data`, or `user_data_base64`
```
48 changes: 48 additions & 0 deletions internal/acctest/statecheck/expect_not_known_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package statecheck

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

type expectNotKnownValueCheck struct {
base Base
attributePath tfjsonpath.Path
notValue knownvalue.Check
}

func (e expectNotKnownValueCheck) CheckState(ctx context.Context, request statecheck.CheckStateRequest, response *statecheck.CheckStateResponse) {
resource, ok := e.base.ResourceFromState(request, response)
if !ok {
return
}

value, err := tfjsonpath.Traverse(resource.AttributeValues, e.attributePath)
if err != nil {
response.Error = err

return
}

err = e.notValue.CheckValue(value)
if err == nil {
response.Error = fmt.Errorf("value for attribute at path: %s.%s is %v", resource.Address, e.attributePath.String(), value)

return
}
}

func ExpectNotKnownValue(resourceAddress string, attributePath tfjsonpath.Path, notValue knownvalue.Check) statecheck.StateCheck {
return expectNotKnownValueCheck{
base: NewBase(resourceAddress),
attributePath: attributePath,
notValue: notValue,
}
}
19 changes: 16 additions & 3 deletions internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,24 @@ func resourceInstance() *schema.Resource {

return nil
},
customdiff.ComputedIf("launch_template.0.id", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("launch_template.0.name")
}),
customdiff.ComputedIf("launch_template.0.name", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("launch_template.0.id")
}),
customdiff.ComputedIf("launch_template.0.id", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("launch_template.0.name")
}),
func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
// Set public_dns and public_ip to newly computed if the instance will be stopped and started
// as part of Update and there is already a public_ip value in state.
if diff.Id() != "" && diff.HasChanges(names.AttrInstanceType, "user_data", "user_data_base64") {
if diff.Get("public_ip").(string) != "" {
diff.SetNewComputed("public_dns")
diff.SetNewComputed("public_ip")
}
}

return nil
},
customdiff.ForceNewIf("user_data", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.Get("user_data_replace_on_change").(bool)
}),
Expand Down Expand Up @@ -1821,6 +1833,7 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, meta in
}
}

// See also CustomizeDiff.
if d.HasChanges(names.AttrInstanceType, "user_data", "user_data_base64") && !d.IsNewResource() {
// For each argument change, we start and stop the instance
// to account for behaviors occurring outside terraform.
Expand Down
28 changes: 14 additions & 14 deletions internal/service/ec2/ec2_instance_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_privateIP(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand All @@ -1051,7 +1051,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_secondaryPrivateIPs(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand All @@ -1073,7 +1073,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_ipv6Addresses(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCIPv6Config(rName),
testAccInstanceConfig_vpcIPv6Base(rName),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand Down Expand Up @@ -1126,7 +1126,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_vpc(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand All @@ -1151,7 +1151,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_placementGroup(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_placement_group" "test" {
name = %[1]q
Expand Down Expand Up @@ -1216,8 +1216,8 @@ data "aws_instance" "test" {

func testAccInstanceDataSourceConfig_vpcSecurityGroups(rName string) string {
return acctest.ConfigCompose(acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceVPCSecurityGroupConfig(rName),
testAccInstanceConfig_vpcBase(rName, false, 1),
testAccInstanceConfig_vpcSecurityGroupBase(rName),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand Down Expand Up @@ -1265,7 +1265,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_getUser(rName string, getUserData bool) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand Down Expand Up @@ -1293,7 +1293,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_getUserNoUser(rName string, getUserData bool) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand All @@ -1315,7 +1315,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_autoRecovery(rName string, val string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand All @@ -1340,7 +1340,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_creditSpecification(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand All @@ -1365,7 +1365,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_metaOptions(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 0),
testAccInstanceConfig_vpcBase(rName, false, 0),
acctest.AvailableEC2InstanceTypeForRegion("t3.micro", "t2.micro"),
fmt.Sprintf(`
resource "aws_instance" "test" {
Expand Down Expand Up @@ -1394,7 +1394,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_enclaveOptions(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 0),
testAccInstanceConfig_vpcBase(rName, false, 0),
acctest.AvailableEC2InstanceTypeForRegion("c5a.xlarge", "c5.xlarge"),
fmt.Sprintf(`
resource "aws_instance" "test" {
Expand Down Expand Up @@ -1457,7 +1457,7 @@ data "aws_instance" "test" {
func testAccInstanceDataSourceConfig_disableAPIStopTermination(rName string, disableApiStopTermination bool) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(),
testAccInstanceVPCConfig(rName, false, 1),
testAccInstanceConfig_vpcBase(rName, false, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
Expand Down
Loading
Loading