hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.

Home Page:https://www.terraform.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Terraform Resource Attribute Set to Null on Second Apply Without Changes

vikashkuma opened this issue · 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      = "john.doe@example.com" -> 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      = "john.doe@example.com"
  	+ 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      = "john.doe@example.com"
  age        = 30
  is_manager = true

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

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 I have updated the issue description with required details.
Could you please take a look and let me know.
Thanks

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.