How can I fix "No Update defined, must set ForceNew on" in terraform?

Viewed 527

Here's my tf schema for a resource:

Schema: map[string]*schema.Schema{
    "config_sensitive": {
        Type:        schema.TypeMap,
        Optional:    true,
        ForceNew:    false,
        Sensitive:   true,
        Description: "Sensitive part of connector configuration. String keys and values",
    },

When I try to compile it, I run into:

resource ...: No Update defined, must set ForceNew on: []string{"config_sensitive"}.

Is there a way to get rid of this error without setting ForceNew: true (I can't figure out what exactly an Update is)?

1 Answers

In Terraform, when a resource's arguments in the config are passed different values according to the current values in the state, and a corresponding resource exists according to the state, the provider must attempt to alter the existing resource to match the new values in the config. There are various functions defined in the schema to support a resource, but at the most basic there exists a corresponding function to support the four basic operations: Create, Read, Update, and Delete. When the Terraform provider attempts to alter the resource to match the new values in the config, it will attempt either an Update (denoted in plans by ~), or a Delete followed by a Create (denoted in plans by -/+).

In this situation, you have assigned false to ForceNew in the resource schema. You also have not defined an Update function. The consequences would be that the provider would be unable to alter an existing resource to match new values in a config.

To fix this problem, you either must specify ForceNew as true (this would force a Delete/Create to alter the resource), or define an Update function to alter the existing resource attributes without deleting it and creating a new one. You can specify the update function in the resource schema like:

&schema.Resource {
  Update: <name of update function>,
  ...
}

Note if you are using plugin sdk version 2, then it should lilely be UpdateContext instead.

Related