awslabs.terraform-mcp-server 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of awslabs.terraform-mcp-server might be problematic. Click here for more details.

Files changed (32) hide show
  1. awslabs/__init__.py +2 -0
  2. awslabs/terraform_mcp_server/__init__.py +3 -0
  3. awslabs/terraform_mcp_server/impl/resources/__init__.py +11 -0
  4. awslabs/terraform_mcp_server/impl/resources/terraform_aws_provider_resources_listing.py +52 -0
  5. awslabs/terraform_mcp_server/impl/resources/terraform_awscc_provider_resources_listing.py +55 -0
  6. awslabs/terraform_mcp_server/impl/tools/__init__.py +15 -0
  7. awslabs/terraform_mcp_server/impl/tools/execute_terraform_command.py +206 -0
  8. awslabs/terraform_mcp_server/impl/tools/run_checkov_scan.py +359 -0
  9. awslabs/terraform_mcp_server/impl/tools/search_aws_provider_docs.py +677 -0
  10. awslabs/terraform_mcp_server/impl/tools/search_awscc_provider_docs.py +627 -0
  11. awslabs/terraform_mcp_server/impl/tools/search_specific_aws_ia_modules.py +444 -0
  12. awslabs/terraform_mcp_server/impl/tools/utils.py +558 -0
  13. awslabs/terraform_mcp_server/models/__init__.py +27 -0
  14. awslabs/terraform_mcp_server/models/models.py +260 -0
  15. awslabs/terraform_mcp_server/scripts/generate_aws_provider_resources.py +1224 -0
  16. awslabs/terraform_mcp_server/scripts/generate_awscc_provider_resources.py +1020 -0
  17. awslabs/terraform_mcp_server/scripts/scrape_aws_terraform_best_practices.py +129 -0
  18. awslabs/terraform_mcp_server/server.py +329 -0
  19. awslabs/terraform_mcp_server/static/AWSCC_PROVIDER_RESOURCES.md +3125 -0
  20. awslabs/terraform_mcp_server/static/AWS_PROVIDER_RESOURCES.md +3833 -0
  21. awslabs/terraform_mcp_server/static/AWS_TERRAFORM_BEST_PRACTICES.md +2523 -0
  22. awslabs/terraform_mcp_server/static/MCP_INSTRUCTIONS.md +126 -0
  23. awslabs/terraform_mcp_server/static/TERRAFORM_WORKFLOW_GUIDE.md +198 -0
  24. awslabs/terraform_mcp_server/static/__init__.py +22 -0
  25. awslabs/terraform_mcp_server/tests/__init__.py +1 -0
  26. awslabs/terraform_mcp_server/tests/run_tests.sh +35 -0
  27. awslabs/terraform_mcp_server/tests/test_parameter_annotations.py +207 -0
  28. awslabs/terraform_mcp_server/tests/test_tool_implementations.py +309 -0
  29. awslabs_terraform_mcp_server-0.0.1.dist-info/METADATA +97 -0
  30. awslabs_terraform_mcp_server-0.0.1.dist-info/RECORD +32 -0
  31. awslabs_terraform_mcp_server-0.0.1.dist-info/WHEEL +4 -0
  32. awslabs_terraform_mcp_server-0.0.1.dist-info/entry_points.txt +2 -0
