Skip to main content
Version: v4.2.24

Optimized Discovery System

Overview

The Optimized Discovery System is an enhanced AWS resource discovery engine that addresses performance and coverage issues found in traditional discovery approaches. It provides 3-4x performance improvements while maintaining high accuracy through service-specific patterns and intelligent field mapping.

Key Improvements

🚀 Performance Enhancements

  • Parallel Processing: Concurrent service discovery with configurable worker pools
  • Optimized API Calls: Service-specific operation selection to reduce unnecessary calls
  • Timeout Management: Prevents hanging operations with configurable timeouts
  • Smart Deduplication: Efficient resource merging with confidence-based selection

🎯 Enhanced Service Coverage

  • 22+ AWS Services: Comprehensive patterns for CloudFront, IAM, Route53, S3, Lambda, EC2, RDS, ECS, EKS, ElastiCache, SNS, SQS, DynamoDB, API Gateway, CloudFormation, CodePipeline, CodeBuild, Secrets Manager, SSM, KMS, ACM, and WAF
  • Service-Specific Patterns: Tailored extraction logic for each AWS service
  • Priority Services: Focus on previously problematic services (CloudFront, IAM, Route53, S3, Lambda)
  • Global Service Handling: Proper region management for global services
  • Fallback Mechanisms: Graceful degradation for unknown services

🧠 Intelligent Field Mapping

  • Service-Specific Extractors: Custom patterns for resource identification
  • Enhanced Confidence Scoring: Weighted quality assessment based on data completeness
  • Better Name Detection: Multiple extraction strategies for resource names
  • Improved Tag Extraction: Handles various AWS tag formats and nested structures

Architecture

Core Components

graph TB
A[OptimizedAWSDiscovery] --> B[OptimizedFieldMapper]
A --> C[Parallel Service Discovery]
A --> D[Resource Deduplication]

B --> E[Service-Specific Patterns]
B --> F[Confidence Scoring]
B --> G[Field Extraction]

C --> H[Priority Services]
C --> I[Global Service Detection]
C --> J[Operation Selection]

D --> K[ARN-based Grouping]
D --> L[ID-based Grouping]
D --> M[Quality Merging]

OptimizedFieldMapper

The OptimizedFieldMapper extends the base IntelligentFieldMapper with enhanced service-specific patterns and AWS managed resource filtering:

class OptimizedFieldMapper(IntelligentFieldMapper):
"""Optimized field mapper with enhanced service-specific patterns and AWS managed resource filtering."""

def __init__(self):
super().__init__()

# Enhanced service-specific patterns based on debugging findings
self.optimized_service_patterns = {
"cloudfront": {
"resource_types": ["Distribution"],
"id_patterns": [r"E[A-Z0-9]+"],
"operations": ["ListDistributions"],
"name_fields": ["DomainName", "Id"],
"region_dependent": False,
"global_service": True,
"exclude_aws_managed": True,
},
"iam": {
"resource_types": ["Role", "User", "Policy", "Group"],
"operations": ["ListRoles", "ListUsers", "ListPolicies", "ListGroups"],
"name_fields": ["RoleName", "UserName", "PolicyName", "GroupName"],
"region_dependent": False,
"global_service": True,
"exclude_aws_managed": True,
"aws_managed_patterns": [
r"^aws-service-role/",
r"^AWSServiceRole",
r"^service-role/",
r"^OrganizationAccountAccessRole$",
r"^AWSReservedSSO_",
r"^StackSet-",
r"^CloudFormation-",
],
},
# ... more service patterns with AWS managed filtering
}

# AWS managed resource patterns (global)
self.global_aws_managed_patterns = [
r"^aws-", r"^AWS", r"^amazon-", r"^Amazon",
r"^default", r"^Default",
]

# Enhanced confidence scoring weights
self.confidence_weights = {
"has_resource_id": 2.5,
"has_resource_name": 2.0,
"has_resource_arn": 1.5,
"has_correct_type": 1.5,
"has_tags": 1.0,
"has_status": 0.5,
"has_creation_date": 0.5,
"has_vpc_info": 0.5,
"has_security_groups": 0.5,
"has_account_id": 0.5,
}

OptimizedAWSDiscovery

The main discovery engine with parallel processing capabilities:

class OptimizedAWSDiscovery(IntelligentAWSDiscovery):
"""Optimized discovery system with enhanced service coverage, region handling, and AWS managed resource filtering."""

def __init__(self, session: boto3.Session = None, regions: List[str] = None):
super().__init__(session, regions)
self.field_mapper = OptimizedFieldMapper()

