Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

region_configs.#.___specs.instance_size in mongodbatlas_advanced_cluster is required #1288

Closed
jdegger opened this issue Jul 3, 2023 · 2 comments
Labels

Comments

@jdegger
Copy link

jdegger commented Jul 3, 2023

Terraform CLI and Terraform MongoDB Atlas Provider Version

1.5.2 / 1.10.0

Terraform Configuration File

Seemingly irrelevant

resource "mongodbatlas_advanced_cluster" "xg_mongo_cluster" {
  for_each = var.cluster_config

  project_id                     = var.project_id
  name                           = each.key
  cluster_type                   = "REPLICASET"
  mongo_db_major_version         = each.value.mongo_db_major_version
  termination_protection_enabled = true

  disk_size_gb   = 20
  backup_enabled = true

  replication_specs {
    num_shards = 1
    zone_name  = "Zone 1"

    // Note: total number of electable_nodes must be 3, 5 or 7 accross all regions.
    // We currently use 3 + 2 = 5.

    // Note: priority must be unique accross all regions.
    // The advice is to start with 7 and count down, and use 0 for read-only regions.

    dynamic "region_configs" {
      for_each = each.value.replication_regions

      content {

        // generic config
        region_name   = region_configs.value.region_name
        priority      = region_configs.value.priority
        provider_name = "GCP"

        auto_scaling {
          disk_gb_enabled            = true
          compute_enabled            = true
          compute_scale_down_enabled = true
          compute_min_instance_size  = each.value.compute_min_instance_size
          compute_max_instance_size  = each.value.compute_max_instance_size
        }

        // electables, read only, and analytics nodes
        electable_specs {
          node_count = region_configs.value.electable_nodes
        }

        read_only_specs {
          node_count = region_configs.value.read_only_nodes
        }

        analytics_specs {
          instance_size = each.value.analytics_size
          node_count    = region_configs.value.analytics_nodes
        }

      }
    }
  }

  advanced_configuration {
    minimum_enabled_tls_protocol = "TLS1_2"
  }

  lifecycle {
    prevent_destroy = true
    ignore_changes = [
      // These attributes can change after provisioning because we have auto scaling enabled.
      disk_size_gb,
    ]
  }

  depends_on = [
    google_compute_network_peering.xg_atlas_network_peering,
  ]
}
provider "mongodbatlas" {
  public_key  = var.MONGODB_ATLAS_PUBLIC_KEY
  private_key = var.MONGODB_ATLAS_PRIVATE_KEY
}

# Common MongoDB infrastructure
module "xg_mongodb_shared" {
  source = "../../modules/shared/mongo"

  project_id              = var.MONGODB_ATLAS_PROJECT_ID
  gcp_project_id          = var.GCP_PROJECT
  cidr_whitelist          = [for region, cidr in module.xg_google_shared.vpc_cidr_ranges : cidr]
  enable_cpu_auto_scaling = true

  users = [for user in local.users : {
    email = user.email,
    role  = user.mongo_atlas_role
  } if user.mongo_atlas_role != null]

  cluster_config = {
    xg-cluster-main = {

      // cluster size
      compute_min_instance_size = "M20"
      compute_max_instance_size = "M40"
      analytics_size            = "M20"

      // config
      mongo_db_major_version = "5.0"

      // regions
      replication_regions = [
        {
          region_name     = "WESTERN_EUROPE" // = GCP europe-west1 region
          electable_nodes = 3
          priority        = 7
          read_only_nodes = 0
          analytics_nodes = 0
        },
        {
          region_name     = "EUROPE_WEST_4" // = GCP europe-west4 region
          electable_nodes = 2
          priority        = 6
          read_only_nodes = 0
          analytics_nodes = 0
        }
      ]
    }
    xgac-history = {

      // cluster size
      compute_min_instance_size = "M10"
      compute_max_instance_size = "M10"
      analytics_size            = "M40"

      // config
      mongo_db_major_version = "6.0"

      // regions
      replication_regions = [
        {
          region_name     = "WESTERN_EUROPE" // = GCP europe-west1 region
          electable_nodes = 2
          priority        = 7
          read_only_nodes = 0
          analytics_nodes = 0
        },
        {
          region_name     = "EUROPE_WEST_4" // = GCP europe-west4 region
          electable_nodes = 1
          priority        = 6
          read_only_nodes = 0
          analytics_nodes = 1
        }
      ]
    }
  }

  depends_on = [
    module.xg_google_shared,
  ]
}

Steps to Reproduce

When we plan a mongodb_advanced_cluster without the instance_size defined, we get an error

Expected Behavior

No error, use min and max of auto-scaling

Actual Behavior

afbeelding

Debug Output

Crash Output

Additional Context

We are trying to enable auto-scaling. However this is seemingly impossible as we can not ignore the lifecycle_change as suggested in the docs (it simply does not exist).

Since instance_size is nested in a list, we do not know how to ignore the lifecylce change dynamically (.0 is not an option as we use for_each)

References

https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster#instance_size

Shows as optional

@github-actions
Copy link
Contributor

github-actions bot commented Jul 3, 2023

Thanks for opening this issue. The ticket INTMDB-909 was created for internal tracking.

@colm-quinn
Copy link
Collaborator

Hi @jdegger,
While we investigate the issue further, some early suggestions that might be useful here.

We are trying to enable auto-scaling. However, this is seemingly impossible as we can not ignore the lifecycle_change as suggested in the docs (it simply does not exist).

Below is an example of ignore changes for instance_size in a static block. Can you let us know if this syntax works for you?

lifecycle {
--
ignore_changes = [
       disk_size_gb,
       replication_specs[0].region_configs[0].electable_specs[0].instance_size,
       replication_specs[0].region_configs[1].electable_specs[0].instance_size,
       replication_specs[0].region_configs[2].electable_specs[0].instance_size,
   ]
}

Since instance_size is nested in a list, we do not know how to ignore the lifecylce change dynamically (.0 is not an option as we use for_each)

Only literal values are supported in Terraform for lifecycle, as the processing happens too early for expression evaluation. See Terraform docs for more details: https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle#literal-values-only

https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster#instance_size Shows as optional

There may be a docs improvement here, as the instance size is required and then ignored via lifecycle if required for auto scaling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants