---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "mongodb_user Resource - terraform-provider-mongodb-driver"
subcategory: ""
description: |-
  User resource
---

# mongodb_user (Resource)

User resource

## Example Usage

```terraform
resource "mongodb_user" "example" {
  user = "my-user"
  db   = "my-db"
  pwd  = "super-secret-password"
}

// With role
resource "mongodb_user" "example" {
  user = "my-user"
  db   = "my-db"
  pwd  = "super-secret-password"

  roles = [
    // Only specifying "role" will use the same database as the user
    {
      role = "readWrite"
    },
    // Reference role in other database
    {
      role = "readWrite"
      db   = "admin"
    },
  ]
}

// With customData
resource "mongodb_user" "example" {
  user = "my-user"
  db   = "my-db"
  pwd  = "super-secret-password"

  custom_data = {
    "my-custom-field" = "my-custom-value"
  }
}

// With explicit mechanisms
resource "mongodb_user" "example" {
  user = "my-user"
  db   = "my-db"
  pwd  = "super-secret-password"

  mechanisms = [
    "SCRAM-SHA-256",
  ]
}

// With custom timeouts
resource "mongodb_user" "example" {
  user = "my-user"
  db   = "my-db"
  pwd  = "super-secret-password"

  // Timeouts default to 30 seconds
  timeouts = {
    create = "1m"    // 1 minute
    read   = "5s"    // 5 seconds
    update = "1m30s" // 1 minute & 30 seconds
    delete = "500ms" // 500 milliseconds (or 0.5 seconds)
  }
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `db` (String) Database this MongoDB user belongs to.

  MongoDB has some restrictions on database names. Such as:

  - Cannot contain any of the following characters (we're following Windows limits): `/\. "$*<>:|?`
  - Cannot create users in the `local` database.
  - Cannot be empty.
  - Cannot be longer than 64 characters.

  See documentation:

  - <https://www.mongodb.com/docs/manual/reference/command/createUser/#local-database>
  - <https://www.mongodb.com/docs/v6.0/reference/limits/#naming-restrictions>
- `pwd` (String, Sensitive) Password of this user.
- `user` (String) Username for this MongoDB user.

### Optional

- `custom_data` (Map of String) Any custom data for this user. Map of string key and values of arbitrary values.
- `mechanisms` (Set of String) Authentication mechanisms this user can use.

  - The default for featureCompatibilityVersion `4.0` is both `SCRAM-SHA-1` and `SCRAM-SHA-256`.
  - The default for featureCompatibilityVersion `3.6` is `SCRAM-SHA-1`.
- `roles` (Attributes Set) Roles this user belongs to. (see [below for nested schema](#nestedatt--roles))
- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))

### Read-Only

- `id` (String) User unique ID in MongoDB. Is composed from the `db` and `user` fields.

<a id="nestedatt--roles"></a>
### Nested Schema for `roles`

Required:

- `role` (String) Role name

Optional:

- `db` (String) Database this role belongs to. Leave unset to target same database as user.


<a id="nestedatt--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs.
- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Read operations occur during any refresh or planning operation when refresh is enabled.
- `update` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