# Priority services that had issues in the original system
self.priority_services = [
"cloudfront", # Was missing distributions
"iam", # Was missing roles/users
"route53", # Was missing hosted zones
"s3", # Had detection issues
"lambda", # Had detection issues
"ec2", # Core service
"rds", # Database service
"cloudwatch", # Monitoring
]

# Performance settings
self.max_workers = 4
self.enable_parallel = True
self.operation_timeout = 20

# Region handling
self.specified_regions = regions
self.fallback_to_all_regions = True

# S3 region cache for bucket location detection
self.s3_region_cache = {}

Service-Specific Patterns

CloudFront

  • Resource Types: Distribution
  • Name Fields: DomainName, Id
  • ID Patterns: E[A-Z0-9]+ format
  • Special Logic: Handles aliases and domain name extraction
  • Global Service: Yes (us-east-1 only)
  • AWS Managed Filtering: Enabled

IAM

  • Resource Types: Role, User, Policy, Group
  • Name Fields: RoleName, UserName, PolicyName, GroupName
  • Special Logic: Account-level resource handling with service-linked role filtering
  • Global Service: Yes (us-east-1 only)
  • AWS Managed Filtering: Enabled with patterns for service roles, AWS policies
  • Filtered Patterns: aws-service-role/, AWSServiceRole*, service-role/, etc.

Route53

  • Resource Types: HostedZone
  • Name Fields: Name, Id
  • ID Patterns: Z[0-9A-Z]+ format
  • Global Service: Yes (us-east-1 only)
  • AWS Managed Filtering: Enabled
  • Excluded Types: GeoLocation records, reverse DNS zones

S3

  • Resource Types: Bucket
  • Name Fields: Name
  • Special Logic: Bucket-level metadata extraction with region detection
  • Global Service: Yes (us-east-1 only)
  • AWS Managed Filtering: Enabled
  • Region Detection: Enhanced with bucket location API calls

Lambda

  • Resource Types: Function
  • Name Fields: FunctionName
  • Special Logic: Runtime and environment detection
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

EC2

  • Resource Types: Instance, VPC, Subnet, SecurityGroup, InternetGateway
  • Name Fields: InstanceId, VpcId, SubnetId, GroupId
  • Special Logic: Network resource relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled for default VPCs and security groups

RDS

  • Resource Types: DBInstance, DBCluster
  • Name Fields: DBInstanceIdentifier, DBClusterIdentifier
  • Special Logic: Database cluster and instance relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

ECS

  • Resource Types: Cluster, Service, TaskDefinition, ContainerInstance
  • Name Fields: clusterName, serviceName, taskDefinitionArn, containerInstanceArn
  • Special Logic: Container orchestration resource relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled with patterns for default clusters and ECS-optimized resources

EKS

  • Resource Types: Cluster, NodeGroup, FargateProfile, Addon
  • Name Fields: name, clusterName, nodegroupName, fargateProfileName
  • Special Logic: Kubernetes cluster and node management
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled with patterns for EKS service roles

ElastiCache

  • Resource Types: CacheCluster, ReplicationGroup, CacheSubnetGroup, CacheParameterGroup
  • Name Fields: CacheClusterId, ReplicationGroupId, CacheSubnetGroupName, CacheParameterGroupName
  • Special Logic: Cache cluster and replication group relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled with patterns for default parameter groups

SNS

  • Resource Types: Topic, Subscription, PlatformApplication
  • Name Fields: TopicArn, SubscriptionArn, Name
  • Special Logic: Message topic and subscription relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

SQS

  • Resource Types: Queue
  • Name Fields: QueueUrl, QueueName
  • Special Logic: Queue URL parsing and name extraction
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

DynamoDB

  • Resource Types: Table, Backup, GlobalTable
  • Name Fields: TableName, BackupName, GlobalTableName
  • Special Logic: Table and backup relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

API Gateway

  • Resource Types: RestApi, DomainName, ApiKey, UsagePlan
  • Name Fields: id, restApiId, name, domainName
  • Special Logic: API resource relationships and domain mapping
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

CloudFormation

  • Resource Types: Stack, StackSet, ChangeSet
  • Name Fields: StackName, StackSetName, ChangeSetName
  • Special Logic: Stack and change set relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled with patterns for AWS service stacks

CodePipeline

  • Resource Types: Pipeline
  • Name Fields: name, pipelineName
  • Special Logic: CI/CD pipeline configuration
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

