mongodb / terraform-provider-mongodbatlas

Terraform MongoDB Atlas Provider: Deploy, update, and manage MongoDB Atlas infrastructure as code through HashiCorp Terraform
https://registry.terraform.io/providers/mongodb/mongodbatlas
Mozilla Public License 2.0
242 stars 169 forks source link

INVALID_CLUSTER_CONFIGURATION when adding new regions #550

Closed slamdev closed 2 years ago

slamdev commented 3 years ago

Terraform CLI and Terraform MongoDB Atlas Provider Version

Terraform v1.0.5 on darwin_amd64
mongo provider: 1.0.0

Terraform Output

Terraform will perform the following actions:

  # mongodbatlas_cluster.runtime will be updated in-place
  ~ resource "mongodbatlas_cluster" "runtime" {
        id                                              = "********"
        name                                            = "prod"
        # (31 unchanged attributes hidden)

      - replication_specs {
          - id         = "******" -> null
          - num_shards = 1 -> null
          - zone_name  = "Zone 1" -> null

          - regions_config {
              - analytics_nodes = 0 -> null
              - electable_nodes = 3 -> null
              - priority        = 7 -> null
              - read_only_nodes = 0 -> null
              - region_name     = "EUROPE_WEST_4" -> null
            }
        }
      + replication_specs {
          + id         = (known after apply)
          + num_shards = 1
          + zone_name  = "Zone 1"

          + regions_config {
              + analytics_nodes = 0
              + electable_nodes = 0
              + priority        = 0
              + read_only_nodes = 2
              + region_name     = "SOUTHEASTERN_ASIA_PACIFIC"
            }
          + regions_config {
              + analytics_nodes = 0
              + electable_nodes = 3
              + priority        = 7
              + read_only_nodes = 0
              + region_name     = "EUROPE_WEST_4"
            }
        }
        # (2 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
mongodbatlas_cluster.runtime: Modifying... [id=****]
╷
│ Error: error updating MongoDB Cluster (runtime-prod): PATCH https://cloud.mongodb.com/api/atlas/v1.0/groups/*******/clusters/prod: 400 (request "INVALID_CLUSTER_CONFIGURATION") The specified cluster configuration is not valid.
│
│   with mongodbatlas_cluster.runtime,
│   on main.tf line 23, in resource "mongodbatlas_cluster" "runtime":
│   23: resource "mongodbatlas_cluster" "runtime" {
│

Steps to Reproduce

  1. Create a replicaset cluster
  2. Add a new read only region replica

Expected Behavior

New replica is added to the cluster or a meaningful error message appears

themantissa commented 3 years ago

@slamdev I just tried to reproduce with a similar config and could not. I got the expected add of read only nodes. If you continue to experience issues please feel free to include more information here.

slamdev commented 3 years ago
2021-09-02T14:22:48.874+0200 [INFO]  provider.terraform-provider-mongodbatlas_v1.0.0: 2021/09/02 14:22:48 [DEBUG] MongoDB Atlas API Request Details:
---[ REQUEST ]---------------------------------------
PATCH /api/atlas/v1.0/groups/**********/clusters/runtime-prod HTTP/1.1
Host: cloud.mongodb.com
User-Agent: terraform-provider-mongodbatlas/1.0.0 go-mongodbatlas/0.11.0 (darwin;amd64)
Content-Length: 292
Accept: application/json
Content-Type: application/json
Accept-Encoding: gzip

{
 "autoScaling": {
  "compute": {}
 },
 "replicationSpecs": [
  {
   "numShards": 1,
   "zoneName": "Zone 1",
   "regionsConfig": {
    "EUROPE_WEST_4": {
     "analyticsNodes": 0,
     "electableNodes": 3,
     "priority": 7,
     "readOnlyNodes": 0
    },
    "SOUTHEASTERN_ASIA_PACIFIC": {
     "analyticsNodes": 0,
     "electableNodes": 0,
     "priority": 0,
     "readOnlyNodes": 2
    }
   }
  }
 ]
}

-----------------------------------------------------: timestamp=2021-09-02T14:22:48.874+0200
2021-09-02T14:22:49.552+0200 [INFO]  provider.terraform-provider-mongodbatlas_v1.0.0: 2021/09/02 14:22:49 [DEBUG] MongoDB Atlas API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 400 Bad Request
Content-Length: 199
Content-Type: application/json
Date: Thu, 02 Sep 2021 12:22:49 GMT
Server: envoy
Strict-Transport-Security: max-age=31536000
X-Envoy-Upstream-Service-Time: 239
X-Frame-Options: DENY
X-Mongodb-Service-Version: gitHash=*****; versionString=v20210824

{
 "detail": "The specified cluster configuration is not valid.",
 "error": 400,
 "errorCode": "INVALID_CLUSTER_CONFIGURATION",
 "parameters": [
  "Updating id of existing replication spec"
 ],
 "reason": "Bad Request"
}

that is a debug output from terraform. I would suggest to improve the error messages atlas api generates, so they clearly show what is wrong with the request

themantissa commented 3 years ago

@slamdev I will pass on to the team the request for better error messaging. This is still not enough to determine why you are hitting an issue as nothing here is jumping out as a problem. If you can include the full config and logs that may help or reach out to support to be able to provide that via our ticketing system. @nikhil-mongo can you respond if more information comes in?

Eji4h commented 3 years ago

Terraform v1.0.7 on darwin_arm64 MongoDB Atlas Provider version 1.0.1

I have same issue when create new cluster and apply terraform again without change mongodbatlas_cluster resource.

Terraform configuration

resource "mongodbatlas_cluster" "test" {
  project_id   = var.mongodbatlas_project_id
  name         = "test-${var.environment}"
  cluster_type = "SHARDED"
  num_shards = 2
  replication_specs {
    num_shards = 2
    regions_config {
      region_name     = local.atlas_region
      electable_nodes = 3
      priority        = 7
      read_only_nodes = 0
    }
  }
  cloud_backup                 = true
  auto_scaling_disk_gb_enabled = true
  mongo_db_major_version       = "4.4"

  //Provider Settings "block"
  provider_name               = "AWS"
  disk_size_gb                = 40
  provider_instance_size_name = "M30"
}

Terraform Output

image image

Steps to Reproduce

  1. Create a shard cluster.
  2. Terraform apply without change mongodbatlas_cluster resource.

Expected Behavior

No change and no error from terraform.

Eji4h commented 3 years ago

Terraform v1.0.7 on darwin_arm64 MongoDB Atlas Provider version 1.0.1

I have same issue when create new cluster and apply terraform again without change mongodbatlas_cluster resource.

Terraform configuration

resource "mongodbatlas_cluster" "test" {
  project_id   = var.mongodbatlas_project_id
  name         = "test-${var.environment}"
  cluster_type = "SHARDED"
  num_shards = 2
  replication_specs {
    num_shards = 2
    regions_config {
      region_name     = local.atlas_region
      electable_nodes = 3
      priority        = 7
      read_only_nodes = 0
    }
  }
  cloud_backup                 = true
  auto_scaling_disk_gb_enabled = true
  mongo_db_major_version       = "4.4"

  //Provider Settings "block"
  provider_name               = "AWS"
  disk_size_gb                = 40
  provider_instance_size_name = "M30"
}

Terraform Output

image image

Steps to Reproduce

  1. Create a shard cluster.
  2. Terraform apply without change mongodbatlas_cluster resource.

Expected Behavior

No change and no error from terraform.

This issue was resolve. Because I set the wrong region (AP-SOUTHEAST-1). It should be used underscore (_) to replace the dash (-).

themantissa commented 3 years ago

Internal ticket INTMDB-254

themantissa commented 3 years ago

Update: We believe we have found the cause of the regression - it started in 1.0.0 with a type change. Team is working to correct and add in testing to assure this is better caught in the future.

themantissa commented 3 years ago

We've got a fix in https://github.com/mongodb/terraform-provider-mongodbatlas/pull/573 and will be in the next release 1.0.2.

themantissa commented 3 years ago

We have released the pre-release https://github.com/mongodb/terraform-provider-mongodbatlas/releases/tag/v1.0.2-pre.1 If anyone here could test and verify before release that would be great.

themantissa commented 3 years ago

1.0.2 is out. Please let us know if any issues remain.

buker commented 3 years ago

I had same issue. Version 1.0.2 works perfect. Thanks

BlacCello commented 2 years ago

The issue is still present for us. We used provider 1.0.2 and terraform 1.0.9 and I can provide an example which we used for debugging, which is unrelated to our big project code.

With the following snippets it is possible to reproduce the issue

An already existing cluster is configured with the following code:

terraform {
  required_providers {
    mongodbatlas = {
      source  = "mongodb/mongodbatlas"
      version = "~> 1.0.2"
    }
  }
  required_version = ">= 1.0.9"
}

resource "mongodbatlas_cluster" "cluster" {
  project_id = "XXX"
  name       = "debug"

  replication_factor           = 3
  cloud_backup                 = false
  pit_enabled                  = false
  auto_scaling_disk_gb_enabled = false
  mongo_db_major_version       = "4.2"

  provider_name               = "AZURE"
  provider_instance_size_name = "M10"
  provider_region_name        = "EUROPE_WEST"
}

The creation of the cluster like this works without issues. Then, we want to change it to multi-region with:

resource "mongodbatlas_cluster" "cluster" {
  project_id = "XXX"
  name       = "debug"

  cloud_backup                 = false
  pit_enabled                  = false
  auto_scaling_disk_gb_enabled = false
  mongo_db_major_version       = "4.2"
  cluster_type                 = "REPLICASET"

  num_shards = 1

  // Provider Settings "block"
  provider_name               = "AZURE"
  provider_instance_size_name = "M10"

  replication_specs {
    num_shards = 1
    regions_config {
      region_name     = "EUROPE_WEST"
      electable_nodes = 1
      priority        = 7
      read_only_nodes = 0
    }
    regions_config {
      region_name     = "EUROPE_NORTH"
      electable_nodes = 1
      priority        = 6
      read_only_nodes = 0
    }
    regions_config {
      region_name     = "GERMANY_WEST_CENTRAL"
      electable_nodes = 1
      priority        = 5
      read_only_nodes = 0
    }
  }
}

The plan looks as follows:

# mongodbatlas_cluster.cluster will be updated in-place
  ~ resource "mongodbatlas_cluster" "cluster" {
        id                                      = "XXXXXXXXXXXX"
        name                                    = "debug"
        # (29 unchanged attributes hidden)

      - replication_specs {
          - id         = "XXXXXXXXXXXXXX" -> null
          - num_shards = 1 -> null
          - zone_name  = "Zone 1" -> null

          - regions_config {
              - analytics_nodes = 0 -> null
              - electable_nodes = 3 -> null
              - priority        = 7 -> null
              - read_only_nodes = 0 -> null
              - region_name     = "EUROPE_WEST" -> null
            }
        }
      + replication_specs {
          + id         = (known after apply)
          + num_shards = 1
          + zone_name  = "ZoneName managed by Terraform"

          + regions_config {
              + analytics_nodes = 0
              + electable_nodes = 1
              + priority        = 5
              + read_only_nodes = 0
              + region_name     = "GERMANY_WEST_CENTRAL"
            }
          + regions_config {
              + analytics_nodes = 0
              + electable_nodes = 1
              + priority        = 6
              + read_only_nodes = 0
              + region_name     = "EUROPE_NORTH"
            }
          + regions_config {
              + analytics_nodes = 0
              + electable_nodes = 1
              + priority        = 7
              + read_only_nodes = 0
              + region_name     = "EUROPE_WEST"
            }
        }
        # (2 unchanged blocks hidden)
    }

However, when we run terraform apply with the listed plan, we get the following error: Error: error updating MongoDB Cluster (debug): PATCH https://cloud.mongodb.com/api/atlas/v1.0/groups/XXXX/clusters/debug: 400 (request "INVALID_CLUSTER_CONFIGURATION") The specified cluster configuration is not valid.

If we create the multi-region cluster from scratch, it works without issues, so the configuration in general has to be correct. We have already tested this migration with provider version 0.6 a few months ago and it worked, so this could also be a regression.

Just a guess: It could relate to the replication_factor attribute that we still have applied to the "old clusters". Migrating to single region specified with replication_specs also does not work.

themantissa commented 2 years ago

@BlacCello I feel this may be a slightly separate issue as we've had multiple reports of the original being corrected w/ the fix.

_Just a guess: It could relate to the replication_factor attribute that we still have applied to the "old clusters". Migrating to single region specified with replicationspecs also does not work. That was going to be my question, if you tried updating your older format. So you did and this did not help?

Could you file a new issue and include any relevant logs? That will help us repro faster. If not we'll need more time to repro and investigate the issue.

BlacCello commented 2 years ago

@themantissa I created a new issue #588 and also included the logs with TF_LOG=trace. I hope this helps.

themantissa commented 2 years ago

Fixed in 1.0.2. See #588 for possibly related situation.