Tutorial: Building an HTTP API Provider¶
This tutorial walks you through building a real Terraform provider for a REST API service. You'll learn production patterns for authentication, error handling, caching, and testing.
🤖 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 We'll Build¶
A provider for the JSONPlaceholder API (a fake REST API for testing): - Provider: Authentication and configuration - Resources: Posts and Comments (CRUD operations) - Data Sources: User lookups - Functions: Text validation
API Documentation: https://jsonplaceholder.typicode.com/
Prerequisites¶
- Python 3.11+
- Basic understanding of Pyvider (complete Quick Start first)
- Familiarity with REST APIs
- Knowledge of async/await in Python
Project Setup¶
1. Create Project Structure¶
2. Create pyproject.toml¶
3. Install Dependencies¶
| Bash | |
|---|---|
Step 1: Build the Provider¶
Create jsonplaceholder_provider/provider/provider.py:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
Step 2: Create Post Resource¶
Create jsonplaceholder_provider/resources/post.py:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
Step 3: Create User Data Source¶
Create jsonplaceholder_provider/data_sources/user.py:
Step 4: Test the Provider¶
Create jsonplaceholder_provider/tests/test_post.py:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
Run tests:
| Bash | |
|---|---|
Step 5: Use with Terraform¶
Create main.tf:
Install and run:
| Bash | |
|---|---|
Key Takeaways¶
Patterns You Learned¶
- Provider Configuration
- Schema definition with validation
- HTTP client setup with connection pooling
-
Retry logic for resilience
-
Resource Implementation
- Full CRUD lifecycle
- Error handling
-
Logging
-
Data Source Implementation
- Read-only data fetching
-
Error handling for missing data
-
Testing
- Async test patterns
- Resource lifecycle testing
- Schema validation
Production Considerations¶
For a real provider, also implement:
- Caching - Cache frequently accessed data
- Rate Limiting - Respect API rate limits
- Pagination - Handle paginated responses
- Authentication - OAuth, API keys, etc.
- Private State - For sensitive data
- Import Support - Allow importing existing resources
Next Steps¶
- Add the comment resource (similar to post)
- Add a validate_text function
- Implement caching for user lookups
- Add comprehensive error handling
- Write integration tests
- Add more data sources (posts by user, etc.)
Related Documentation¶
- Creating Providers - Complete provider guide
- Creating Resources - Resource patterns
- Creating Data Sources - Data source patterns
- Testing Providers - Testing strategies
- Error Handling - Error patterns
- Security Best Practices - Security guidance
- Performance Optimization - Performance tips