Resource Lifecycle API Reference¶
Complete API reference for resource lifecycle methods and the ResourceContext object.
🤖 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.
Base Resource Class¶
| Python | |
|---|---|
Class Attributes¶
| Attribute | Type | Required | Description |
|---|---|---|---|
config_class |
Type[attrs.define] |
Yes | Configuration attrs class |
state_class |
Type[attrs.define] |
Yes | State attrs class |
Lifecycle Methods¶
Required Methods¶
read()¶
| Python | |
|---|---|
Purpose: Check if the resource still exists and return its current state.
Parameters:
- ctx: ResourceContext with current state
Returns:
- StateType: Current state if resource exists
- None: Resource no longer exists (deleted outside Terraform)
When Called: During terraform plan and terraform refresh
Example:
| Python | |
|---|---|
_delete_apply()¶
Purpose: Remove the resource from the remote system.
Parameters:
- ctx: ResourceContext with current state
Returns: None
When Called: During terraform destroy or when resource removed from config
Example:
| Python | |
|---|---|
Optional Methods¶
_create_apply()¶
| Python | |
|---|---|
Purpose: Create a new resource.
Parameters:
- ctx: ResourceContext with configuration
Returns:
- Tuple of (state, private_data)
- state: New state to track
- private_data: Sensitive data (not in state file)
When Called: During terraform apply when creating new resource
Default Behavior: Calls _update_apply() if not overridden
Example:
_update_apply()¶
| Python | |
|---|---|
Purpose: Modify an existing resource.
Parameters:
- ctx: ResourceContext with configuration and current state
Returns:
- Tuple of (state, private_data)
- state: Updated state
- private_data: Sensitive data (not in state file)
When Called: During terraform apply when updating existing resource
Example:
_validate_config()¶
| Python | |
|---|---|
Purpose: Validate user configuration before applying.
Parameters:
- config: Typed configuration object
Returns: - List of error messages (empty list = valid)
When Called: During terraform plan and terraform apply
Example:
| Python | |
|---|---|
ResourceContext API¶
Properties¶
config¶
| Python | |
|---|---|
Typed configuration attrs instance. None if configuration contains unknown values.
Example:
config_cty¶
| Python | |
|---|---|
Raw CTY value from Terraform, always available even with unknown values.
Example:
| Python | |
|---|---|
state¶
| Python | |
|---|---|
Current state. None during create operations.
Example:
planned_state¶
| Python | |
|---|---|
Planned state from plan phase.
Example:
| Python | |
|---|---|
Methods¶
is_field_unknown()¶
| Python | |
|---|---|
Check if a configuration field has an unknown value (e.g., depends on another resource).
Parameters:
- field_name: Name of the configuration field
Returns: True if value is unknown
Example:
| Python | |
|---|---|
add_error()¶
| Python | |
|---|---|
Add an error diagnostic to show in Terraform output.
Parameters:
- message: Error message
Example:
| Python | |
|---|---|
add_warning()¶
| Python | |
|---|---|
Add a warning diagnostic to show in Terraform output.
Parameters:
- message: Warning message
Example:
| Python | |
|---|---|
Schema Definition¶
get_schema()¶
Returns: PvsSchema object defining the resource schema
Example:
Type Signatures¶
Configuration Class¶
| Python | |
|---|---|
State Class¶
| Python | |
|---|---|
Private Data¶
Sensitive data that shouldn't be stored in state:
Access private data:
| Python | |
|---|---|
Lifecycle Sequence¶
Create Resource¶
- User adds resource to Terraform config
terraform plan:- Calls
_validate_config()with configuration - Calls
get_schema()to validate schema terraform apply:- Calls
_create_apply()with ctx.config - Stores returned state
Update Resource¶
- User modifies resource in Terraform config
terraform plan:- Calls
read()to get current state - Calls
_validate_config()with new configuration - Compares current state with planned state
terraform apply:- Calls
_update_apply()with ctx.config and ctx.state - Stores returned state
Delete Resource¶
- User removes resource from Terraform config
terraform plan:- Shows resource will be deleted
terraform apply:- Calls
_delete_apply()with ctx.state - Removes resource from state
Refresh State¶
terraform refreshorterraform plan:- Calls
read()with ctx.state - Updates state if changed
- Removes from state if
read()returnsNone
See Also¶
- Create a Resource - How-to guide
- Building Your First Resource - Tutorial
- Add Validation - Validation patterns
- API Reference - Auto-generated API docs