@@ -0,0 +1,677 @@
1
+ """Implementation of AWS provider documentation search tool."""
2
+
3
+ import re
4
+ import requests
5
+ import sys
6
+ import time
7
+ from awslabs.terraform_mcp_server.models import TerraformAWSProviderDocsResult
8
+ from loguru import logger
9
+ from pathlib import Path
10
+ from typing import Any, Dict, List, Literal, Optional, Tuple, cast
11
+
12
+
13
+ # Configure logger for enhanced diagnostics with stacktraces
14
+ logger.configure(
15
+ handlers=[
16
+ {
17
+ 'sink': sys.stderr,
18
+ 'backtrace': True,
19
+ 'diagnose': True,
20
+ 'format': '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>',
21
+ }
22
+ ]
23
+ )
24
+
25
+ # Path to the static markdown file
26
+ STATIC_RESOURCES_PATH = (
27
+ Path(__file__).parent.parent.parent / 'static' / 'AWS_PROVIDER_RESOURCES.md'
28
+ )
29
+
30
+ # Base URLs for AWS provider documentation
31
+ AWS_DOCS_BASE_URL = 'https://registry.terraform.io/providers/hashicorp/aws/latest/docs'
32
+ GITHUB_RAW_BASE_URL = (
33
+ 'https://raw.githubusercontent.com/hashicorp/terraform-provider-aws/main/website/docs'
34
+ )
35
+
36
+ # Simple in-memory cache
37
+ _GITHUB_DOC_CACHE = {}
38
+
39
+
40
+ def resource_to_github_path(
41
+ asset_name: str, asset_type: str = 'resource', correlation_id: str = ''
42
+ ) -> Tuple[str, str]:
43
+ """Convert AWS resource type to GitHub documentation file path.
44
+
45
+ Args:
46
+ asset_name: The name of the asset to search (e.g., 'aws_s3_bucket')
47
+ asset_type: Type of asset to search for - 'resource' or 'data_source'
48
+ correlation_id: Identifier for tracking this request in logs
49
+
50
+ Returns:
51
+ A tuple of (path, url) for the GitHub documentation file
52
+ """
53
+ # Validate input parameters
54
+ if not isinstance(asset_name, str) or not asset_name:
55
+ logger.error(f'[{correlation_id}] Invalid asset_name: {asset_name}')
56
+ raise ValueError('asset_name must be a non-empty string')
57
+
58
+ # Sanitize asset_name to prevent path traversal and URL manipulation
59
+ # Only allow alphanumeric characters, underscores, and hyphens
60
+ sanitized_name = asset_name
61
+ if not re.match(r'^[a-zA-Z0-9_-]+$', sanitized_name.replace('aws_', '')):
62
+ logger.error(f'[{correlation_id}] Invalid characters in asset_name: {asset_name}')
63
+ raise ValueError('asset_name contains invalid characters')
64
+
65
+ # Validate asset_type
66
+ valid_asset_types = ['resource', 'data_source', 'both']
67
+ if asset_type not in valid_asset_types:
68
+ logger.error(f'[{correlation_id}] Invalid asset_type: {asset_type}')
69
+ raise ValueError(f'asset_type must be one of {valid_asset_types}')
70
+
71
+ # Remove the 'aws_' prefix if present
72
+ if sanitized_name.startswith('aws_'):
73
+ resource_name = sanitized_name[4:]
74
+ logger.trace(f"[{correlation_id}] Removed 'aws_' prefix: {resource_name}")
75
+ else:
76
+ resource_name = sanitized_name
77
+ logger.trace(f"[{correlation_id}] No 'aws_' prefix to remove: {resource_name}")
78
+
79
+ # Determine document type based on asset_type parameter
80
+ if asset_type == 'data_source':
81
+ doc_type = 'd' # data sources
82
+ elif asset_type == 'resource':
83
+ doc_type = 'r' # resources
84
+ else:
85
+ # For "both" or any other value, determine based on name pattern
86
+ # Data sources typically have 'data' in the name or follow other patterns
87
+ is_data_source = 'data' in sanitized_name.lower()
88
+ doc_type = 'd' if is_data_source else 'r'
89
+
90
+ # Create the file path for the markdown documentation
91
+ file_path = f'{doc_type}/{resource_name}.html.markdown'
92
+ logger.trace(f'[{correlation_id}] Constructed GitHub file path: {file_path}')
93
+
94
+ # Create the full URL to the raw GitHub content
95
+ github_url = f'{GITHUB_RAW_BASE_URL}/{file_path}'
96
+ logger.trace(f'[{correlation_id}] GitHub raw URL: {github_url}')
97
+
98
+ return file_path, github_url
99
+
100
+
101
+ def fetch_github_documentation(
102
+ asset_name: str, asset_type: str, cache_enabled: bool, correlation_id: str = ''
103
+ ) -> Optional[Dict[str, Any]]:
104
+ """Fetch documentation from GitHub for a specific resource type.
105
+
106
+ Args:
107
+ asset_name: The asset name (e.g., 'aws_s3_bucket')
108
+ asset_type: Either 'resource' or 'data_source'
109
+ cache_enabled: Whether local cache is enabled or not
110
+ correlation_id: Identifier for tracking this request in logs
111
+
112
+ Returns:
113
+ Dictionary with markdown content and metadata, or None if not found
114
+ """
115
+ start_time = time.time()
116
+ logger.info(f"[{correlation_id}] Fetching documentation from GitHub for '{asset_name}'")
117
+
118
+ # Create a cache key that includes both asset_name and asset_type
119
+ # Use a hash function to ensure the cache key is safe
120
+ cache_key = f'{asset_name}_{asset_type}'
121
+
122
+ # Check cache first
123
+ if cache_enabled:
124
+ if cache_key in _GITHUB_DOC_CACHE:
125
+ logger.info(
126
+ f"[{correlation_id}] Using cached documentation for '{asset_name}' (asset_type: {asset_type})"
127
+ )
128
+ return _GITHUB_DOC_CACHE[cache_key]
129
+
130
+ try:
131
+ # Convert resource type to GitHub path and URL
132
+ # This will validate and sanitize the input
133
+ try:
134
+ _, github_url = resource_to_github_path(asset_name, asset_type, correlation_id)
135
+ except ValueError as e:
136
+ logger.error(f'[{correlation_id}] Invalid input parameters: {str(e)}')
137
+ return None
138
+
139
+ # Validate the constructed URL to ensure it points to the expected domain
140
+ if not github_url.startswith(GITHUB_RAW_BASE_URL):
141
+ logger.error(f'[{correlation_id}] Invalid GitHub URL constructed: {github_url}')
142
+ return None
143
+
144
+ # Fetch the markdown content from GitHub
145
+ logger.info(f'[{correlation_id}] Fetching from GitHub URL: {github_url}')
146
+ response = requests.get(github_url, timeout=10)
147
+
148
+ if response.status_code != 200:
149
+ logger.warning(
150
+ f'[{correlation_id}] GitHub request failed: HTTP {response.status_code}'
151
+ )
152
+ return None
153
+
154
+ markdown_content = response.text
155
+ content_length = len(markdown_content)
156
+ logger.debug(f'[{correlation_id}] Received markdown content: {content_length} bytes')
157
+
158
+ if content_length > 0:
159
+ preview_length = min(200, content_length)
160
+ logger.trace(
161
+ f'[{correlation_id}] Markdown preview: {markdown_content[:preview_length]}...'
162
+ )
163
+
164
+ # Parse the markdown content
165
+ result = parse_markdown_documentation(
166
+ markdown_content, asset_name, github_url, correlation_id
167
+ )
168
+
169
+ # Cache the result with the composite key
170
+ if cache_enabled:
171
+ _GITHUB_DOC_CACHE[cache_key] = result
172
+
173
+ fetch_time = time.time() - start_time
174
+ logger.info(f'[{correlation_id}] GitHub documentation fetched in {fetch_time:.2f} seconds')
175
+ return result
176
+
177
+ except requests.exceptions.Timeout as e:
178
+ logger.warning(f'[{correlation_id}] Timeout error fetching from GitHub: {str(e)}')
179
+ return None
180
+ except requests.exceptions.RequestException as e:
181
+ logger.warning(f'[{correlation_id}] Request error fetching from GitHub: {str(e)}')
182
+ return None
183
+ except Exception as e:
184
+ logger.error(
185
+ f'[{correlation_id}] Unexpected error fetching from GitHub: {type(e).__name__}: {str(e)}'
186
+ )
187
+ # Don't log the full stack trace to avoid information disclosure
188
+ return None
189
+
190
+
191
+ def parse_markdown_documentation(
192
+ content: str, asset_name: str, url: str, correlation_id: str = ''
193
+ ) -> Dict[str, Any]:
194
+ """Parse markdown documentation content for a resource.
195
+
196
+ Args:
197
+ content: The markdown content
198
+ asset_name: The asset name
199
+ url: The source URL for this documentation
200
+ correlation_id: Identifier for tracking this request in logs
201
+
202
+ Returns:
203
+ Dictionary with parsed documentation details
204
+ """
205
+ start_time = time.time()
206
+ logger.debug(f"[{correlation_id}] Parsing markdown documentation for '{asset_name}'")
207
+
208
+ try:
209
+ # Find the title (typically the first heading)
210
+ title_match = re.search(r'^#\s+(.*?)$', content, re.MULTILINE)
211
+ if title_match:
212
+ title = title_match.group(1).strip()
213
+ logger.debug(f"[{correlation_id}] Found title: '{title}'")
214
+ else:
215
+ title = f'AWS {asset_name}'
216
+ logger.debug(f"[{correlation_id}] No title found, using default: '{title}'")
217
+
218
+ # Find the main description section (all content after resource title before next heading)
219
+ description = ''
220
+ resource_heading_pattern = re.compile(
221
+ rf'# Resource: {re.escape(asset_name)}\s*(.*?)(?=\n##|\Z)', re.DOTALL
222
+ )
223
+ resource_match = resource_heading_pattern.search(content)
224
+
225
+ if resource_match:
226
+ # Extract the description text and clean it up
227
+ description = resource_match.group(1).strip()
228
+ logger.debug(
229
+ f"[{correlation_id}] Found resource description section: '{description[:100]}...'"
230
+ )
231
+ else:
232
+ # Fall back to the description found on the starting markdown table of each github markdown page
233
+ desc_match = re.search(r'description:\s*\|-\n(.*?)\n---', content, re.MULTILINE)
234
+ if desc_match:
235
+ description = desc_match.group(1).strip()
236
+ logger.debug(
237
+ f"[{correlation_id}] Using fallback description: '{description[:100]}...'"
238
+ )
239
+ else:
240
+ description = f'Documentation for AWS {asset_name}'
241
+ logger.debug(f'[{correlation_id}] No description found, using default')
242
+
243
+ # Find all example snippets
244
+ example_snippets = []
245
+
246
+ # First try to extract from the Example Usage section
247
+ example_section_match = re.search(r'## Example Usage\n([\s\S]*?)(?=\n## |\Z)', content)
248
+
249
+ if example_section_match:
250
+ # logger.debug(f"example_section_match: {example_section_match.group()}")
251
+ example_section = example_section_match.group(1).strip()
252
+ logger.debug(
253
+ f'[{correlation_id}] Found Example Usage section ({len(example_section)} chars)'
254
+ )
255
+
256
+ # Find all subheadings in the Example Usage section with a more robust pattern
257
+ subheading_list = list(
258
+ re.finditer(r'### (.*?)[\r\n]+(.*?)(?=###|\Z)', example_section, re.DOTALL)
259
+ )
260
+ logger.debug(
261
+ f'[{correlation_id}] Found {len(subheading_list)} subheadings in Example Usage section'
262
+ )
263
+ subheading_found = False
264
+
265
+ # Check if there are any subheadings
266
+ for match in subheading_list:
267
+ # logger.info(f"subheading match: {match.group()}")
268
+ subheading_found = True
269
+ title = match.group(1).strip()
270
+ subcontent = match.group(2).strip()
271
+
272
+ logger.debug(
273
+ f"[{correlation_id}] Found subheading '{title}' with {len(subcontent)} chars content"
274
+ )
275
+
276
+ # Find code blocks in this subsection - pattern to match terraform code blocks
277
+ code_match = re.search(r'```(?:terraform|hcl)?\s*(.*?)```', subcontent, re.DOTALL)
278
+ if code_match:
279
+ code_snippet = code_match.group(1).strip()
280
+ example_snippets.append({'title': title, 'code': code_snippet})
281
+ logger.debug(
282
+ f"[{correlation_id}] Added example snippet for '{title}' ({len(code_snippet)} chars)"
283
+ )
284
+
285
+ # If no subheadings were found, look for direct code blocks under Example Usage
286
+ if not subheading_found:
287
+ logger.debug(
288
+ f'[{correlation_id}] No subheadings found, looking for direct code blocks'
289
+ )
290
+ # Improved pattern for code blocks
291
+ code_blocks = re.finditer(
292
+ r'```(?:terraform|hcl)?\s*(.*?)```', example_section, re.DOTALL
293
+ )
294
+ code_found = False
295
+
296
+ for code_match in code_blocks:
297
+ code_found = True
298
+ code_snippet = code_match.group(1).strip()
299
+ example_snippets.append({'title': 'Example Usage', 'code': code_snippet})
300
+ logger.debug(
301
+ f'[{correlation_id}] Added direct example snippet ({len(code_snippet)} chars)'
302
+ )
303
+
304
+ if not code_found:
305
+ logger.debug(
306
+ f'[{correlation_id}] No code blocks found in Example Usage section'
307
+ )
308
+ else:
309
+ logger.debug(f'[{correlation_id}] No Example Usage section found')
310
+
311
+ if example_snippets:
312
+ logger.info(f'[{correlation_id}] Found {len(example_snippets)} example snippets')
313
+ else:
314
+ logger.debug(f'[{correlation_id}] No example snippets found')
315
+
316
+ # Extract Arguments Reference section
317
+ arguments = []
318
+ arg_ref_section_match = re.search(
319
+ r'## Argument Reference\n([\s\S]*?)(?=\n## |\Z)', content
320
+ )
321
+ if arg_ref_section_match:
322
+ arg_section = arg_ref_section_match.group(1).strip()
323
+ logger.debug(
324
+ f'[{correlation_id}] Found Argument Reference section ({len(arg_section)} chars)'
325
+ )
326
+
327
+ # Look for arguments directly under the main Argument Reference section
328
+ args_under_main_section_match = re.search(
329
+ r'(.*?)(?=\n###|\n##|$)', arg_section, re.DOTALL
330
+ )
331
+ if args_under_main_section_match:
332
+ args_under_main_section = args_under_main_section_match.group(1).strip()
333
+ logger.debug(
334
+ f'[{correlation_id}] Found arguments directly under the Argument Reference section ({len(args_under_main_section)} chars)'
335
+ )
336
+
337
+ # Find arguments in this subsection
338
+ arg_matches = re.finditer(
339
+ r'\*\s+`([^`]+)`\s+-\s+(.*?)(?=\n\*\s+`|$)',
340
+ args_under_main_section,
341
+ re.DOTALL,
342
+ )
343
+ arg_list = list(arg_matches)
344
+ logger.debug(
345
+ f'[{correlation_id}] Found {len(arg_list)} arguments directly under the Argument Reference section'
346
+ )
347
+
348
+ for match in arg_list:
349
+ arg_name = match.group(1).strip()
350
+ arg_desc = match.group(2).strip() if match.group(2) else None
351
+ # Do not add arguments that do not have a description
352
+ if arg_name is not None and arg_desc is not None:
353
+ arguments.append(
354
+ {'name': arg_name, 'description': arg_desc, 'argument_section': 'main'}
355
+ )
356
+ else:
357
+ logger.debug(
358
+ f"[{correlation_id}] Added argument '{arg_name}': '{arg_desc[:50] if arg_desc else 'No description found'}...' (truncated)"
359
+ )
360
+
361
+ # Now, Find all subheadings in the Argument Reference section with a more robust pattern
362
+ subheading_list = list(
363
+ re.finditer(r'### (.*?)[\r\n]+(.*?)(?=###|\Z)', arg_section, re.DOTALL)
364
+ )
365
+ logger.debug(
366
+ f'[{correlation_id}] Found {len(subheading_list)} subheadings in Argument Reference section'
367
+ )
368
+ subheading_found = False
369
+
370
+ # Check if there are any subheadings
371
+ for match in subheading_list:
372
+ subheading_found = True
373
+ title = match.group(1).strip()
374
+ subcontent = match.group(2).strip()
375
+ logger.debug(
376
+ f"[{correlation_id}] Found subheading '{title}' with {len(subcontent)} chars content"
377
+ )
378
+
379
+ # Find arguments in this subsection
380
+ arg_matches = re.finditer(
381
+ r'\*\s+`([^`]+)`\s+-\s+(.*?)(?=\n\*\s+`|$)',
382
+ subcontent,
383
+ re.DOTALL,
384
+ )
385
+ arg_list = list(arg_matches)
386
+ logger.debug(
387
+ f'[{correlation_id}] Found {len(arg_list)} arguments in subheading {title}'
388
+ )
389
+
390
+ for match in arg_list:
391
+ arg_name = match.group(1).strip()
392
+ arg_desc = match.group(2).strip() if match.group(2) else None
393
+ # Do not add arguments that do not have a description
394
+ if arg_name is not None and arg_desc is not None:
395
+ arguments.append(
396
+ {'name': arg_name, 'description': arg_desc, 'argument_section': title}
397
+ )
398
+ else:
399
+ logger.debug(
400
+ f"[{correlation_id}] Added argument '{arg_name}': '{arg_desc[:50] if arg_desc else 'No description found'}...' (truncated)"
401
+ )
402
+
403
+ arguments = arguments if arguments else None
404
+ if arguments:
405
+ logger.info(
406
+ f'[{correlation_id}] Found {len(arguments)} arguments across all sections'
407
+ )
408
+
409
+ else:
410
+ logger.debug(f'[{correlation_id}] No Argument Reference section found')
411
+
412
+ # Extract Attributes Reference section
413
+ attributes = []
414
+ attr_ref_match = re.search(r'## Attribute Reference\n([\s\S]*?)(?=\n## |\Z)', content)
415
+ if attr_ref_match:
416
+ attr_section = attr_ref_match.group(1).strip()
417
+ logger.debug(
418
+ f'[{correlation_id}] Found Attribute Reference section ({len(attr_section)} chars)'
419
+ )
420
+
421
+ # Parse attributes - similar format to arguments
422
+ attr_matches = re.finditer(
423
+ r'[*-]\s+[`"]?([^`":\n]+)[`"]?(?:[`":\s-]+)?(.*?)(?=\n[*-]|\n\n|\Z)',
424
+ attr_section,
425
+ re.DOTALL,
426
+ )
427
+ attr_list = list(attr_matches)
428
+ logger.debug(
429
+ f'[{correlation_id}] Found {len(attr_list)} attributes in Attribute Reference section'
430
+ )
431
+
432
+ for match in attr_list:
433
+ attr_name = match.group(1).strip()
434
+ attr_desc = (
435
+ match.group(2).strip() if match.group(2) else 'No description available'
436
+ )
437
+ attributes.append({'name': attr_name, 'description': attr_desc})
438
+ logger.debug(
439
+ f"[{correlation_id}] Added attribute '{attr_name}': '{attr_desc[:50]}...' (truncated)"
440
+ )
441
+
442
+ attributes = attributes if attributes else None
443
+ if attributes:
444
+ logger.info(f'[{correlation_id}] Found {len(attributes)} attributes')
445
+ else:
446
+ logger.debug(f'[{correlation_id}] No Attribute Reference section found')
447
+
448
+ # Return the parsed information
449
+ parse_time = time.time() - start_time
450
+ logger.debug(f'[{correlation_id}] Markdown parsing completed in {parse_time:.2f} seconds')
451
+
452
+ return {
453
+ 'title': title,
454
+ 'description': description,
455
+ 'example_snippets': example_snippets,
456
+ 'url': url,
457
+ 'arguments': arguments,
458
+ 'attributes': attributes,
459
+ }
460
+
461
+ except Exception as e:
462
+ logger.exception(f'[{correlation_id}] Error parsing markdown content')
463
+ logger.error(f'[{correlation_id}] Error type: {type(e).__name__}, message: {str(e)}')
464
+
465
+ # Return partial info if available
466
+ return {
467
+ 'title': f'AWS {asset_name}',
468
+ 'description': f'Documentation for AWS {asset_name} (Error parsing details: {str(e)})',
469
+ 'url': url,
470
+ 'example_snippets': None,
471
+ 'arguments': None,
472
+ 'attributes': None,
473
+ }
474
+
475
+
476
+ async def search_aws_provider_docs_impl(
477
+ asset_name: str, asset_type: str = 'resource', cache_enabled: bool = False
478
+ ) -> List[TerraformAWSProviderDocsResult]:
479
+ """Search AWS provider documentation for resources and data sources.
480
+
481
+ This tool searches the Terraform AWS provider documentation for information about
482
+ specific assets, which can either be resources or data sources. It retrieves comprehensive details including
483
+ descriptions, example code snippets, argument references, and attribute references.
484
+
485
+ The implementation fetches documentation directly from the official Terraform AWS provider
486
+ GitHub repository to ensure the most up-to-date information. Results are cached for
487
+ improved performance on subsequent queries.
488
+
489
+ Use the 'asset_type' parameter to specify if you are looking for information about provider
490
+ resources, data sources, or both. The tool will automatically handle prefixes - you can
491
+ search for either 'aws_s3_bucket' or 's3_bucket'.
492
+
493
+ Examples:
494
+ - To get documentation for an S3 bucket resource:
495
+ search_aws_provider_docs_impl(asset_name='aws_s3_bucket')
496
+
497
+ - To search only for data sources:
498
+ search_aws_provider_docs_impl(asset_name='aws_ami', asset_type='data_source')
499
+
500
+ - To search only for resources:
501
+ search_aws_provider_docs_impl(asset_name='aws_instance', asset_type='resource')
502
+
503
+ Parameters:
504
+ asset_name: Name of the AWS Provider resource or data source to look for (e.g., 'aws_s3_bucket', 'aws_lambda_function')
505
+ asset_type: Type of documentation to search - 'resource' (default), 'data_source', or 'both'. Some resources and data sources share the same name.
506
+ cache_enabled: Whether the local cache of results is enabled or not
507
+
508
+ Returns:
509
+ A list of matching documentation entries with details including:
510
+ - Asset name, type, and description
511
+ - URL to the official documentation
512
+ - Example code snippets
513
+ - Arguments with descriptions
514
+ - Attributes with descriptions
515
+ """
516
+ start_time = time.time()
517
+ correlation_id = f'search-{int(start_time * 1000)}'
518
+ logger.info(f"[{correlation_id}] Starting AWS provider docs search for '{asset_name}'")
519
+
520
+ # Validate input parameters
521
+ if not isinstance(asset_name, str) or not asset_name:
522
+ logger.error(f'[{correlation_id}] Invalid asset_name parameter: {asset_name}')
523
+ return [
524
+ TerraformAWSProviderDocsResult(
525
+ asset_name='Error',
526
+ asset_type=cast(Literal['both', 'resource', 'data_source'], asset_type),
527
+ description='Invalid asset_name parameter. Must be a non-empty string.',
528
+ url=None,
529
+ example_usage=None,
530
+ arguments=None,
531
+ attributes=None,
532
+ )
533
+ ]
534
+
535
+ # Validate asset_type
536
+ valid_asset_types = ['resource', 'data_source', 'both']
537
+ if asset_type not in valid_asset_types:
538
+ logger.error(f'[{correlation_id}] Invalid asset_type parameter: {asset_type}')
539
+ return [
540
+ TerraformAWSProviderDocsResult(
541
+ asset_name='Error',
542
+ asset_type=cast(Literal['both', 'resource', 'data_source'], 'resource'),
543
+ description=f'Invalid asset_type parameter. Must be one of {valid_asset_types}.',
544
+ url=None,
545
+ example_usage=None,
546
+ arguments=None,
547
+ attributes=None,
548
+ )
549
+ ]
550
+
551
+ search_term = asset_name.lower()
552
+
553
+ try:
554
+ # Try fetching from GitHub
555
+ logger.info(f'[{correlation_id}] Fetching from GitHub')
556
+
557
+ results = []
558
+
559
+ # If asset_type is "both", try both resource and data source paths
560
+ if asset_type == 'both':
561
+ logger.info(f'[{correlation_id}] Searching for both resources and data sources')
562
+
563
+ # First try as a resource
564
+ github_result = fetch_github_documentation(
565
+ search_term, 'resource', cache_enabled, correlation_id
566
+ )
567
+ if github_result:
568
+ logger.info(f'[{correlation_id}] Found documentation as a resource')
569
+ # Create result object
570
+ description = github_result['description']
571
+
572
+ result = TerraformAWSProviderDocsResult(
573
+ asset_name=asset_name,
574
+ asset_type='resource',
575
+ description=description,
576
+ url=github_result['url'],
577
+ example_usage=github_result.get('example_snippets'),
578
+ arguments=github_result.get('arguments'),
579
+ attributes=github_result.get('attributes'),
580
+ )
581
+ results.append(result)
582
+
583
+ # Then try as a data source
584
+ data_result = fetch_github_documentation(
585
+ search_term, 'data_source', cache_enabled, correlation_id
586
+ )
587
+ if data_result:
588
+ logger.info(f'[{correlation_id}] Found documentation as a data source')
589
+ # Create result object
590
+ description = data_result['description']
591
+
592
+ result = TerraformAWSProviderDocsResult(
593
+ asset_name=asset_name,
594
+ asset_type='data_source',
595
+ description=description,
596
+ url=data_result['url'],
597
+ example_usage=data_result.get('example_snippets'),
598
+ arguments=data_result.get('arguments'),
599
+ attributes=data_result.get('attributes'),
600
+ )
601
+ results.append(result)
602
+
603
+ if results:
604
+ logger.info(f'[{correlation_id}] Found {len(results)} documentation entries')
605
+ end_time = time.time()
606
+ logger.info(
607
+ f'[{correlation_id}] Search completed in {end_time - start_time:.2f} seconds (GitHub source)'
608
+ )
609
+ return results
610
+ else:
611
+ # Search for either resource or data source based on asset_type parameter
612
+ github_result = fetch_github_documentation(
613
+ search_term, asset_type, cache_enabled, correlation_id
614
+ )
615
+ if github_result:
616
+ logger.info(f'[{correlation_id}] Successfully found GitHub documentation')
617
+
618
+ # Create result object
619
+ description = github_result['description']
620
+ result = TerraformAWSProviderDocsResult(
621
+ asset_name=asset_name,
622
+ asset_type=cast(Literal['both', 'resource', 'data_source'], asset_type),
623
+ description=description,
624
+ url=github_result['url'],
625
+ example_usage=github_result.get('example_snippets'),
626
+ arguments=github_result.get('arguments'),
627
+ attributes=github_result.get('attributes'),
628
+ )
629
+
630
+ end_time = time.time()
631
+ logger.info(
632
+ f'[{correlation_id}] Search completed in {end_time - start_time:.2f} seconds (GitHub source)'
633
+ )
634
+ return [result]
635
+
636
+ # If GitHub approach fails, return a "not found" result
637
+ logger.warning(f"[{correlation_id}] Documentation not found on GitHub for '{search_term}'")
638
+
639
+ # Return a "not found" result
640
+ logger.warning(f'[{correlation_id}] No documentation found for asset {asset_name}')
641
+ end_time = time.time()
642
+ logger.info(
643
+ f'[{correlation_id}] Search completed in {end_time - start_time:.2f} seconds (no results)'
644
+ )
645
+ return [
646
+ TerraformAWSProviderDocsResult(
647
+ asset_name='Not found',
648
+ asset_type=cast(Literal['both', 'resource', 'data_source'], asset_type),
649
+ description=f"No documentation found for resource type '{asset_name}'.",
650
+ url=None,
651
+ example_usage=None,
652
+ arguments=None,
653
+ attributes=None,
654
+ )
655
+ ]
656
+
657
+ except Exception as e:
658
+ logger.error(
659
+ f'[{correlation_id}] Error searching AWS provider docs: {type(e).__name__}: {str(e)}'
660
+ )
661
+ # Don't log the full stack trace to avoid information disclosure
662
+
663
+ end_time = time.time()
664
+ logger.info(f'[{correlation_id}] Search failed in {end_time - start_time:.2f} seconds')
665
+
666
+ # Return a generic error message without exposing internal details
667
+ return [
668
+ TerraformAWSProviderDocsResult(
669
+ asset_name='Error',
670
+ asset_type=cast(Literal['both', 'resource', 'data_source'], asset_type),
671
+ description='Failed to search AWS provider documentation. Please check your input and try again.',
672
+ url=f'{AWS_DOCS_BASE_URL}/resources',
673
+ example_usage=None,
674
+ arguments=None,
675
+ attributes=None,
676
+ )
677
+ ]