(terraform-plugin-framework) Required attribute and environment variables

How can I represent the following SDKv2 schema attribute in the plugin framework:

"username": {
	Type:        schema.TypeString,
	Required:    true,
	DefaultFunc: schema.EnvDefaultFunc("USERNAME", nil),

My (flaved) migration currently is:

"username": {
	Optional: true,
	Type:     types.StringType,
	PlanModifiers: tfsdk.AttributePlanModifiers{

where DefaultValueFromEnvironment is a simple modification of the DefaultValue attribute plan modifier.

The problem is that I can’t mark the attribute as required (as in either set in the resource, or from the environment variable), since required seems to be checked a long time before the attribute plan modifiers are ran.

1 Like

Hi Magne, I got the same issue here… I wonder whether you were able to find a solution or workaround for this.

Sorry, but I never found a solution. There seems to be little progress on the new API, and even less interest in supporting and answering early adopters.

Not sure if it is a solution, cause I’m currently struggling with “defaults” myself, but might be worth a shot.

Core Framework v1.2.0 introduces default values.
So it might be possible to simply set the default to the os.Getenv("USERNAME")

Default:             stringdefault.StaticString(os.Getenv("USERNAME")),

Don’t forget to add.


Hi @magne,

I apologize for the delay. Is the username attribute defined in a Provider schema? And is it a top-level attribute or defined within a Block?

In general, we don’t allow the Required field to be defined with the PlanModifiers or Default fields for Resources in the Framework to prevent the Provider from overwriting any user configuration. In either of those cases the attribute would need to be marked Optional and Computed.

If this is an attribute in a Provider schema, then since newer versions of the Framework no longer allow PlanModifiers to be set in Provider schemas (there’s no concept of “planning” at the Provider level), you would move the logic of your DefaultValueFromEnvironment() function to the Provider level Configure() method instead.