Resource Implementation
Module Overview
Resources are the core building blocks of Terraform providers. They represent infrastructure objects that can be created, read, updated, and deleted. This module teaches you how to implement production-ready resources with full CRUD operations.
What You’ll Learn
By the end of this module, you’ll be able to:
- ✅ Implement resource structs with proper interfaces
- ✅ Handle Create, Read, Update, and Delete operations
- ✅ Manage resource state and plan data
- ✅ Implement resource import functionality
- ✅ Configure resource-level timeouts
- ✅ Handle API errors gracefully
- ✅ Test resources comprehensively
- ✅ Follow resource best practices
Module Chapters
1. Resource Basics
Learn resource structure, interfaces, metadata, and schema definition.
Topics:
- Resource struct and interfaces
- Metadata method
- Schema definition
- Configure method
- Resource lifecycle overview
Time: 30-40 minutes
2. Create Operation
Implement the Create operation to provision new infrastructure resources.
Topics:
- Create method implementation
- Reading plan data
- Making API calls
- Setting state
- Error handling
- Computed attributes
Time: 40-50 minutes
3. Read Operation
Implement the Read operation to sync Terraform state with actual infrastructure.
Topics:
- Read method implementation
- Fetching resource data
- Updating state
- Handling deleted resources
- Drift detection
- Refresh behavior
Time: 35-45 minutes
4. Update Operation
Implement the Update operation to modify existing resources.
Topics:
- Update method implementation
- Reading both plan and state
- Partial updates
- Force replacement
- Update-in-place vs replace
- Complex update scenarios
Time: 40-50 minutes
5. Delete Operation
Implement the Delete operation to destroy infrastructure resources.
Topics:
- Delete method implementation
- API deletion calls
- Error handling
- Already-deleted resources
- Forced deletion
- Cleanup operations
Time: 30-40 minutes
6. Resource Import
Enable importing existing infrastructure into Terraform state.
Topics:
- ImportState interface
- ID parsing
- State population
- Complex import scenarios
- Multi-resource imports
- Import validation
Time: 35-45 minutes
7. Resource Timeouts
Configure operation-specific timeouts for long-running operations.
Topics:
- Timeout schema configuration
- Using timeouts in operations
- Default timeout values
- Async operations
- Polling and waiting
- Timeout best practices
Time: 30-40 minutes
8. Resource Implementation Lab
Build a complete resource with full CRUD operations and import support.
Topics:
- Complete resource implementation
- Database resource example
- All CRUD operations
- Import functionality
- Timeouts configuration
- Testing the resource
Time: 60-75 minutes
9. Resource Practice
Test your knowledge with comprehensive practice questions and scenarios.
Topics:
- CRUD operation questions
- State management scenarios
- Import functionality
- Error handling
- Real-world scenarios
- Best practices
Time: 40-50 minutes
Prerequisites
Before starting this module, you should have completed:
- ✅ Module 3: Development Setup
- ✅ Module 4: Schema Design
- ✅ Module 5: Provider Configuration
Learning Path
Resource Implementation
├── Resource Basics (Ch 1)
│ └── Understand structure and interfaces
├── CRUD Operations (Ch 2-5)
│ ├── Create: Provision resources
│ ├── Read: Sync state
│ ├── Update: Modify resources
│ └── Delete: Destroy resources
├── Advanced Features (Ch 6-7)
│ ├── Import: Existing infrastructure
│ └── Timeouts: Long operations
└── Practice (Ch 8-9)
├── Hands-on lab
└── Assessment
Key Concepts
Resource Lifecycle
Every Terraform resource follows a lifecycle:
terraform apply
↓
Create ──→ State File
↓
Read (refresh)
↓
Update (if changes)
↓
terraform destroy
↓
Delete
Plan vs State
- Plan: Desired configuration (what user wants)
- State: Current reality (what exists in infrastructure)
- Update: Reconcile plan with state
Required Interfaces
// Minimum required
type MyResource struct{}
var _ resource.Resource = &MyResource{}
// For provider-configured client
var _ resource.ResourceWithConfigure = &MyResource{}
// For import support
var _ resource.ResourceWithImportState = &MyResource{}
Resource Example
Here’s a preview of what you’ll build:
# User configuration
resource "mycloud_database" "example" {
name = "my-database"
engine = "postgres"
engine_version = "14.5"
instance_class = "db.t3.medium"
storage_gb = 100
backup_retention_days = 7
tags = {
Environment = "production"
Team = "platform"
}
timeouts {
create = "30m"
update = "20m"
delete = "15m"
}
}
# Import existing database
terraform import mycloud_database.example db-abc123xyz
What Makes a Good Resource?
✅ Idempotent - Same input produces same result
✅ Complete - All CRUD operations implemented
✅ Error handling - Graceful failures with clear messages
✅ State management - Accurate state representation
✅ Importable - Can import existing infrastructure
✅ Tested - Unit and acceptance tests
✅ Documented - Clear examples and descriptions
✅ Performant - Efficient API usage
Common Patterns
1. API Client Pattern
func (r *DatabaseResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan DatabaseModel
req.Plan.Get(ctx, &plan)
// Use shared client
db, err := r.client.CreateDatabase(ctx, ...)
// Update state
resp.State.Set(ctx, plan)
}
2. Error Handling Pattern
if err != nil {
resp.Diagnostics.AddError(
"Failed to Create Database",
fmt.Sprintf("Could not create database: %s", err.Error()),
)
return
}
3. Computed Attributes Pattern
// Set computed values after creation
plan.ID = types.StringValue(db.ID)
plan.ARN = types.StringValue(db.ARN)
plan.Endpoint = types.StringValue(db.Endpoint)
plan.CreatedAt = types.StringValue(db.CreatedAt)
Module Timeline
- Total Time: 5-6 hours
- Chapters: 9
- Hands-on Labs: 1 comprehensive lab
- Practice Questions: 20+ questions
Learning Approach
- Read each chapter thoroughly
- Code along with examples
- Experiment with variations
- Complete the hands-on lab
- Test your knowledge with practice questions
Next Steps
Ready to start implementing resources? Begin with the fundamentals!
Previous Module: ← Provider Configuration
Next Module: Data Sources → (after completing this module)
Additional Resources
Last updated: June 30, 2026