Blocks¶
Blocks are nested structures in Terraform schemas that allow repeatable, complex configurations. Pyvider uses block factory functions to create nested blocks.
🤖 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.
What are Blocks?¶
Blocks in Terraform: - Group related attributes into nested structures - Can repeat (lists, sets, maps of blocks) - Can be optional or required - Support nesting (blocks within blocks)
Block Factory Functions¶
Pyvider provides b_* factory functions:
| Python | |
|---|---|
Basic Example¶
Single Block (0 or 1)¶
Terraform usage:
| Terraform | |
|---|---|
List of Blocks (0 or more)¶
Terraform usage:
| Terraform | |
|---|---|
Block Types¶
b_single - Optional Single Block¶
A block that can appear 0 or 1 times:
| Python | |
|---|---|
b_list - List of Blocks¶
Blocks that can appear 0 or more times, order preserved:
| Python | |
|---|---|
b_set - Set of Blocks¶
Blocks that can appear 0 or more times, no guaranteed order:
| Python | |
|---|---|
b_map - Map of Blocks¶
Blocks indexed by a key:
| Python | |
|---|---|
b_group - Required Group Block¶
A block that must appear exactly once:
| Python | |
|---|---|
Nested Blocks¶
Blocks can contain other blocks:
Terraform usage:
| Terraform | |
|---|---|
Complete Example: Complex Resource¶
Here's a comprehensive example with multiple block types:
Terraform usage:
| Terraform | |
|---|---|
| |
Accessing Block Data¶
In your resource implementation, blocks are converted to Python objects:
Block Validation¶
Validate block contents in your resource:
Block Best Practices¶
1. Use Descriptive Block Names¶
| Python | |
|---|---|
2. Choose Appropriate Block Type¶
| Python | |
|---|---|
3. Limit Nesting Depth¶
| Python | |
|---|---|
4. Provide Good Descriptions¶
| Python | |
|---|---|
5. Make Blocks Optional When Appropriate¶
| Python | |
|---|---|
Common Patterns¶
Configuration Blocks¶
| Python | |
|---|---|
Repeatable Rule Blocks¶
| Python | |
|---|---|
Nested Configuration¶
| Python | |
|---|---|
See Also¶
- Schema Overview - Complete schema system guide
- Attributes - Attribute types and options
- Best Practices - Schema design guidelines
- Creating Resources - Using blocks in resources