How to Create a Resource¶
Quick reference for creating Terraform resources with pyvider. For a step-by-step learning tutorial, see Building Your First Resource.
🤖 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.
Quick Steps¶
- Define runtime types (config and state)
- Create resource class with
@register_resource() - Define schema with
get_schema() - Implement lifecycle methods
- Test with Terraform
Minimal Example¶
API Resource Example¶
For resources that manage remote API objects:
Required Methods¶
| Method | Purpose | Return Type |
|---|---|---|
read() |
Refresh state from remote system | State \| None |
_delete_apply() |
Delete the resource | None |
Optional Methods¶
| Method | Purpose | Default Behavior |
|---|---|---|
_create_apply() |
Custom create logic | Uses _update_apply() |
_update_apply() |
Custom update logic | Uses generic update |
_validate_config() |
Validate configuration | No validation |
ResourceContext API¶
The ctx parameter provides:
Common Patterns¶
Handle Unknown Values¶
During planning, some values may be unknown:
| Python | |
|---|---|
Return Private Data¶
Store sensitive data that shouldn't be in state:
| Python | |
|---|---|
Handle API Errors¶
| Python | |
|---|---|
See Also¶
- Building Your First Resource - Step-by-step tutorial
- Add Validation - Validation patterns
- Resource Lifecycle Reference - Complete API docs
- Testing Resources - Testing strategies