# Terraform with the mongodbatlas provider

```hcl
provider "mongodbatlas" {
  public_key [your value]
  private_key value var.atlas_private_key
}

resource "mongodbatlas_project" "main" {
  name   = "production"
  org_id = var.org_id
}

resource "mongodbatlas_cluster" "main" {
  project_id   = mongodbatlas_project.main.id
  name         = "prod"
  cluster_type = "REPLICASET"
  provider_name = "AWS"
  provider_region_name = "US_EAST_1"
  provider_instance_size_name = "M10"
}

resource "mongodbatlas_database_user" "app" {
  project_id         = mongodbatlas_project.main.id
  username           = "app-svc"
  password value var.db_password
  auth_database_name = "admin"
  roles { role_name = "readWrite" database_name = "mydb" }
}

resource "mongodbatlas_project_ip_access_list" "office" {
  project_id = mongodbatlas_project.main.id
  cidr_block = "203.0.113.0/24"
  comment    = "office egress"
}
```

## Rules

- Pin the provider version. Provider upgrades change resource schemas.
- API keys for Terraform need project-level permissions; scope them to the one project and keep them out of the repo (env vars or a secret store).
- `terraform plan` in CI on every PR; `apply` only from the main branch. Control-plane changes deserve the same review as code.
- Import existing manually-created resources before managing them, or Terraform will try to recreate your cluster.

## Verify

`plan` shows no changes after `apply` (converged), and the Atlas UI reflects the resources. Then make one small change through a PR to prove the whole loop.