Why default attribute must also be computed?

I’m using the terraform plugin framework.

Here is a simplified schema:

func (t *T) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
    resp.Schema = return schema.Schema{
      "name": schema.StringAttribute{
                Optional: true,
                Default:  stringdefault.StaticString("john_doe"),
            },

    }
}

When I do terraform apply I get this error:

│ Error: Failed to load plugin schemas
│
│ Error while loading schemas for plugin components: Failed to obtain provider schema: Could not load the schema for provider my_provider: failed to retrieve schema from provider
│ "my_provider": Schema Using Attribute Default For Non-Computed Attribute: Attribute "name" must be computed when using default. This is an issue with the provider and should be
│ reported to the provider developers...

Why do attributes with a default value have to be computed ?

Hi @picavoh.mobijaf

Terraform has no concept of default values in a resource configuration, and initially only has access to the value as written in the config file. Following the resource lifecycle rules, in order for a provider to change a null configuration value during the plan it must be marked as computed. A default therefore is really just a computed value which returns a known value during the plan, as opposed to an unknown value which will be filled in during apply.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.