CodeBuild

  • Resource Types: Project, Build
  • Name Fields: name, projectName, buildId
  • Special Logic: Build project and execution relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

Secrets Manager

  • Resource Types: Secret
  • Name Fields: Name, ARN, SecretId
  • Special Logic: Secret metadata and rotation configuration
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled with patterns for RDS managed secrets

Systems Manager (SSM)

  • Resource Types: Parameter, Document, PatchBaseline
  • Name Fields: Name, ParameterName, DocumentName
  • Special Logic: Parameter hierarchy and document relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

KMS

  • Resource Types: Key, Alias
  • Name Fields: KeyId, AliasName
  • Special Logic: Key and alias relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled with patterns for AWS managed keys

ACM

  • Resource Types: Certificate
  • Name Fields: CertificateArn, DomainName
  • Special Logic: Certificate domain and validation information
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

WAF

  • Resource Types: WebACL, RuleGroup, IPSet
  • Name Fields: Name, Id, ARN
  • Special Logic: Web ACL and rule relationships
  • Global Service: No (region-specific)
  • AWS Managed Filtering: Enabled

AWS Managed Resource Filtering

The optimized discovery system includes intelligent filtering of AWS-managed resources to focus on user-created resources:

Global Filtering Patterns

# AWS managed resource patterns (global)
self.global_aws_managed_patterns = [
r"^aws-", r"^AWS", r"^amazon-", r"^Amazon",
r"^default", r"^Default",
]

Service-Specific Filtering

IAM Resource Filtering

  • Service-linked roles: Filters roles in /aws-service-role/ and /service-role/ paths
  • AWS managed policies: Excludes policies with ARNs starting with arn:aws:iam::aws:policy/
  • Organization roles: Filters OrganizationAccountAccessRole and similar
  • SSO roles: Excludes AWSReservedSSO_* roles
  • CloudFormation roles: Filters StackSet-* and CloudFormation-* roles

EC2 Resource Filtering

  • Default VPCs: Excludes VPCs marked as default
  • Default security groups: Filters security groups named "default"
  • AWS managed resources: Excludes resources with AWS-managed indicators

Route53 Resource Filtering

  • Reverse DNS zones: Excludes .in-addr.arpa. and .ip6.arpa. zones
  • GeoLocation records: Filters geolocation-based records
  • AWS managed zones: Excludes AWS-controlled hosted zones

Filtering Implementation

def _is_aws_managed_resource(
self, data: Dict[str, Any], service_name: str,
resource_id: str, resource_type: str
) -> bool:
"""Check if a resource is AWS managed and should be excluded."""

service_lower = service_name.lower()

# Skip filtering if not configured for this service
if service_lower not in self.optimized_service_patterns:
return False

patterns = self.optimized_service_patterns[service_lower]
if not patterns.get("exclude_aws_managed", False):
return False

# Check service-specific AWS managed patterns
service_patterns = patterns.get("aws_managed_patterns", [])
for pattern in service_patterns:
if re.match(pattern, resource_id, re.IGNORECASE):
return True

# Check global AWS managed patterns
for pattern in self.global_aws_managed_patterns:
if re.match(pattern, resource_id, re.IGNORECASE):
return True

# Service-specific detailed checks
if service_lower == "iam":
return self._is_aws_managed_iam_resource(data, resource_id, resource_type)
elif service_lower == "route53":
return self._is_aws_managed_route53_resource(data, resource_id, resource_type)
elif service_lower == "ec2":
return self._is_aws_managed_ec2_resource(data, resource_id, resource_type)

return False

Performance Optimizations

Parallel Processing

def _discover_services_parallel(self) -> List[StandardResource]:
"""Parallel service discovery for better performance."""

all_resources = []

with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor:
# Submit discovery tasks for priority services
future_to_service = {
executor.submit(self._discover_service_safe, service): service
for service in self.priority_services
}

# Collect results as they complete
for future in concurrent.futures.as_completed(future_to_service, timeout=120):
service = future_to_service[future]
try:
resources = future.result(timeout=self.operation_timeout)
all_resources.extend(resources)
except Exception as e:
self.logger.warning(f"Discovery failed for {service}: {e}")

return self._intelligent_deduplication(all_resources)

Operation Selection

The system intelligently selects API operations based on service patterns:

def _get_optimized_discovery_operations(self, service_name: str, client) -> List[str]:
"""Get optimized discovery operations for a service."""

# Get all available operations
all_operations = client._service_model.operation_names

