BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News HashiCorp Terraform Plugin Framework Now Generally Available

HashiCorp Terraform Plugin Framework Now Generally Available

HashiCorp has released the 1.0 version of the Terraform Plugin Framework. This framework improves upon and replaces the current Terraform Plugin SDKv2. It includes support for validators, path expressions, nested attributes, resource private state management, and custom types. Providers written in the new framework are executable binaries written in Go.

The Terraform Plugins work with Terraform Core via RPC (remote procedure calls). Plugins can be used to expose an implementation for a specific service such as a cloud resource, SaaS platform, or API. According to Brian Flad, Dave Parfitt, and Vishnu Ravindra the new framework is an improvement over the previous Terraform Plugin SDKv2:

The Plugin Framework’s code is more maintainable and future-proof, and it will serve as the foundation for future Terraform plugin development features and tooling.

The release includes a number of pre-built validators that provide feedback to users about syntax, types, and acceptable values. For example, there are validators for checking the length of strings or ensuring numbers are within a valid range. Pre-built validators can be found in terraform-plugin-framwork-validators Go module.

Validation can be done on attributes using the generic framework-defined types. The Validators field should be provided with a list of validations as shown in the example below:

schema.StringAttribute{
    // ... other Attribute configuration ...

    Validators: []validator.String{
        // These are example validators from terraform-plugin-framework-validators
        stringvalidator.LengthBetween(10, 256),
        stringvalidator.RegexMatches(
            regexp.MustCompile(`^[a-z0-9]+$`),
            "must contain only lowercase alphanumeric characters",
        ),
    },
}

Path expressions allow for the relationship between attributes to be specified as a relative path. These may represent one or more actual paths within the schema or schema-based data. Relative path expressions are started using the path.MatchRelative() function.

Resource private state management allows for data to be stored alongside the state but withheld from the Terraform plans. HashiCorp suggests this could be used for storing and retrieving values that do not need to be shown to users such as ETags for API calls. Providers are able to save this data during create, import, planning, read, and update operations. The data can be read during delete, planning, read, and update operations. Private state data can be read using the GetKey function:

func (r *resourceExample) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
    
    value, diags := req.Private.GetKey(ctx, "key")
    
    resp.Diagnostics.Append(diags...)
    
    if value != nil {
        // value will be []byte.
        ... 
    }
}

While the Terraform Plugin SDKv2 will continue to be supported, HashiCorp has indicated that the new Terraform Plugin Framework will receive priority for new features and tooling. A migration guide is available for moving plugins from the Terraform Plugin SDKv2 into the new Terraform Plugin Framework.

Plugin framework-based providers can be backward compatible with Terraform versions 0.12 and up. It is also possible to opt-in to use the new Terraform Plugin Protocol version 6. This will allow access to the latest features present in the Terraform 1.x versions. More details about the release can be found in the blog post or on the getting started page.

About the Author

Rate this Article

Adoption
Style

BT