terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Terraform Resource Attribute Set to Null on Second Apply Without Changes

Open vikashkuma opened this issue 1 year ago • 3 comments

Terraform Version

Terraform v1.7.4
on windows_386
+ provider registry.terraform.io/appName/appName v1.1.0

Terraform Configuration Files

terraform {
  required_providers {
    appName = {
      source = "appName/appName"
      version = "1.1.0"
    }
  }
}

provider "appName" {
  # Configuration options
}

Debug Output

employeeSchema{
          -  name       = "John Doe" -> null
          -  id         = "12345" -> null
  	- email      = "[email protected]" -> null
  	- age        = 30 -> null
  	- is_manager = true -> null

  	- address {
    		- street     = "123 Main St" -> null
    		- city       = "Anytown" -> null
    		- state      = "CA" -> null
    		- zip        = "90210" -> null
    		- is_primary = true -> null
  	}
                
        }

      + employeeSchema{
          +  name       = "John Doe"
          +  id         = "12345"
  	+ email      = "[email protected]"
  	+ age        = 30
  	+ is_manager = true

  	+ address {
    		+ street     = "123 Main St"
    		+ city       = "Anytown"
    		+ state      = "CA"
    		+ zip        = "90210"
    		+ is_primary = true 
                        }
          }

Expected Behavior

Terraform should detect no changes and confirm the infrastructure is up-to-date without setting any resource attributes to null.

To elaborate, when running terraform apply a second time without making any changes to the resources, Terraform should recognize that the current state matches the desired configuration as defined in the .tf files. It should not alter the existing resources or their attributes, and it should certainly not set any previously defined attributes (e.g., name = "John Doe") to null.

Actual Behavior

When running terraform apply a second time without making any changes to the Terraform configuration, the expected behavior is for Terraform to recognize that no changes are required and confirm that the infrastructure is already up-to-date. However, the actual behavior observed is that a specific resource attribute, name, is being unexpectedly set to null. This indicates a discrepancy between the Terraform state and the actual configuration or the infrastructure, resulting in unintended modifications to the resource state.

Steps to Reproduce

  1. terraform apply
  2. terraform apply

Additional Context

No response

References

github issue not yet created

employeeSchema := map[string]*schema.Schema{
		"name": {
			Type:     schema.TypeString,
			Required: true,
		},
		"id": {
			Type:     schema.TypeString,
			Required: true,
		},
		"email": {
			Type:     schema.TypeString,
			Optional: true,
		},
		"age": {
			Type:     schema.TypeInt,
			Optional: true,
		},
		"is_manager": {
			Type:     schema.TypeBool,
			Optional: true,
		},
		"address": {
			Type:     schema.TypeList,
			Optional: true,
			MaxItems: 1,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"street": {
						Type:     schema.TypeString,
						Optional: true,
					},
					"city": {
						Type:     schema.TypeString,
						Optional: true,
					},
					"state": {
						Type:     schema.TypeString,
						Optional: true,
					},
					"zip": {
						Type:     schema.TypeString,
						Optional: true,
					},
					"is_primary": {
						Type:     schema.TypeBool,
						Optional: true,
					},
				},
			},
		},
	
resource "yourprovider_employee" "example" {
  name       = "John Doe"
  id         = "12345"
  email      = "[email protected]"
  age        = 30
  is_manager = true

  address {
    street     = "123 Main St"
    city       = "Anytown"
    state      = "CA"
    zip        = "90210"
    is_primary = true
  }
}

vikashkuma avatar Jun 18 '24 03:06 vikashkuma

Hi @vikashkuma, are you able to share the resource configuration for the resource that is changing? Based on the diff it looks like an employeeSchema block is being removed and a new one added with a different email address rather than anything being set to null.

It's difficult for me to replicate this without knowing which resource you are using and the config of that resource.

Thanks!

liamcervante avatar Jun 18 '24 08:06 liamcervante

@liamcervante I have updated the issue description with required details. Could you please take a look and let me know. Thanks

vikashkuma avatar Jun 18 '24 17:06 vikashkuma

Hi @vikashkuma,

Symptoms like what you've described are more likely to be caused by the provider plugin than by Terraform Core, because it's ultimately the provider plugin that decides how to resolve any apparent differences between the desired state (from the configuration) and the prior state.

Therefore I expect that to reproduce this would require us to be able to use the same provider you are using. It seems like you are writing a new provider yourself, rather than using an existing provider.

Although I don't think we will be able to reproduce it directly without access to your full provider source code, we can probably get some clues as to what's going wrong if you run your second terraform plan with the environment variable TF_LOG=trace set, and then share the resulting verbose logs as a GitHub Gist. For providers using the legacy plugin SDK (which yours appears to be), Terraform Core often includes some additional notes in the trace log about apparent provider misbehavior which, if present in your log, will hopefully give us a clue as to what might be causing the strange behavior you observed here.

apparentlymart avatar Jun 18 '24 17:06 apparentlymart

Since we have not heard back in a while I'm going to close the issue. If you have any updates regarding the issue, feel free to open a new issue with the requested information. If you have more questions, you can also use the community forum where there are more people ready to help.

Thanks!

jbardin avatar Oct 18 '24 17:10 jbardin

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Nov 18 '24 02:11 github-actions[bot]