# Service-specific operation priorities
service_lower = service_name.lower()
if service_lower in self.field_mapper.optimized_service_patterns:
preferred_ops = self.field_mapper.optimized_service_patterns[service_lower].get("operations", [])
# Prioritize preferred operations
operations = [op for op in preferred_ops if op in all_operations]
if operations:
return operations

# Fall back to generic discovery operations
discovery_ops = [
op for op in all_operations
if op.startswith(("List", "Describe", "Get"))
and not any(skip in op for skip in ["Policy", "Version", "Status", "Health", "Metrics"])
]

# Prioritize List operations over Describe operations
list_ops = [op for op in discovery_ops if op.startswith("List")]
describe_ops = [op for op in discovery_ops if op.startswith("Describe")]
get_ops = [op for op in discovery_ops if op.startswith("Get")]

return list_ops[:2] + describe_ops[:2] + get_ops[:1] # Limit to avoid too many calls

Confidence Scoring

The optimized system uses weighted confidence scoring based on data completeness:

def _optimized_calculate_confidence_score(self, resource: StandardResource) -> float:
"""Optimized confidence scoring with weighted factors."""

confidence_weights = {
'has_resource_id': 2.5,
'has_resource_name': 2.0,
'has_resource_arn': 1.5,
'has_correct_type': 1.5,
'has_tags': 1.0,
'has_status': 0.5,
'has_creation_date': 0.5,
'has_vpc_info': 0.5,
'has_security_groups': 0.5,
'has_account_id': 0.5,
}

score = 0.0
total_weight = sum(confidence_weights.values())

# Apply weighted scoring based on available data
if resource.resource_id and resource.resource_id != "unknown":
score += confidence_weights['has_resource_id']
if resource.resource_name:
score += confidence_weights['has_resource_name']
# ... additional scoring logic

return min(score / total_weight, 1.0)

Usage Examples

Basic Usage

from inventag.discovery.optimized_discovery import OptimizedAWSDiscovery

# Initialize optimized discovery
discovery = OptimizedAWSDiscovery(regions=['us-east-1', 'us-west-2'])

# Configure performance settings
discovery.max_workers = 6
discovery.enable_parallel = True
discovery.operation_timeout = 30

# Discover resources
resources = discovery.discover_all_services()

print(f"Found {len(resources)} resources")
for resource in resources[:5]:
print(f"- {resource.service_name}: {resource.resource_name} (confidence: {resource.confidence_score:.2f})")

Integration with Existing Inventory

from inventag.discovery import AWSResourceInventory
from inventag.discovery.optimized_discovery import OptimizedAWSDiscovery

# Replace intelligent discovery with optimized version
inventory = AWSResourceInventory(regions=['us-east-1'])

# Use optimized discovery directly
optimized_discovery = OptimizedAWSDiscovery(regions=['us-east-1'])
resources = optimized_discovery.discover_all_services()

# Convert to inventory format if needed
inventory_resources = []
for resource in resources:
inventory_resources.append({
'arn': resource.resource_arn,
'id': resource.resource_id,
'service': resource.service_name,
'type': resource.resource_type,
'name': resource.resource_name,
'region': resource.region,
'tags': resource.tags,
'confidence_score': resource.confidence_score,
})

Performance Monitoring

import time
from inventag.discovery.optimized_discovery import OptimizedAWSDiscovery

# Initialize with monitoring
discovery = OptimizedAWSDiscovery(regions=['us-east-1'])

start_time = time.time()
resources = discovery.discover_all_services()
end_time = time.time()

duration = end_time - start_time
rate = len(resources) / duration if duration > 0 else 0

print(f"Performance Metrics:")
print(f" - Total Resources: {len(resources)}")
print(f" - Discovery Time: {duration:.2f} seconds")
print(f" - Discovery Rate: {rate:.2f} resources/second")

# Quality analysis
high_confidence = [r for r in resources if r.confidence_score >= 0.7]
medium_confidence = [r for r in resources if 0.4 <= r.confidence_score < 0.7]
low_confidence = [r for r in resources if r.confidence_score < 0.4]

print(f"Quality Distribution:")
print(f" - High Confidence (≥0.7): {len(high_confidence)} ({len(high_confidence)/len(resources)*100:.1f}%)")
print(f" - Medium Confidence (0.4-0.7): {len(medium_confidence)} ({len(medium_confidence)/len(resources)*100:.1f}%)")
print(f" - Low Confidence (<0.4): {len(low_confidence)} ({len(low_confidence)/len(resources)*100:.1f}%)")

