Terraform Plugin Framework: Use State For Unknown
Computed attributes set on create
This post is part of a series on the Terraform Plugin Framework:
- Terraform Plugin Framework: Optional Attributes With Defaults
- Terraform Plugin Framework: Computed Attributes Known Only After Apply
- Terraform Plugin Framework: Use State For Unknown
Today I learned that there is an official UseStateForUnknown()
plan modifier which has moved around a bit since it was first introduced in v0.5.0 and then appears to have been finalized as typed modifiers in v1.0.0
The proper way to do this now seems to be
"id": schema.StringAttribute{
MarkdownDescription: "internal terraform resource id (matches the uid when the Environment has been created/imported)",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
Then the Create
method is responsible for manually saving that id
value into state once the resource has been created.