Index Terraform/OpenTofu state files, detect drift, and power infra analytics.
Import and query your Terraform/OpenTofu states as s3db resources with automatic change tracking and intelligent partition-based queries.
1 line to get started:
plugins: [new TfStatePlugin({ filters: { types: ['aws_instance', 'aws_db_instance'], providers: ['aws'] } })]Key features:
- Flexible import: Local files, S3, glob patterns
- O(1) queries: Partitions by type, provider, serial
- Diff tracking: Compare versions and see changes
- SHA256 deduplication: Never imports same state twice
- Provider detection: Identifies aws, google, azure, kubernetes
Use cases:
- Infrastructure inventory and analytics
- Drift detection and compliance auditing
- Cost analysis and resource tracking
- CI/CD pipeline validation
import { Database } from 's3db.js';
import { TfStatePlugin } from 's3db.js';
const db = new Database({ connectionString: 's3://...' });
await db.connect();
const plugin = new TfStatePlugin({
filters: {
types: ['aws_instance', 'aws_db_instance', 'aws_s3_bucket'],
providers: ['aws']
}
});
await db.usePlugin(plugin);
// Import local state
await plugin.importState('./terraform.tfstate');
// Import from S3
await plugin.importStateFromS3('prod/terraform.tfstate');
// Import multiple states (glob)
await plugin.importStatesGlob('./terraform/**/*.tfstate');
// Query resources (O(1) partition-based)
const ec2Instances = await plugin.getResourcesByType('aws_instance');
const awsResources = await plugin.getResourcesByProvider('aws');
const rdsInstances = await plugin.getResourcesByProviderAndType('aws', 'aws_db_instance');
// Statistics
const stats = await plugin.getStats();
console.log(`Total: ${stats.totalResources} resources`);
console.log(`Providers: ${Object.keys(stats.providers).length}`);
// Change tracking
const diff = await plugin.getDiff('terraform.tfstate', 1, 2);
console.log(`Added: ${diff.summary.addedCount}`);
console.log(`Modified: ${diff.summary.modifiedCount}`);
console.log(`Deleted: ${diff.summary.deletedCount}`);Zero external dependencies - built directly into s3db.js core.
| Guide | Description |
|---|---|
| Configuration | All options, filter options, resource schemas, API reference |
| Usage Patterns | Import methods, query patterns, diff tracking, real-world examples |
| Best Practices | Performance optimization, troubleshooting, FAQ |
| Option | Type | Default | Description |
|---|---|---|---|
resourceName |
string | 'plg_tfstate_resources' |
Extracted resources table |
stateFilesName |
string | 'plg_tfstate_states' |
State files metadata table |
diffsName |
string | 'plg_tfstate_diffs' |
Change history table |
trackDiffs |
boolean | true |
Enable diff tracking |
asyncPartitions |
boolean | true |
70-100% faster writes |
filters.types |
array | - | Resource types to import |
filters.providers |
array | - | Providers to import |
| Resource | Description | Key Partitions |
|---|---|---|
plg_tfstate_states |
State file metadata | bySourceFile, bySerial |
plg_tfstate_resources |
Extracted infrastructure | byType, byProvider, byProviderAndType |
plg_tfstate_diffs |
Change history | bySourceFile, byOldSerial, byNewSerial |
// Import methods
await plugin.importState('./terraform.tfstate');
await plugin.importStateFromS3('prod/terraform.tfstate');
await plugin.importStatesGlob('./terraform/**/*.tfstate');
await plugin.importStatesFromS3Glob('**/terraform.tfstate');
// Query methods (O(1) partition-based)
await plugin.getResourcesByType('aws_instance');
await plugin.getResourcesByProvider('aws');
await plugin.getResourcesByProviderAndType('aws', 'aws_db_instance');
// Diff methods
await plugin.getDiff('terraform.tfstate', oldSerial, newSerial);
await plugin.getLatestDiff('terraform.tfstate');
await plugin.getAllDiffs('terraform.tfstate');
// Statistics
await plugin.getStats();
await plugin.getStatsByProvider();
await plugin.getStatsByType();| Prefix | Provider |
|---|---|
aws_* |
aws |
google_* |
google |
azurerm_* |
azure |
kubernetes_* |
kubernetes |
- Import states from local files, S3, or using glob patterns
- SHA256 deduplication prevents duplicate imports
- Extract resources with provider detection
- Index with partitions for O(1) queries by type, provider, serial
- Track diffs between state versions automatically
- Query and analyze your infrastructure inventory
const plugin = new TfStatePlugin();
await db.usePlugin(plugin);
await plugin.importState('./terraform.tfstate');const plugin = new TfStatePlugin({
filters: {
types: ['aws_instance', 'aws_db_instance', 'aws_s3_bucket'],
providers: ['aws', 'google'],
exclude: ['data.*'] // Exclude data sources
}
});const infraPlugin = new TfStatePlugin({ namespace: 'infra' });
const appsPlugin = new TfStatePlugin({ namespace: 'apps' });
// Resources: plg_infra_tfstate_resources, plg_apps_tfstate_resources- ✅ Terraform (all versions)
- ✅ OpenTofu (all versions)
- ✅ State format versions: v3, v4
- ✅ Backends: local, S3, GCS, Azure Blob, HTTP
- ✅ Providers: AWS, Google Cloud, Azure, Kubernetes, and others
- Cloud Inventory Plugin - Real-time cloud resource discovery
- Metrics Plugin - Monitor infrastructure performance
- Audit Plugin - Track all infrastructure changes