Configuration Options

Performance Tuning

# Adjust parallel processing
discovery.max_workers = 8 # Increase for more parallelism
discovery.operation_timeout = 45 # Increase for slower environments

# Service prioritization
discovery.priority_services = [
'ec2', 's3', 'rds', 'lambda', # Focus on specific services
'cloudfront', 'iam', 'route53'
]

# Enable/disable parallel processing
discovery.enable_parallel = True # Set to False for sequential processing

Service Filtering

# Discover specific services only
specific_discovery = OptimizedAWSDiscovery(regions=['us-east-1'])
specific_discovery.priority_services = ['s3', 'ec2', 'lambda']

resources = specific_discovery.discover_all_services()

Performance Benchmarks

Comparison with Legacy Systems

MetricLegacy DiscoveryIntelligent DiscoveryOptimized Discovery
Resources Found364652
Discovery Time58+ seconds60+ seconds4.03 seconds
Discovery Rate6.24 res/sec0.1 res/sec12.92 res/sec
Service Coverage15+ services2 services22+ services
High ConfidenceN/A0%98%
Missing ServicesNoneCloudFront, IAM, Route53None
AWS Managed FilteringNoNoYes

Performance Improvements

  • 3-4x Faster: Parallel processing reduces discovery time significantly
  • Better Coverage: Finds previously missing services (CloudFront, IAM, Route53)
  • Higher Quality: 98% of resources have high confidence scores (≥0.7)
  • More Reliable: Robust error handling and fallback mechanisms

Troubleshooting

Common Issues

Slow Discovery Performance

# Reduce parallelism for resource-constrained environments
discovery.max_workers = 2
discovery.operation_timeout = 60

# Focus on specific services
discovery.priority_services = ['ec2', 's3', 'rds']

Missing Resources

# Enable debug logging
import logging
logging.basicConfig(level=logging.DEBUG)

# Check service-specific patterns
discovery = OptimizedAWSDiscovery()
print("Supported services:", list(discovery.field_mapper.optimized_service_patterns.keys()))

Low Confidence Scores

# Analyze confidence factors
for resource in resources:
if resource.confidence_score < 0.7:
print(f"Low confidence resource: {resource.service_name}:{resource.resource_id}")
print(f" - Has ID: {bool(resource.resource_id and resource.resource_id != 'unknown')}")
print(f" - Has Name: {bool(resource.resource_name)}")
print(f" - Has ARN: {bool(resource.resource_arn)}")
print(f" - Has Tags: {bool(resource.tags)}")

Performance Optimization

Memory Usage

# Process regions sequentially for large environments
discovery = OptimizedAWSDiscovery(regions=['us-east-1']) # Single region
resources_east = discovery.discover_all_services()

discovery = OptimizedAWSDiscovery(regions=['us-west-2']) # Another region
resources_west = discovery.discover_all_services()

all_resources = resources_east + resources_west

API Rate Limiting

# Reduce concurrent operations
discovery.max_workers = 2
discovery.operation_timeout = 30

# Add delays between service discoveries
import time
discovery.enable_parallel = False # Sequential processing

Best Practices

Production Deployment

  1. Start with Single Region: Test with one region before expanding
  2. Monitor Performance: Track discovery time and resource counts
  3. Configure Timeouts: Set appropriate timeouts for your environment
  4. Use Service Filtering: Focus on relevant services for your use case
  5. Enable Logging: Use appropriate log levels for monitoring

Integration Guidelines

  1. Gradual Migration: Replace existing discovery incrementally
  2. Validate Results: Compare with existing systems during transition
  3. Monitor Quality: Track confidence scores and resource completeness
  4. Performance Testing: Benchmark in your specific environment
  5. Error Handling: Implement proper error handling and retry logic

Future Enhancements

Planned Improvements

  • Additional Service Patterns: Expand coverage to more AWS services
  • Adaptive Timeouts: Dynamic timeout adjustment based on service response times
  • Caching Layer: Resource caching for repeated discoveries
  • Metrics Export: Prometheus metrics for monitoring and alerting
  • Custom Patterns: User-defined service patterns for specialized use cases

Extensibility

The optimized discovery system is designed for easy extension:

# Add custom service patterns
discovery = OptimizedAWSDiscovery()
discovery.field_mapper.optimized_service_patterns['custom_service'] = {
"resource_types": ["CustomResource"],
"operations": ["ListCustomResources"],
"name_fields": ["CustomName"],
"global_service": False,
}

For more information, see: