tfschema-bindgen
This crate aims to compile schemas extracted from Terraform providers into Serde type definitions.
Quick Start
A Terraform schema is required for generating Rust types responsible of deserialization and serialization. It can either be exported from your Terraform configuration or manually generated. We'll take the latter approach, therefore defining a reference schema with just one provider type having one attribute:
In addition to a Rust library, this crate provides a binary tool tfbindgen
to process Terraform schemas
saved on disk.
Outside of this repository, you may install the tool with:
Then use $HOME/.cargo/bin/tfbindgen
.
We're going to use this tool assuming that we're inside the repository.
The following command will generate Serde bindings from the previous definitions, outputting those to test.rs
module:
The following is a Rust example snippet comprising the previously generated bindings and a main function building on these in order deserialize a configuration descriptor adhering to our Terraform schema:
use BTreeMap as Map;
use ;
use ByteBuf as Bytes;
const TF_JSON_CONFIG: &str = r#"{
"provider": [
{
"test_provider": [
{
"base_url": "https://acme.com/foo"
}
]
}
]
}"#;
Quickstart Example
In addition to a Rust library and generation tool, this crate provides the above example which can be executed using the following command:
Consuming third-party Terraform schemas
In order to operate on Terraform configuration descriptors of third-party providers, Rust bindings have to be generated using the provided schema descriptor in the JSON format.
Firstly, create a minimal Terraform configuration declaring the target provider. The following is an example for enabling the Amazon Web Services (AWS) Terraform provider:
provider "aws" {
version = ">= 2.31.0, < 3.0"
}
Initialize Terraform so that configured providers are installed in the local environment:
Secondly, extract the schema for the providers defined in the Terraform configuration, AWS in this case:
Finally, generate the Rust (de)serialization types for the given provider using the following command (assuming you are inside the repository):
In order do (de)serialize provider's configuration, import the generated module in your application.
License
This project is available under the terms of either the Apache 2.0 license or the MIT license.