This repository contains Terraform scripts to deploy and manage Azure Cosmos DB with support for SQL API and Mongo API. It includes examples for configuring throughput, partition keys, global distribution, and RBAC.
📌 Features
- Deploys Azure Resource Group, Cosmos DB Account, Database, and Container/Collection-
- Supports Provisioned and Serverless throughput modes
- Configurable partition key strategy
- Enables multi-region replication with read/write regions
- Option to enable Continuous Backup (RPO ~15 minutes)
- RBAC integration with Azure AD roles
Tags & naming conventions for governance
📂 Project Structure
├── main.tf # Core Cosmos DB resources
├── variables.tf # Input variables (region, RU, partition key, etc.)
├── outputs.tf # Key outputs (endpoint, connection strings)
├── provider.tf # Azure provider setup
└── README.md # Project documentation
🚀 Usage
Clone this repo:
git clone https://github.com/ssk845/cosmosdbtf.git cd terraform-cosmosdb
#Initialize Terraform terraform init
#Apply the configuration
terraform apply -var="cosmos_account_name=mycosmosacct" \
-var="resource_group_name=my-rg" \
-var="location=eastus" \
-var="throughput=400" \
-var="api=MongoDB"
Confirm deployment in the Azure Portal
⚙️ Example Variables
variable "cosmos_account_name" { default = "mycosmosacct" }
variable "resource_group_name" { default = "my-rg" }
variable "location" { default = "eastus" }
variable "api" { default = "SQL" } # Options: SQL, MongoDB
variable "throughput" { default = 400 }
variable "enable_multi_region" { default = true }
📊 Outputs
-
Cosmos DB Endpoint
-
Primary Key & Connection Strings
-
Regions Configured
🔐 Security Best Practices
-
Store secrets in Azure Key Vault, not in Terraform variables
-
Enable Private Endpoints for production workloads
-
Use RBAC roles for granular access to Cosmos DB resources
📝 Next Steps
-
Add Change Feed Processor demo with Azure Functions
-
CI/CD integration using GitHub Actions
-
Monitoring with Azure Monitor + Kusto Queries