Surya Pratap
Surya Pratap

Reputation: 153

How to define Subnet in Terraform for Azure CosmosDB

I'm trying to create Azure CosmosDB with Terraform Template. I'm able to create CosmosDB successfully but unable to add existing subnet details in terraform template. I'm trying to add subnet as below, but not working. Can someone please help me how can I define subnet in terraform. Any specific version I need to use.

is_virtual_network_filter_enabled = true

virtual_network_rule {
    id = <MY SUBNET ID>
  }

Upvotes: 0

Views: 3061

Answers (2)

Nancy Xiong
Nancy Xiong

Reputation: 28284

From the error message in your comment,

Can not parse "virtual_network_rule.0.id" as a resource id: Cannot parse Azure ID: parse "APSG-APP1": invalid URI for request on main.tf line 16, in resource "azurerm_cosmosdb_account" "db": 16: resource "azurerm_cosmosdb_account" "db" {

I can reproduce this issue. It might be a problem with your subnet ID. Please check the subnet id you have passed to the terraform code. The id should be a resource id of the subnet instead of a subnet name.

  virtual_network_rule  {
    id                = "/subscriptions/xxxx/resourceGroups/xxxrg/providers/Microsoft.Network/virtualNetworks/xxxxvnet/subnets/xxxSubnet"
   # ignore_missing_vnet_service_endpoint = true
  }

If you're trying to add an existing subnet, the subnet should enable the service endpoint for Microsoft.AzureCosmosDB.

service_endpoints    = ["Microsoft.AzureCosmosDB"] 

Or you can add ignore_missing_vnet_service_endpoint to ignore missing service endpoint.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222722

You can reuse the below template,

## Azure resource provider ##
provider "azurerm" {
  version = ">= 0.12"
  features {}
}

resource "azurerm_cosmosdb_account" "main" {
  name                = var.cosmosdb
  location            = var.location
  resource_group_name = var.resource_group_name  
  offer_type          = "Standard"
  kind                = "MongoDB"
  is_virtual_network_filter_enabled = "true"
  ip_range_filter     = var.ip_range_filter

  enable_automatic_failover = false

  consistency_policy {
    consistency_level       = "Session"
    max_interval_in_seconds = 5
    max_staleness_prefix    = 100
  }

  geo_location {
    location          = var.location
    failover_priority = 0
  }

  virtual_network_rule  {
    id                = var.vnet_subnet_id
    ignore_missing_vnet_service_endpoint = true
  }
  
}

#resource "azurerm_cosmosdb_mongo_database" "db" {
#  name                = azurerm_cosmosdb_account.main.name
#  resource_group_name = var.resource_group_name
#  account_name        = azurerm_cosmosdb_account.main.name
#}

Upvotes: 1

Related Questions