### Terraform Module Source from GitHub Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/AGENTS.md This is an example of a Terraform module source URL pointing to a GitHub repository for an AVM module. ```terraform source = "https://github.com/Azure/terraform-azurerm-avm-{type}-{service}-{resource}" ``` -------------------------------- ### Terraform Module Source from Registry Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/AGENTS.md This is an example of a Terraform module source URL pointing to the Terraform Registry for an AVM module. ```terraform source = "https://registry.terraform.io/modules/Azure/{module}/azurerm/latest" ``` -------------------------------- ### Configure MongoDB Database and Collections Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/README.md Example configuration for a MongoDB database with collections, specifying throughput, default TTL, shard key, and indexing. Ensure throughput is set upon creation for databases and collections to avoid manual destroy-apply. ```hcl database_collection = { name = "database_mongoDb_collections" throughput = 400 collections = { "collection" = { name = "MongoDBcollection" default_ttl_seconds = "3600" shard_key = "_id" throughput = 400 index = { keys = ["_id"] unique = true } } } } ``` -------------------------------- ### Replacing Local Source with Registry Source in Terraform Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/AGENTS.md When copying examples from module documentation, replace the local source path (e.g., "../../") with the appropriate Terraform Registry source. ```terraform source = "../../" ``` -------------------------------- ### Assign Tags to Cosmos DB Resource Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/README.md Example of how to define tags for a Cosmos DB resource. These tags will propagate to child resources unless overridden. ```hcl tags = { environment = "testing" } ``` -------------------------------- ### Configure Diagnostic Settings for Cosmos DB Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/README.md Example of how to configure diagnostic settings for a Cosmos DB account. This includes specifying log and metric categories, as well as destinations like Log Analytics, Storage Accounts, or Event Hubs. Ensure resource IDs are correctly formatted. ```hcl diagnostic_settings = { diagnostic1 = { event_hub_name = "hub-name" log_analytics_destination_type = "Dedicated" name = "diagnostics" event_hub_authorization_rule_resource_id = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.EventHub/namespaces/{eventHubNamespaceName}/authorizationRules/{authorizationRuleName}" #log_categories = ["DataPlaneRequests", "MongoRequests", "CassandraRequests", "GremlinRequests", "QueryRuntimeStatistics", "PartitionKeyStatistics", "PartitionKeyRUConsumption", "ControlPlaneRequests", "TableApiRequests"] metric_categories = ["AllMetrics"] log_groups = ["allLogs", "audit"] workspace_resource_id = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}" storage_account_resource_id = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}" } } ``` -------------------------------- ### Terraform Configuration for Gremlin API Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/examples/gremlin/README.md This configuration sets up the necessary Terraform providers and modules, defines resource groups, and configures a Cosmos DB account with the Gremlin API enabled. It includes examples of creating databases and graphs with various settings like autoscale, fixed throughput, TTL, analytical storage, partition keys, index policies, conflict resolution, and unique keys. ```hcl terraform { required_version = ">= 1.9, < 2.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 4.0" } random = { source = "hashicorp/random" version = "~> 3.6" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } locals { prefix = "gremlin" } module "regions" { source = "Azure/regions/azurerm" version = "0.3.0" recommended_regions_only = true } resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } module "naming" { source = "Azure/naming/azurerm" version = "0.3.0" } resource "azurerm_resource_group" "example" { location = "spaincentral" name = "${module.naming.resource_group.name_unique}-${local.prefix}" } module "cosmos" { source = "../../" location = azurerm_resource_group.example.location name = "${module.naming.cosmosdb_account.name_unique}-${local.prefix}" resource_group_name = azurerm_resource_group.example.name analytical_storage_enabled = true capabilities = [{ name = "EnableGremlin" }] gremlin_databases = { empty_database = { name = "empty_database" throughput = 400 } database_autoscale_throughput = { name = "database_autoscale_throughput" autoscale_settings = { max_throughput = 4000 } } database_with_fixed_throughput = { name = "database_with_fixed_throughput" throughput = 400 } database_with_graphs = { name = "database_with_graphs" throughput = 400 graphs = { "graph_fixed_throughput" = { name = "graph_fixed_throughput" partition_key_path = "/partitionKey" throughput = 400 } "graph_autoscale" = { name = "graph_autoscale" partition_key_path = "/partitionKey" autoscale_settings = { max_throughput = 4000 } } "graph_with_ttl" = { name = "graph_with_ttl" partition_key_path = "/partitionKey" default_ttl = 3600 } "graph_with_analytical_storage" = { name = "graph_with_analytical_storage" partition_key_path = "/partitionKey" analytical_storage_ttl = -1 } "graph_with_partition_key_v2" = { name = "graph_with_partition_key_v2" partition_key_path = "/largePartitionKey" partition_key_version = "2" } "graph_with_index_policy" = { name = "graph_with_index_policy" partition_key_path = "/partitionKey" index_policy = { automatic = true indexing_mode = "consistent" included_paths = ["/", "/included/*"] excluded_paths = ["/excluded/*"] composite_index = [ { index = [ { path = "/name" order = "Ascending" }, { path = "/age" order = "Descending" } ] } ] spatial_index = [ { path = "/location/*" } ] } } "graph_with_conflict_resolution" = { name = "graph_with_conflict_resolution" partition_key_path = "/partitionKey" conflict_resolution_policy = { mode = "LastWriterWins" conflict_resolution_path = "/_ts" } } "graph_with_unique_key" = { name = "graph_with_unique_key" partition_key_path = "/partitionKey" unique_key = { paths = ["/uniqueField"] } } } } } } ``` -------------------------------- ### Terraform Configuration for Max Example Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/examples/max-account/README.md This Terraform configuration deploys an Azure DocumentDB Database Account with extensive settings. It includes provider requirements, resource group, log analytics workspace, and the main cosmosdb module with various options enabled. ```hcl terraform { required_version = ">= 1.9, < 2.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 4.0" } random = { source = "hashicorp/random" version = "~> 3.6" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } data "azurerm_client_config" "current" {} locals { prefix = "max" } module "regions" { source = "Azure/regions/azurerm" version = "0.3.0" recommended_regions_only = true } resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } module "naming" { source = "Azure/naming/azurerm" version = "0.3.0" } resource "azurerm_resource_group" "example" { location = "northeurope" name = "${module.naming.resource_group.name_unique}-${local.prefix}" } resource "azurerm_log_analytics_workspace" "example" { location = azurerm_resource_group.example.location name = module.naming.log_analytics_workspace.name_unique resource_group_name = azurerm_resource_group.example.name retention_in_days = 30 sku = "PerGB2018" } module "cosmos" { source = "../../" location = azurerm_resource_group.example.location name = "${module.naming.cosmosdb_account.name_unique}-${local.prefix}" resource_group_name = azurerm_resource_group.example.name access_key_metadata_writes_enabled = true analytical_storage_config = { schema_type = "WellDefined" } analytical_storage_enabled = true automatic_failover_enabled = false backup = { retention_in_hours = 8 interval_in_minutes = 1440 storage_redundancy = "Geo" type = "Periodic" } capacity = { total_throughput_limit = 10000 } consistency_policy = { consistency_level = "Session" } cors_rule = { max_age_in_seconds = 3600 allowed_origins = ["*"] exposed_headers = ["*"] allowed_headers = ["Authorization"] allowed_methods = ["GET", "POST", "PUT"] } diagnostic_settings = { cosmosdb = { name = "diag" workspace_resource_id = azurerm_log_analytics_workspace.example.id metric_categories = ["SLI", "Requests"] } } enable_telemetry = false geo_locations = [ { failover_priority = 0 zone_redundant = false location = azurerm_resource_group.example.location } ] local_authentication_disabled = false lock = { kind = "CanNotDelete" name = "Testing name CanNotDelete" } multiple_write_locations_enabled = true partition_merge_enabled = false public_network_access_enabled = false role_assignments = { key = { skip_service_principal_aad_check = false role_definition_id_or_name = "Contributor" description = "This is a test role assignment" principal_id = data.azurerm_client_config.current.object_id } } tags = { environment = "testing" department = "engineering" } } ``` -------------------------------- ### Using Pessimistic Constraints for Providers in Terraform Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/AGENTS.md For providers, it is recommended to use pessimistic version constraints to ensure compatibility and avoid unexpected upgrades. This example shows the syntax. ```terraform version = "~> 1.0" ``` -------------------------------- ### Configure Gremlin Databases and Graphs in Terraform Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/README.md Example of defining Gremlin databases and their associated graphs within an Azure Cosmos DB account using Terraform. Includes settings for name, throughput, partition key, TTL, and autoscale. ```hcl gremlin_databases = { "database_name" = { name = "database_gremlin" throughput = 400 graphs = { "graph" = { name = "graph" partition_key_path = "/myPartitionKey" partition_key_version = "1" throughput = 400 default_ttl = 3600 analytical_storage_ttl = -1 autoscale_settings = { max_throughput = 1000 } } } } } ``` -------------------------------- ### Pinning AVM Module Version in Terraform Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/AGENTS.md When using AVM modules, it's crucial to pin to a specific version for predictable deployments. This example shows how to specify the version. ```terraform version = "1.2.3" ``` -------------------------------- ### Terraform Configuration for Private Endpoint Example Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/examples/private-endpoints-unmanaged-dns-records/README.md This HCL code block defines the Terraform configuration, including required providers and resources for deploying Azure Cosmos DB with private endpoints. It sets up networking, private DNS zones, and the Cosmos DB module itself, with public network access disabled and DNS zone management turned off. ```hcl terraform { required_version = ">= 1.9, < 2.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 4.0" } random = { source = "hashicorp/random" version = "~> 3.6" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } data "azurerm_client_config" "current" {} locals { prefix = "pe-umg" } module "regions" { source = "Azure/regions/azurerm" version = "0.3.0" recommended_regions_only = true } resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } module "naming" { source = "Azure/naming/azurerm" version = "0.3.0" } resource "azurerm_resource_group" "example" { location = "spaincentral" name = "${module.naming.resource_group.name_unique}-${local.prefix}" } resource "azurerm_virtual_network" "example" { location = azurerm_resource_group.example.location name = "${module.naming.virtual_network.name_unique}-${local.prefix}" resource_group_name = azurerm_resource_group.example.name address_space = ["10.0.0.0/16"] } resource "azurerm_subnet" "example" { address_prefixes = ["10.0.0.0/24"] name = module.naming.subnet.name_unique resource_group_name = azurerm_resource_group.example.name virtual_network_name = azurerm_virtual_network.example.name } resource "azurerm_private_dns_zone" "example" { name = "privatelink.documents.azure.com" resource_group_name = azurerm_resource_group.example.name } resource "azurerm_private_dns_zone_virtual_network_link" "private_links" { name = "vnet-link" private_dns_zone_name = azurerm_private_dns_zone.example.name resource_group_name = azurerm_resource_group.example.name virtual_network_id = azurerm_virtual_network.example.id } resource "azurerm_application_security_group" "example" { location = azurerm_resource_group.example.location name = "tf-appsecuritygroup-${local.prefix}" resource_group_name = azurerm_resource_group.example.name } module "cosmos" { source = "../../" location = azurerm_resource_group.example.location name = "${module.naming.cosmosdb_account.name_unique}-${local.prefix}" resource_group_name = azurerm_resource_group.example.name private_endpoints = { max = { name = "max" subresource_name = "SQL" network_interface_name = "max_nic1" private_dns_zone_group_name = "max_dns_group" private_service_connection_name = "max_connection" subnet_resource_id = azurerm_subnet.example.id private_dns_zone_resource_ids = [azurerm_private_dns_zone.example.id] role_assignments = { key = { role_definition_id_or_name = "Contributor" description = "This is a test role assignment" principal_id = data.azurerm_client_config.current.object_id } } lock = { kind = "CanNotDelete" name = "Testing name CanNotDelete" } tags = { environment = "testing" department = "engineering" } application_security_group_associations = { asg1 = azurerm_application_security_group.example.id } } noDnsGroup = { name = "no-dns-group" subresource_name = "SQL" subnet_resource_id = azurerm_subnet.example.id } withDnsGroup = { name = "with-dns-group" subresource_name = "SQL" subnet_resource_id = azurerm_subnet.example.id private_dns_zone_resource_ids = [azurerm_private_dns_zone.example.id] } } private_endpoints_manage_dns_zone_group = false public_network_access_enabled = false } ``` -------------------------------- ### Configure Periodic Backup Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/README.md Set up periodic backups with specified storage redundancy, interval, and retention period. Ensure the interval is between 60 and 1440 minutes, and retention is between 8 and 720 hours. ```hcl backup = { type = "Periodic" storage_redundancy = "Geo" interval_in_minutes = 240 retention_in_hours = 8 } ``` -------------------------------- ### Configure Cosmos DB Account with Containers Source: https://github.com/azure/terraform-azurerm-avm-res-documentdb-databaseaccount/blob/main/examples/sql/README.md Defines a Cosmos DB SQL API account with multiple containers, each having specific configurations like partition keys and indexing policies. ```terraform resource "azurerm_resource_group" "rg" { name = "rg-cosmosdb-example" location = "West Europe" } resource "azurerm_cosmosdb_account" "example" { name = "cosmosdb-account-example" location = azurermerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name offer_type = "Standard" kind = "GlobalDocumentDB" capabilities { name = "EnableServerless" } capabilities { name = "EnableFreeTier" } analytical_storage_mode = "Default" identity { type = "SystemAssigned" } backup_policy { type = "Periodic" periodic_mode_properties { backup_interval_in_minutes = 60 backup_retention_in_hours = 8 } } consistency_policy { consistency_level = "BoundedStaleness" max_staleness_prefix = 10 max_interval_in_seconds = 300 } geo_location { location = "West US" failover_priority = 0 } geo_location { location = "East US" failover_priority = 1 } tags = { environment = "dev" } } resource "azurerm_cosmosdb_sql_container" "example" { name = "container_with_functions" database_name = azurerm_cosmosdb_account.example.name resource_group_name = azurerm_resource_group.rg.name partition_key_path = "/id" throughput = 400 indexing_policy = jsonencode({ indexingMode = "consistent" automatic = true includedPaths = [ { path = "/path1/?" }, { path = "/path2" } ] excludedPaths = [ { path = "/path3/_all" } ] }) conflict_resolution_policy { mode = "LastWriterWins" path = "/_ts" } unique_key { paths = ["/uniqueKey1", "/uniqueKey2"] } } resource "azurerm_cosmosdb_sql_container" "container_with_stored_procedures" { name = "container_with_stored_procedures" database_name = azurerm_cosmosdb_account.example.name resource_group_name = azurerm_resource_group.rg.name partition_key_paths = ["/id"] stored_procedures = { empty = { name = "empty" body = <