Schema System¶
The Pyvider schema system is the bridge between Python and Terraform's type systems. It provides a declarative way to define the structure and constraints of your provider's resources, data sources, and functions.
🤖 AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
Why Schemas Matter¶
Schemas serve multiple purposes in Pyvider:
- Type Safety: Define what data flows between Terraform and your provider
- Validation: Ensure user inputs meet requirements before execution
- Documentation: Descriptions appear in Terraform documentation
- Terraform Integration: Automatically translated to Terraform's protocol format
Quick Example¶
Here's how schemas work in practice:
This schema tells Terraform:
- Users must provide a name (string, required)
- port is optional with a default of 8080
- id and ip_address are computed by the provider
Schema Factories¶
Pyvider uses factory functions to build schemas:
| Python | |
|---|---|
These factories create type-safe schema definitions that Pyvider automatically converts to Terraform's protocol format.
Key Concepts¶
Attributes vs Blocks¶
- Attributes: Simple or collection values (
a_str(),a_num(),a_list()) - Blocks: Nested configuration structures (
b_list(),b_single())
Required, Optional, and Computed¶
| Python | |
|---|---|
Type Mapping¶
Pyvider automatically maps Python types to Terraform types:
| Python | Terraform | Factory |
|---|---|---|
str |
string |
a_str() |
int, float |
number |
a_num() |
bool |
bool |
a_bool() |
list[T] |
list(T) |
a_list(T) |
dict[str, T] |
map(T) |
a_map(T) |
How It Works¶
When you define a schema:
- Build Time: Factory functions create a schema structure
- Startup: Pyvider converts schemas to Terraform protocol format
- Runtime: Data flows between Terraform ↔ Python via automatic conversion
- Type Safety: Your
@attrs.defineclasses get properly typed data
graph LR
TF[Terraform HCL] --> |protocol| PV[Pyvider Schema]
PV --> |conversion| PY[Python attrs]
PY --> |your code| RES[Resource Methods]
RES --> |returns| PY2[Python attrs]
PY2 --> |conversion| PV2[Pyvider Schema]
PV2 --> |protocol| TF2[Terraform State]
Best Practices¶
- Always add descriptions - They appear in Terraform docs
- Use appropriate defaults - Make common cases easy
- Mark sensitive data - Use
sensitive=Truefor passwords, tokens - Validate inputs - Add validators to catch errors early
- Keep schemas simple - Prefer flat structures when possible
Complete Schema Reference¶
This overview introduces the core concepts. For comprehensive documentation including:
- Detailed attribute reference - All available types and modifiers
- Nested blocks - Complex configuration structures
- Validators - Input validation techniques
- Advanced patterns - Schema composition and reuse
- Examples - Real-world schema definitions
See the Complete Schema Documentation →
Continue to Creating Providers →