OpenMCF logoOpenMCF

Loading...

Azure Cosmos DB Account

Deploys an Azure Cosmos DB account supporting both SQL/NoSQL and MongoDB APIs, with configurable consistency levels, global distribution across multiple regions, automatic failover, throughput provisioning (fixed or autoscale), backup policies, VNet rules, and IP-based firewall. The component bundles the account with its databases and containers/collections as a single deployable unit.

What Gets Created

When you deploy an AzureCosmosdbAccount resource, OpenMCF provisions:

  • Cosmos DB Account -- a cosmosdb.Account resource in the specified region and resource group, configured with the chosen API kind, consistency policy, geo-locations, capabilities, and network access rules
  • SQL Databases -- a cosmosdb.SqlDatabase for each entry in sqlDatabases (when kind is GlobalDocumentDB), with optional shared throughput
  • SQL Containers -- a cosmosdb.SqlContainer for each container within a SQL database, configured with partition key, optional dedicated throughput, and TTL
  • MongoDB Databases -- a cosmosdb.MongoDatabase for each entry in mongoDatabases (when kind is MongoDB), with optional shared throughput
  • MongoDB Collections -- a cosmosdb.MongoCollection for each collection within a MongoDB database, configured with shard key, optional throughput, TTL, and indexes
  • Azure Tags -- resource metadata tags applied to the account for tracking and governance

Prerequisites

  • Azure credentials configured via environment variables or OpenMCF provider config
  • An Azure Resource Group where the account will be created (can reference an AzureResourceGroup resource)
  • A globally unique account name -- the name becomes the endpoint https://{name}.documents.azure.com:443/
  • Partition key design -- determine the partition key (SQL) or shard key (MongoDB) for each container/collection before deployment; this cannot be changed after creation

Quick Start

Create a file cosmosdb.yaml:

apiVersion: azure.openmcf.org/v1
kind: AzureCosmosdbAccount
metadata:
  name: my-cosmos
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: dev.AzureCosmosdbAccount.my-cosmos
spec:
  region: eastus
  resourceGroup: my-rg
  name: my-cosmos-db
  geoLocations:
    - location: eastus
      failoverPriority: 0
  sqlDatabases:
    - name: myapp
      containers:
        - name: items
          partitionKeyPaths:
            - /tenantId

Deploy:

openmcf apply -f cosmosdb.yaml

This creates a GlobalDocumentDB (SQL API) Cosmos DB account with Session consistency, a single myapp database, and an items container partitioned by /tenantId.

Configuration Reference

Required Fields

FieldTypeDescriptionValidation
regionstringAzure region for the account (e.g., eastus, westeurope).Required, minimum length 1
resourceGroupStringValueOrRefAzure Resource Group name. Can reference an AzureResourceGroup resource via valueFrom.Required
namestringGlobally unique account name. Becomes the endpoint https://{name}.documents.azure.com:443/. ForceNew.Required, 3-50 characters, pattern ^[-a-z0-9]{3,50}$
geoLocationslistGeographic regions for the account. At least one required. First entry with failoverPriority: 0 is the primary write region.Minimum 1 item
geoLocations[].locationstringAzure region name (e.g., eastus).Required
geoLocations[].failoverPriorityintFailover priority (0 = primary write region). Must be unique and contiguous.Required, >= 0

Optional Fields

FieldTypeDefaultDescription
kindstring"GlobalDocumentDB"API kind. GlobalDocumentDB (SQL/NoSQL API) or MongoDB (MongoDB wire protocol). ForceNew.
consistencyPolicy.consistencyLevelstring"Session"Default consistency. Values: Strong, BoundedStaleness, Session, ConsistentPrefix, Eventual.
consistencyPolicy.maxIntervalInSecondsint5Max staleness interval (BoundedStaleness only). Range: 5-86400.
consistencyPolicy.maxStalenessPrefixint100Max staleness prefix (BoundedStaleness only). Minimum: 10.
geoLocations[].zoneRedundantboolfalseEnable availability zone redundancy for this region.
capabilitiesstring[][]Account capabilities (e.g., EnableServerless, EnableAggregationPipeline). EnableMongo is auto-added for MongoDB kind.
freeTierEnabledboolfalseEnable free tier (1000 RU/s + 25 GB, one per subscription). ForceNew.
automaticFailoverEnabledboolfalseAuto-promote next region on failure. Recommended for multi-region.
multipleWriteLocationsEnabledboolfalseEnable active-active multi-region writes. Requires automaticFailoverEnabled.
publicNetworkAccessEnabledbooltrueAllow public internet access.
isVirtualNetworkFilterEnabledboolfalseRestrict access to allowed VNet subnets only.
virtualNetworkRuleslist[]Subnet rules. Each has subnetId (StringValueOrRef to AzureSubnet).
ipRangeFilterstring[][]Allowed CIDR ranges or IPs. Use 0.0.0.0 for all Azure services.
backup.typestring--Backup type: Periodic or Continuous.
backup.intervalInMinutesint240Periodic backup interval (60-1440).
backup.retentionInHoursint8Periodic backup retention (8-720).
backup.storageRedundancystring"Geo"Periodic backup storage: Geo, Local, Zone.
backup.tierstring--Continuous backup tier: Continuous7Days, Continuous30Days.
mongoServerVersionstring--MongoDB wire protocol version (MongoDB kind only). Values: 3.6, 4.0, 4.2, 5.0, 6.0, 7.0.
sqlDatabaseslist[]SQL API databases with containers (GlobalDocumentDB kind).
mongoDatabaseslist[]MongoDB API databases with collections (MongoDB kind).

SQL database/container fields:

FieldTypeDefaultDescription
sqlDatabases[].namestring--Database name (required, 1-255 chars)
sqlDatabases[].throughputint--Shared provisioned RU/s (min 400). Mutually exclusive with autoscaleMaxThroughput.
sqlDatabases[].autoscaleMaxThroughputint--Autoscale max RU/s (min 1000).
containers[].namestring--Container name (required)
containers[].partitionKeyPathsstring[]--Partition key paths (required, e.g., ["/tenantId"])
containers[].partitionKeyKindstring"Hash"Hash (single key) or MultiHash (hierarchical).
containers[].throughputint--Dedicated RU/s (min 400).
containers[].autoscaleMaxThroughputint--Autoscale max RU/s (min 1000).
containers[].defaultTtlint--Document TTL in seconds (-1 = enabled without default).

MongoDB database/collection fields:

FieldTypeDefaultDescription
mongoDatabases[].namestring--Database name (required)
mongoDatabases[].throughputint--Shared provisioned RU/s (min 400).
mongoDatabases[].autoscaleMaxThroughputint--Autoscale max RU/s (min 1000).
collections[].namestring--Collection name (required)
collections[].shardKeystring--Shard key field (required, e.g., tenantId)
collections[].throughputint--Dedicated RU/s (min 400).
collections[].defaultTtlSecondsint--Document TTL in seconds.
collections[].indexeslist[]Indexes with keys (string[]) and optional unique (bool).

Examples

SQL API with Autoscale

A SQL API account with autoscale throughput and multiple containers:

apiVersion: azure.openmcf.org/v1
kind: AzureCosmosdbAccount
metadata:
  name: app-cosmos
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: prod.AzureCosmosdbAccount.app-cosmos
spec:
  region: eastus
  resourceGroup: prod-rg
  name: app-cosmos-db
  automaticFailoverEnabled: true
  backup:
    type: Continuous
    tier: Continuous7Days
  geoLocations:
    - location: eastus
      failoverPriority: 0
      zoneRedundant: true
  sqlDatabases:
    - name: appdata
      autoscaleMaxThroughput: 4000
      containers:
        - name: users
          partitionKeyPaths:
            - /tenantId
          defaultTtl: -1
        - name: sessions
          partitionKeyPaths:
            - /userId
          defaultTtl: 86400

MongoDB API Account

A MongoDB account with MongoDB 7.0 wire protocol, a sharded collection, and indexes:

apiVersion: azure.openmcf.org/v1
kind: AzureCosmosdbAccount
metadata:
  name: mongo-cosmos
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: prod.AzureCosmosdbAccount.mongo-cosmos
spec:
  region: westeurope
  resourceGroup: prod-rg
  name: mongo-cosmos-db
  kind: MongoDB
  mongoServerVersion: "7.0"
  automaticFailoverEnabled: true
  geoLocations:
    - location: westeurope
      failoverPriority: 0
  mongoDatabases:
    - name: catalog
      autoscaleMaxThroughput: 4000
      collections:
        - name: products
          shardKey: categoryId
          indexes:
            - keys:
                - name
              unique: false
            - keys:
                - sku
              unique: true
        - name: reviews
          shardKey: productId
          defaultTtlSeconds: 7776000

Multi-Region with Strong Consistency

A globally distributed SQL API account with BoundedStaleness consistency, zone-redundant regions, and continuous backup:

apiVersion: azure.openmcf.org/v1
kind: AzureCosmosdbAccount
metadata:
  name: global-cosmos
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: prod.AzureCosmosdbAccount.global-cosmos
spec:
  region: eastus
  resourceGroup: prod-rg
  name: global-cosmos-db
  automaticFailoverEnabled: true
  consistencyPolicy:
    consistencyLevel: BoundedStaleness
    maxIntervalInSeconds: 300
    maxStalenessPrefix: 100000
  backup:
    type: Continuous
    tier: Continuous30Days
  geoLocations:
    - location: eastus
      failoverPriority: 0
      zoneRedundant: true
    - location: westeurope
      failoverPriority: 1
      zoneRedundant: true
    - location: southeastasia
      failoverPriority: 2
  sqlDatabases:
    - name: orders
      containers:
        - name: transactions
          partitionKeyPaths:
            - /regionId
            - /customerId
          partitionKeyKind: MultiHash
          autoscaleMaxThroughput: 10000

Using Foreign Key References

Reference OpenMCF-managed resources for the resource group and VNet rules:

apiVersion: azure.openmcf.org/v1
kind: AzureCosmosdbAccount
metadata:
  name: ref-cosmos
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: prod.AzureCosmosdbAccount.ref-cosmos
spec:
  region: eastus
  resourceGroup:
    valueFrom:
      kind: AzureResourceGroup
      name: my-rg
      field: status.outputs.resource_group_name
  name: ref-cosmos-db
  isVirtualNetworkFilterEnabled: true
  virtualNetworkRules:
    - subnetId:
        valueFrom:
          kind: AzureSubnet
          name: app-subnet
          field: status.outputs.subnet_id
  geoLocations:
    - location: eastus
      failoverPriority: 0
  sqlDatabases:
    - name: mydb
      containers:
        - name: items
          partitionKeyPaths:
            - /id

Stack Outputs

After deployment, the following outputs are available in status.outputs:

OutputTypeDescription
account_idstringAzure Resource Manager ID of the Cosmos DB account. Referenced by AzurePrivateEndpoint for private connectivity.
account_namestringName of the Cosmos DB account
endpointstringDocument endpoint URI (e.g., https://{name}.documents.azure.com:443/)
primary_keystringPrimary access key for authentication (sensitive)
primary_connection_stringstringSQL API connection string (sensitive). Always populated.
primary_mongodb_connection_stringstringMongoDB API connection string (sensitive). Only populated when kind is MongoDB.
database_idsmap<string, string>Map of database names to their Azure Resource Manager IDs

Related Components

  • AzureResourceGroup -- provides the resource group for account placement
  • AzureSubnet -- provides subnets for VNet access rules
  • AzurePrivateEndpoint -- establishes private connectivity to the account
  • AzureKeyVault -- stores the primary key or connection strings as secrets

Next article

Azure DNS Record

Azure DNS Record Deploys an individual DNS record (A, AAAA, CNAME, MX, TXT, SRV, NS, PTR, or CAA) within an existing Azure DNS Zone. The component supports all standard record types with configurable TTL, multiple record values for round-robin behavior, and MX priority for mail exchange records. What Gets Created When you deploy an AzureDnsRecord resource, OpenMCF provisions: DNS Record -- one of the following Pulumi Azure DNS resources based on the specified type: dns.ARecord, dns.AaaaRecord,...
Read next article
Presets
3 ready-to-deploy configurationsView presets →