The recommended way to manage auto-scaling without encountering Terraform plan drift is to set use_effective_fields = true in the mongodbatlas_advanced_cluster resource.
This approach allows you to define an initial instance_size in your configuration, while Terraform uses the 'effective' values (the actual sizes Atlas has scaled to) to prevent unnecessary updates. You can use the mongodbatlas_advanced_cluster data source with use_effective_fields = true to read these actual values back into your configuration.
Module Authors: This is highly recommended for reusable modules to support both auto-scaling and non-auto-scaling clusters without requiring lifecycle blocks.
resource "mongodbatlas_advanced_cluster" "test" {
project_id = var.project_id
name = "AutoScalingCluster"
cluster_type = "SHARDED"
use_effective_fields = true
replication_specs = [{
region_configs = [{
electable_specs = {
instance_size = "M40"
node_count = 3
}
auto_scaling = {
compute_enabled = true
compute_max_instance_size = "M60"
}
# ...
}]
zone_name = "Zone 1"
}]
}
# Read effective values to see what Atlas has scaled to
data "mongodbatlas_advanced_cluster" "test" {
project_id = mongodbatlas_advanced_cluster.test.project_id
name = mongodbatlas_advanced_cluster.test.name
use_effective_fields = true
depends_on = [mongodbatlas_advanced_cluster.test]
}
output "shard_sizes" {
value = [
for idx, spec in data.mongodbatlas_advanced_cluster.test.replication_specs : {
shard_index = idx
configured_size = spec.region_configs[0].electable_specs.instance_size
actual_electable_size = spec.region_configs[0].effective_electable_specs.instance_size
actual_analytics_size = spec.region_configs[0].effective_analytics_specs.instance_size
}
]
}