Skip to content

Azure/terraform-azurerm-avm-res-documentdb-mongocluster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terraform-azurerm-avm-res-documentdb-mongocluster

This module manages a MongoDB Cluster using vCore Architecture.

Upgrading from v0.1.0 to v0.2.0

v0.2.0 renamed the internal cluster resource from azapi_resource.mongo_cluster to azapi_resource.this and moved the API version to 2025-09-01. The module now ships a moved block, so upgrading no longer plans a destroy/recreate of an existing cluster — run terraform plan and confirm the state is migrated in place. If you previously worked around this with terraform state mv, the moved block is a harmless no-op.

The create-time-only properties create_mode, storage_type, and customer-managed-key encryption (with its managed identity) now default to being omitted unless you set them, so a plain upgrade with no new inputs produces a clean, no-op plan.

Note: create_mode, storage_type, customer_managed_key, and managed_identities are create-time-only on Cosmos DB for MongoDB vCore. Changing any of them on an existing cluster forces a replacement (destroy + create) — Terraform will show # forces replacement. To enable CMK encryption on an already-provisioned cluster you must create a new, CMK-enabled cluster and migrate/restore into it; it cannot be enabled in place.

Requirements

The following requirements are needed by this module:

Resources

The following resources are used by this module:

Required Inputs

The following input variables are required:

Description: Administrator (cluster) login name. Must start with a letter; allowed: letters, numbers, - and _.

Type: string

Description: Administrator login password (sensitive). Prefer injecting from a secure external secret source (e.g., TF Cloud Var, Key Vault data source).

Type: string

Description: Azure region where the resource should be deployed.

Type: string

Description: The name of the this resource.

Type: string

Description: The fully-qualified ARM resource ID of the resource group where the MongoDB vCore cluster will be deployed.

Type: string

Optional Inputs

The following input variables are optional (have default values):

Description: (Optional, 2025-09-01+) Allowed authentication modes for the cluster: 'NativeAuth' and/or 'MicrosoftEntraID'. When empty the API default (NativeAuth only) is used.

Type: list(string)

Default: []

Description: Backup policy type (e.g., Periodic, Continuous7Days, Continuous30Days).

Type: string

Default: "Continuous7Days"

Description: Compute tier (e.g., M30, M40, etc.).

Type: string

Default: "M30"

Description: Cluster creation mode: 'Default', 'GeoReplica', 'PointInTimeRestore', or 'Replica'. Introduced in API version 2025-09-01. Defaults to null (property omitted) so existing clusters remain unaffected. This is a create-time-only property; changing it forces a replacement.

Type: string

Default: null

Description: Customer-managed key encryption settings for the MongoDB cluster. When specified, the cluster will be encrypted using a key from your Key Vault.
Interface-compliant properties include:

  • key_vault_resource_id - (Required) The resource ID of the Key Vault where the key is stored.
  • key_name - (Required) The name of the key in the vault.
  • key_version - (Optional) The version of the key. If not specified, the latest version is used.
  • user_assigned_identity - (Optional in AVM interface) An object with the user-assigned managed identity. Includes:
    • resource_id - (Required) The resource ID of the user-assigned identity.

Note: This module requires user_assigned_identity.resource_id when customer_managed_key is set because
the MongoDB 2025-09-01 API requires keyEncryptionKeyIdentity for customer-managed keys.

Type:

object({
    key_vault_resource_id = string
    key_name              = string
    key_version           = optional(string, null)
    user_assigned_identity = optional(object({
      resource_id = string
    }), null)
  })

Default: null

Description: (Optional, 2025-09-01+) Enable or disable the Mongo Data API: 'Enabled' or 'Disabled'. When null the API default is used.

Type: string

Default: null

Description: A map of diagnostic settings to create on the Key Vault. The map key is deliberately arbitrary to avoid issues where map keys maybe unknown at plan time.

  • name - (Optional) The name of the diagnostic setting. One will be generated if not set, however this will not be unique if you want to create multiple diagnostic setting resources.
  • log_categories - (Optional) A set of log categories to send to the log analytics workspace. Defaults to [].
  • log_groups - (Optional) A set of log groups to send to the log analytics workspace. Defaults to ["allLogs"].
  • metric_categories - (Optional) A set of metric categories to send to the log analytics workspace. Defaults to ["AllMetrics"].
  • log_analytics_destination_type - (Optional) The destination type for the diagnostic setting. Possible values are Dedicated and AzureDiagnostics. Defaults to Dedicated.
  • workspace_resource_id - (Optional) The resource ID of the log analytics workspace to send logs and metrics to.
  • storage_account_resource_id - (Optional) The resource ID of the storage account to send logs and metrics to.
  • event_hub_authorization_rule_resource_id - (Optional) The resource ID of the event hub authorization rule to send logs and metrics to.
  • event_hub_name - (Optional) The name of the event hub. If none is specified, the default event hub will be selected.
  • marketplace_partner_resource_id - (Optional) The full ARM resource ID of the Marketplace resource to which you would like to send Diagnostic LogsLogs.

Type:

map(object({
    name                                     = optional(string, null)
    log_categories                           = optional(set(string), [])
    log_groups                               = optional(set(string), ["allLogs"])
    metric_categories                        = optional(set(string), ["AllMetrics"])
    log_analytics_destination_type           = optional(string, "Dedicated")
    workspace_resource_id                    = optional(string, null)
    storage_account_resource_id              = optional(string, null)
    event_hub_authorization_rule_resource_id = optional(string, null)
    event_hub_name                           = optional(string, null)
    marketplace_partner_resource_id          = optional(string, null)
  }))

Default: {}

Description: (Deprecated) Previous preview boolean for HA. If set and ha_mode not overridden, true=>SameZone, false=>Disabled.

Type: bool

Default: null

Description: This variable controls whether or not telemetry is enabled for the module.
For more information see https://aka.ms/avm/telemetryinfo.
If it is set to false, then no telemetry will be collected.

Type: bool

Default: true

Description: List of firewall rules (public IP ranges) applied when public network access is Enabled. Note: Only specify firewall_rules when public_network_access is 'Enabled'.

Type:

list(object({
    name     = string
    start_ip = string
    end_ip   = string
  }))

Default: []

Description: High availability target mode for the cluster: Disabled, SameZone, ZoneRedundantPreferred. (Legacy value ZoneRedundant maps to ZoneRedundantPreferred).

Type: string

Default: "Disabled"

Description: Controls the Resource Lock configuration for this resource. The following properties can be specified:

  • kind - (Required) The type of lock. Possible values are \"CanNotDelete\" and \"ReadOnly\".
  • name - (Optional) The name of the lock. If not specified, a name will be generated based on the kind value. Changing this forces the creation of a new resource.

Type:

object({
    kind = string
    name = optional(string, null)
  })

Default: null

Description: Controls the Managed Identity configuration on this resource. The following properties can be specified:

  • system_assigned - (Optional) Specifies if the System Assigned Managed Identity should be enabled.
  • user_assigned_resource_ids - (Optional) Specifies a list of User Assigned Managed Identity resource IDs to be assigned to this resource.

Type:

object({
    system_assigned            = optional(bool, false)
    user_assigned_resource_ids = optional(set(string), [])
  })

Default: {}

Description: (Deprecated) Previous preview node_count for nodeGroupSpecs. Ignored in 2024-07-01 api; use shard_count instead.

Type: number

Default: null

Description: (Optional, 2025-09-01+) Preview features to opt into. Currently supported value: 'GeoReplicas'.

Type: list(string)

Default: []

Description: A map of private endpoint connection approvals to manage on this cluster.
The map key is the connection name (as assigned by Azure when the private endpoint was created).

  • private_link_service_connection_state.status - (Required) Approval state: 'Approved', 'Pending', or 'Rejected'.
  • private_link_service_connection_state.description - (Optional) Reason for approval or rejection.
  • private_link_service_connection_state.actions_required - (Optional) Message about required consumer-side changes.

Note: use var.private_endpoints to create private endpoints. This variable manages the approval state of connections that already exist (e.g. created externally or by var.private_endpoints).

Type:

map(object({
    private_link_service_connection_state = object({
      status           = string
      description      = optional(string, null)
      actions_required = optional(string, null)
    })
  }))

Default: {}

Description: A map of private endpoints to create on this resource. The map key is deliberately arbitrary to avoid issues where map keys maybe unknown at plan time.

  • name - (Optional) The name of the private endpoint. One will be generated if not set.
  • role_assignments - (Optional) A map of role assignments to create on the private endpoint. The map key is deliberately arbitrary to avoid issues where map keys maybe unknown at plan time. See var.role_assignments for more information.
  • lock - (Optional) The lock level to apply to the private endpoint. Default is None. Possible values are None, CanNotDelete, and ReadOnly.
  • tags - (Optional) A mapping of tags to assign to the private endpoint.
  • subnet_resource_id - The resource ID of the subnet to deploy the private endpoint in.
  • private_dns_zone_group_name - (Optional) The name of the private DNS zone group. One will be generated if not set.
  • private_dns_zone_resource_ids - (Optional) A set of resource IDs of private DNS zones to associate with the private endpoint. If not set, no zone groups will be created and the private endpoint will not be associated with any private DNS zones. DNS records must be managed external to this module.
  • application_security_group_resource_ids - (Optional) A map of resource IDs of application security groups to associate with the private endpoint. The map key is deliberately arbitrary to avoid issues where map keys maybe unknown at plan time.
  • private_service_connection_name - (Optional) The name of the private service connection. One will be generated if not set.
  • network_interface_name - (Optional) The name of the network interface. One will be generated if not set.
  • location - (Optional) The Azure location where the resources will be deployed. Defaults to the location of the resource group.
  • resource_group_name - (Optional) The resource group where the resources will be deployed. Defaults to the resource group of this resource.
  • ip_configurations - (Optional) A map of IP configurations to create on the private endpoint. If not specified the platform will create one. The map key is deliberately arbitrary to avoid issues where map keys maybe unknown at plan time.
    • name - The name of the IP configuration.
    • private_ip_address - The private IP address of the IP configuration.

Type:

map(object({
    name = optional(string, null)
    role_assignments = optional(map(object({
      role_definition_id_or_name             = string
      principal_id                           = string
      description                            = optional(string, null)
      skip_service_principal_aad_check       = optional(bool, false)
      condition                              = optional(string, null)
      condition_version                      = optional(string, null)
      delegated_managed_identity_resource_id = optional(string, null)
      principal_type                         = optional(string, null)
    })), {})
    lock = optional(object({
      kind = string
      name = optional(string, null)
    }), null)
    tags                                    = optional(map(string), null)
    subnet_resource_id                      = string
    private_dns_zone_group_name             = optional(string, "default")
    private_dns_zone_resource_ids           = optional(set(string), [])
    application_security_group_associations = optional(map(string), {})
    private_service_connection_name         = optional(string, null)
    network_interface_name                  = optional(string, null)
    location                                = optional(string, null)
    resource_group_name                     = optional(string, null)
    ip_configurations = optional(map(object({
      name               = string
      private_ip_address = string
    })), {})
  }))

Default: {}

Description: Whether to manage private DNS zone groups with this module. If set to false, you must manage private DNS zone groups externally, e.g. using Azure Policy.

Type: bool

Default: true

Description: Enable or disable public network access: Enabled or Disabled.

Type: string

Default: "Disabled"

Description: (Optional, 2025-09-01+) Source cluster parameters for geo-replica or replica creation. Required when create_mode is 'GeoReplica' or 'Replica'.

Type:

object({
    source_location    = string
    source_resource_id = string
  })

Default: null

Description: Optional overrides for the MongoDB vCore cluster resource types and API versions.

Type:

object({
    mongo_cluster               = optional(string, "Microsoft.DocumentDB/mongoClusters@2025-09-01")
    firewall_rule               = optional(string, "Microsoft.DocumentDB/mongoClusters/firewallRules@2025-09-01")
    private_endpoint_connection = optional(string, "Microsoft.DocumentDB/mongoClusters/privateEndpointConnections@2025-09-01")
    user                        = optional(string, "Microsoft.DocumentDB/mongoClusters/users@2025-09-01")
  })

Default: {}

Description: (Optional, 2025-09-01+) Restore parameters for point-in-time restore. Required when create_mode is 'PointInTimeRestore'.

Type:

object({
    point_in_time_utc  = optional(string, null)
    source_resource_id = optional(string, null)
  })

Default: null

Description: Retry configuration applied to the root azapi_resource. Defaults to null (provider defaults).

Type:

object({
    error_message_regex  = optional(list(string))
    interval_seconds     = optional(number)
    max_interval_seconds = optional(number)
  })

Default: null

Description: A map of role assignments to create on this resource. The map key is deliberately arbitrary to avoid issues where map keys maybe unknown at plan time.

  • role_definition_id_or_name - The ID or name of the role definition to assign to the principal.
  • principal_id - The ID of the principal to assign the role to.
  • description - The description of the role assignment.
  • skip_service_principal_aad_check - If set to true, skips the Azure Active Directory check for the service principal in the tenant. Defaults to false.
  • condition - The condition which will be used to scope the role assignment.
  • condition_version - The version of the condition syntax. Valid values are '2.0'.
  • delegated_managed_identity_resource_id - The delegated Azure Resource Id which contains a Managed Identity. Changing this forces a new resource to be created.
  • principal_type - The type of the principal_id. Possible values are User, Group and ServicePrincipal. Changing this forces a new resource to be created. It is necessary to explicitly set this attribute when creating role assignments if the principal creating the assignment is constrained by ABAC rules that filters on the PrincipalType attribute.

Note: only set skip_service_principal_aad_check to true if you are assigning a role to a service principal.

Type:

map(object({
    role_definition_id_or_name             = string
    principal_id                           = string
    description                            = optional(string, null)
    skip_service_principal_aad_check       = optional(bool, false)
    condition                              = optional(string, null)
    condition_version                      = optional(string, null)
    delegated_managed_identity_resource_id = optional(string, null)
    principal_type                         = optional(string, null)
  }))

Default: {}

Description: MongoDB server version (e.g. 7.0).

Type: string

Default: "7.0"

Description: Number of shards (properties.sharding.shardCount).

Type: number

Default: 1

Description: Cluster storage size in GB (service supported increments).

Type: number

Default: 32

Description: (Optional, 2025-09-01+) Storage type to provision: 'PremiumSSD' or 'PremiumSSDv2'. Defaults to null (property omitted) so existing clusters remain unaffected; new clusters that omit it inherit the service default (PremiumSSD). This is a create-time-only property; changing it forces a replacement.

Type: string

Default: null

Description: (Optional) Tags of the resource.

Type: map(string)

Default: null

Description: Per-operation timeouts applied to the root azapi_resource. Defaults to null (provider defaults).

Type:

object({
    create = optional(string)
    read   = optional(string)
    update = optional(string)
    delete = optional(string)
  })

Default: null

Description: A map of users to create on this cluster. The map key is the user name.

For NativeAuth users the key is the login name (alphanumeric + hyphens, 1-63 chars).
For MicrosoftEntraID users the key must be the Entra principal's object ID (GUID).

  • roles - (Required) List of database roles. Each entry needs:
    • db - Database scope (e.g. 'admin').
    • role - Role name. Currently only 'root' is supported.
  • identity_provider - (Optional) Omit for native-auth users.
    • type - Must be 'MicrosoftEntraID'.
    • properties.principal_type - 'servicePrincipal' or 'user'.

Type:

map(object({
    roles = list(object({
      db   = string
      role = string
    }))
    identity_provider = optional(object({
      type = string
      properties = optional(object({
        principal_type = string
      }), null)
    }), null)
  }))

Default: {}

Outputs

The following outputs are exported:

Description: Primary Mongo connection string if exposed (preview shape). Null if not available yet.

Description: Collection of connection strings if service returns multiple. Null if not available.

Description: Resource ID of the MongoDB vCore cluster.

Description: Location of the MongoDB vCore cluster.

Description: Name of the MongoDB vCore cluster.

Description: Selected stable properties returned by the AzAPI provider. Volatile/server-computed fields (properties.backup.earliestRestoreTime and properties.privateEndpointConnections) are intentionally excluded via response_export_values to keep plans idempotent. Subject to change with API versions.

Description: A map of the private endpoints created.

Description: The resource ID of the MongoDB vCore cluster (required by AVM).

Modules

The following Modules are called:

Source: ./modules/firewall_rule

Version:

Source: ./modules/private_endpoint_connection

Version:

Source: ./modules/user

Version:

Data Collection

The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.

About

Terraform Azure Verified Resource Module for Cosmos DB for MongoDB (vCore)

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors