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

Terraform Resource Attribute Set to Null on Second Apply Without Changes #35351

Open
vikashkuma opened this issue Jun 18, 2024 · 3 comments
Open
Labels
bug new new issue not yet triaged waiting-response An issue/pull request is waiting for a response from the community

Comments

@vikashkuma
Copy link

vikashkuma commented Jun 18, 2024

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 vikashkuma added bug new new issue not yet triaged labels Jun 18, 2024
@liamcervante
Copy link
Member

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 liamcervante added the waiting-response An issue/pull request is waiting for a response from the community label Jun 18, 2024
@vikashkuma
Copy link
Author

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

@apparentlymart
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug new new issue not yet triaged waiting-response An issue/pull request is waiting for a response from the community
Projects
None yet
Development

No branches or pull requests

3 participants