Customer-Managed Keys
Complete the full lesson to earn 25 points — 50 with Pro
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks, the wait, and see fewer ads — read each lesson on a single page with Pro
Lesson: Implementing Customer-Managed Keys in Azure Cosmos DB
Introduction: Taking Control of Your Data Encryption
When you store data in the cloud, security is the primary concern for any organization. By default, Azure Cosmos DB provides encryption at rest, which means your data is automatically encrypted using service-managed keys. For many applications, this is sufficient and provides a high level of security without requiring any additional management effort from your team. However, in highly regulated industries—such as finance, healthcare, or government—you may be required to maintain more granular control over the encryption process.
This is where Customer-Managed Keys (CMK), often referred to as "Bring Your Own Key" (BYOK), come into play. CMK allows you to use your own encryption keys to protect the data stored in your Cosmos DB accounts. Instead of trusting the cloud provider to manage the root of trust, you maintain control over the key lifecycle, including rotation, access policies, and revocation. Understanding how to implement this is a critical skill for any cloud architect or administrator tasked with managing enterprise-level security for distributed databases.
In this lesson, we will explore the architecture of CMK in Azure, the prerequisites for setting it up, the technical implementation steps, and the operational best practices required to ensure your data remains secure and accessible. By the end of this module, you will be able to configure an Azure Key Vault, link it to your Cosmos DB account, and manage the keys effectively.
Understanding the Encryption Architecture
To understand how Customer-Managed Keys work, we must first look at the relationship between Azure Cosmos DB and Azure Key Vault. When you opt for CMK, you are essentially asking Cosmos DB to wrap its internal data encryption keys with a key that you own and store within a secure hardware security module (HSM) or a software-based vault.
The Role of Azure Key Vault
Azure Key Vault acts as the centralized repository for your cryptographic keys. When you enable CMK for Cosmos DB, the database service is granted permission to access your specific key in the vault. Every time the database needs to read or write data, it uses the key stored in the vault to decrypt or encrypt the data encryption keys used for your documents.
The Encryption Flow
The process follows a specific sequence to ensure security:
- Key Creation: You create a key in Azure Key Vault. This key can be software-protected or hardware-protected (using an HSM).
- Identity Assignment: You assign a Managed Identity (either System-Assigned or User-Assigned) to your Cosmos DB account.
- Access Policy: You grant the Managed Identity permission to perform specific operations on the Key Vault, such as
wrapKey,unwrapKey,get,list, andsign. - Configuration: You update the Cosmos DB account settings to point to the Key Vault URI and the specific key version (or leave it to automatically update to the latest version).
Callout: Service-Managed vs. Customer-Managed Keys Service-managed keys are the default state for Azure services. Microsoft manages the rotation, storage, and protection of these keys, providing a "hands-off" experience. Customer-managed keys shift this responsibility to you. While this provides greater control and compliance, it also introduces a significant risk: if you accidentally delete or disable your key in the vault, your data in Cosmos DB becomes inaccessible and potentially unrecoverable.
Prerequisites for Implementation
Before you attempt to configure CMK, you must ensure your environment is correctly prepared. Skipping these prerequisites will lead to deployment failures or, worse, operational outages.
1. Azure Key Vault Setup
You must have an Azure Key Vault instance created in the same region as your Cosmos DB account or a compatible region. Ensure that "Soft Delete" and "Purge Protection" are enabled on the vault. These features are mandatory for CMK because they prevent the accidental permanent deletion of keys, which would cause an unrecoverable data loss event for your database.
2. Managed Identity
Cosmos DB requires an identity to authenticate with the Key Vault. You should enable a system-assigned managed identity on the Cosmos DB account. This identity will be the principal that requests access to the key.
3. Key Permissions
The identity needs specific access permissions. Do not use the "Owner" role for the database identity. Follow the principle of least privilege by only granting the minimum permissions required for the service to function: get, list, wrapKey, and unwrapKey.
Step-by-Step Implementation Guide
Implementing CMK is not a one-click process; it requires careful coordination between your database configuration and your security vault settings.
Step 1: Create and Configure the Key Vault
Start by creating a Key Vault if you do not already have one. Use the Azure CLI to ensure the settings are correct.
# Create a Resource Group
az group create --name myResourceGroup --location eastus
# Create the Key Vault with Purge Protection enabled
az keyvault create --name myKeyVault --resource-group myResourceGroup --location eastus --enable-purge-protection true
Step 2: Create the Encryption Key
Inside the vault, generate the key that will protect your database.
# Create a key
az keyvault key create --vault-name myKeyVault --name myCosmosKey --protection software
Step 3: Enable Managed Identity on Cosmos DB
You need to enable the identity so that the database can identify itself to the Key Vault.
# Enable system-assigned identity
az cosmosdb update --name myCosmosAccount --resource-group myResourceGroup --assign-identity
Step 4: Grant Access to the Key Vault
Now, retrieve the principal ID of the Cosmos DB identity and grant it access to the Key Vault.
# Get the principal ID
principal_id=$(az cosmosdb show --name myCosmosAccount --resource-group myResourceGroup --query identity.principalId --output tsv)
# Grant access
az keyvault set-policy --name myKeyVault --object-id $principal_id --key-permissions get list wrapKey unwrapKey
Step 5: Link the Key to Cosmos DB
Finally, update the Cosmos DB account to use the key. You will need the Key Vault URI and the key ID.
# Get the key identifier
key_id=$(az keyvault key show --vault-name myKeyVault --name myCosmosKey --query key.kid --output tsv)
# Update Cosmos DB to use the CMK
az cosmosdb update --name myCosmosAccount --resource-group myResourceGroup --key-uri $key_id
Best Practices for Managing Customer-Managed Keys
Managing your own keys is a serious operational responsibility. If you lose access to the keys, you lose access to the data. Follow these industry-standard practices to minimize risk.
Use Key Rotation
Do not use the same key indefinitely. Security standards often require regular key rotation. Azure Key Vault makes this easy. You can configure automatic rotation policies or perform manual rotation. When you rotate a key, Cosmos DB will automatically start using the new version for new data, while retaining the ability to decrypt older data using previous key versions.
Monitor Key Vault Access
Use Azure Monitor and Log Analytics to track all access requests to your Key Vault. You should set up alerts for unauthorized access attempts. If someone attempts to access the key and fails, it might be an indicator of a configuration error or a malicious attempt to compromise your security.
Implement Redundancy
Ensure your Key Vault is highly available. Use regional replication if your environment supports it. If your Key Vault goes down, your Cosmos DB account will be unable to process requests because it cannot fetch the encryption keys.
Warning: The Risk of Key Deletion If you delete the key in Azure Key Vault, you will trigger a catastrophic failure. Even with "Soft Delete" enabled, if you purge the key, your data in Cosmos DB becomes encrypted with a key that no longer exists. There is no "backdoor" for Microsoft to recover this data for you. Always back up your key vault configuration and ensure that administrative access to the vault is strictly controlled.
Comparison of Key Management Options
| Feature | Service-Managed Keys | Customer-Managed Keys |
|---|---|---|
| Control | Microsoft managed | Customer managed |
| Operational Effort | Low (Automatic) | High (Manual/Policy-based) |
| Compliance | Standard | High (FIPS 140-2/3 compliance) |
| Key Rotation | Handled by Azure | Manual or Automated via Key Vault |
| Data Recovery | Managed by Microsoft | Customer responsibility |
| Cost | Included in DB cost | Additional Key Vault costs |
Common Pitfalls and Troubleshooting
Troubleshooting Access Issues
If your application starts receiving errors like Forbidden or KeyVaultAccessDenied when querying your database, the first thing to check is the Managed Identity permissions. Often, administrators update the Key Vault but forget to re-verify the Access Policy. Ensure the object-id assigned to the policy matches the principal-id of the Cosmos DB account.
Network Isolation
If you are using Private Endpoints for your Cosmos DB, ensure that your Key Vault is also reachable via the same network or allow-listed in the Key Vault firewall. A common mistake is restricting the Key Vault so heavily that the Cosmos DB service cannot reach it, causing the database to go into a locked state.
Key Versioning
When you update the key URI in Cosmos DB, be careful with the URI format. If you include the specific version in the URI, the database will only ever use that version. If you omit the version, the database will always use the "latest" version of the key. For most production environments, using the "latest" version (by omitting the version string) is preferred for easier rotation, but ensure you understand the implications of automatic updates.
Operational Scenarios
Scenario A: Rotating the Key
To rotate a key, you create a new version of the key in the same Key Vault. Once the new version is created, you update the Cosmos DB account to point to the new key URI. The database will then re-wrap its internal keys using the new version. This process is transparent to your application layer; your developers do not need to change any code, as the encryption and decryption happen at the storage layer.
Scenario B: Revoking Access
In the event of a security breach, you may need to immediately stop access to your data. By removing the permissions from the Key Vault or disabling the key, you effectively "lock" the database. Any read or write operation will fail immediately. This is the ultimate "kill switch" for your data. Once you have remediated the threat, you can restore the permissions to bring the database back online.
Detailed Technical Considerations
Performance Impact
One question often raised is whether CMK adds latency to database operations. Because the encryption keys are cached by the Cosmos DB service, the latency impact is minimal. The database does not perform a network round-trip to the Key Vault for every single document read or write. Instead, it fetches the root key, caches it securely, and uses it to perform cryptographic operations locally within the service's memory space.
Regional Considerations
When using CMK with multi-region Cosmos DB accounts, you must ensure that the Key Vault is replicated or accessible across those regions. If a region fails over, the new primary region must have the same level of access to the Key Vault to decrypt the data. Failure to align your Key Vault geography with your database geography can lead to unexpected downtime during regional failover events.
Auditing and Compliance
For organizations that must pass audits (like SOC2, HIPAA, or PCI-DSS), CMK provides the necessary audit trail. Azure Key Vault logs every single operation performed with your key. You can export these logs to a SIEM (Security Information and Event Management) system like Azure Sentinel. This allows you to prove to auditors exactly who, what, and when a key was accessed, satisfying the stringent requirements of most compliance frameworks.
Advanced Configuration: Using HSM-Backed Keys
For the highest level of security, you might choose to store your keys in a Hardware Security Module (HSM). Azure Key Vault Managed HSM provides a single-tenant, fully managed, highly available, standards-compliant cloud service for managing your cryptographic keys.
To use an HSM-backed key:
- Provision a Managed HSM instance.
- Create an HSM-backed key (which is FIPS 140-2 Level 3 compliant).
- Follow the same procedure as the standard Key Vault, granting the Cosmos DB managed identity access to the Managed HSM.
This approach is significantly more expensive than standard software-based Key Vault keys, but it is often a requirement for government projects or highly sensitive financial data.
Key Takeaways
- Control and Compliance: Customer-Managed Keys provide you with complete ownership of your encryption lifecycle, which is essential for meeting strict regulatory and compliance requirements.
- The Vault is Critical: Your Azure Key Vault is the single point of failure for your data. Its availability, backup, and security are directly tied to the availability and security of your Cosmos DB data.
- Managed Identity is the Bridge: Using Managed Identities for authentication between services is the standard, secure way to connect Cosmos DB to Key Vault, eliminating the need for hardcoded credentials.
- Operations Matter: You must have a robust plan for key rotation and emergency access recovery. Automating key rotation reduces human error and keeps your security posture fresh.
- Least Privilege: Always apply the principle of least privilege to your Key Vault access policies. Only grant the specific permissions (
get,list,wrap,unwrap) necessary for the database to function. - No "Backdoor" for Recovery: Understand clearly that if you delete or purge your keys, Microsoft cannot recover your data. This is a deliberate design feature to ensure your data remains under your absolute control.
- Monitor Everything: Use logging and alerting to monitor access to your keys. Any unexpected access to your encryption key is a high-priority security event that should be investigated immediately.
Implementing Customer-Managed Keys is a significant step forward in securing your Azure Cosmos DB environment. While it introduces complexity, the trade-off is a level of transparency and control that is simply not possible with service-managed solutions. By following the steps and best practices outlined in this lesson, you can confidently manage your encryption keys and protect your most valuable data assets.
Reach the last section to complete this lesson and earn points — you're on section 1 of 9.
- Introduction to Cosmos DB Data Modeling
- Introduction to Cosmos DB Data Modeling Quiz5q
- Multiple Entity Types in Same Container
- Multiple Entity Types in Same Container Quiz5q
- Storing Related Entities in Same Document
- Storing Related Entities in Same Document Quiz5q
- Denormalizing Data Across Documents
- Denormalizing Data Across Documents Quiz5q
- Referencing Between Documents
- Referencing Between Documents Quiz5q
- Partition Keys and Document IDs
- Partition Keys and Document IDs Quiz5q
- Time to Live (TTL) Configuration
- Time to Live (TTL) Configuration Quiz5q
- Document Versioning Strategies
- Document Versioning Strategies Quiz5q
- Schema Versioning Patterns
- Schema Versioning Patterns Quiz5q
- Choosing Partition Strategies
- Choosing Partition Strategies Quiz5q
- Partition Key Selection Best Practices
- Partition Key Selection Best Practices Quiz5q
- Transactions and Partition Keys
- Transactions and Partition Keys Quiz5q
- Cross-Partition Query Costs
- Cross-Partition Query Costs Quiz5q
- Data Distribution Analysis
- Data Distribution Analysis Quiz5q
- Throughput Distribution Planning
- Throughput Distribution Planning Quiz5q
- Synthetic Partition Keys
- Synthetic Partition Keys Quiz5q
- Hierarchical Partition Keys
- Hierarchical Partition Keys Quiz5q
- Throughput and Storage Requirements
- Throughput and Storage Requirements Quiz5q
- Serverless vs Provisioned Throughput
- Serverless vs Provisioned Throughput Quiz5q
- Database-Level Provisioned Throughput
- Database-Level Provisioned Throughput Quiz5q
- Granular Scale Units
- Granular Scale Units Quiz5q
- Global Distribution Costs
- Global Distribution Costs Quiz5q
- Configuring Throughput in Portal
- Configuring Throughput in Portal Quiz5q
- Gateway vs Direct Connectivity Mode
- Gateway vs Direct Connectivity Mode Quiz5q
- Creating Database Connections
- Creating Database Connections Quiz5q
- Azure Cosmos DB Emulator
- Azure Cosmos DB Emulator Quiz5q
- Connection Error Handling
- Connection Error Handling Quiz5q
- Singleton Pattern for Clients
- Singleton Pattern for Clients Quiz5q
- Global Distribution Regions
- Global Distribution Regions Quiz5q
- Threading and Parallelism
- Threading and Parallelism Quiz5q
- Arrays and Nested Objects Queries
- Arrays and Nested Objects Queries Quiz5q
- Correlated Subqueries
- Correlated Subqueries Quiz5q
- Array and Type-Checking Functions
- Array and Type-Checking Functions Quiz5q
- Mathematical and String Functions
- Mathematical and String Functions Quiz5q
- Date Functions in Queries
- Date Functions in Queries Quiz5q
- Point Operations vs Query Operations
- Point Operations vs Query Operations Quiz5q
- CRUD Point Operations
- CRUD Point Operations Quiz5q
- Patch Operations for Updates
- Patch Operations for Updates Quiz5q
- Transactional Batch Operations
- Transactional Batch Operations Quiz5q
- Bulk Operations with SDK
- Bulk Operations with SDK Quiz5q
- Optimistic Concurrency with ETags
- Optimistic Concurrency with ETags Quiz5q
- Query Pagination and Continuation
- Query Pagination and Continuation Quiz5q
- Cosmos DB Mirroring for Fabric
- Cosmos DB Mirroring for Fabric Quiz5q
- Mirroring vs Spark Connector
- Mirroring vs Spark Connector Quiz5q
- Enabling Analytical Store
- Enabling Analytical Store Quiz5q
- Synapse Spark and SQL Queries
- Synapse Spark and SQL Queries Quiz5q
- Change Data Capture in Analytical Store
- Change Data Capture in Analytical Store Quiz5q
- Azure Functions and Event Hubs Integration
- Azure Functions and Event Hubs Integration Quiz5q
- Denormalization with Change Feed
- Denormalization with Change Feed Quiz5q
- Referential Integrity with Change Feed
- Referential Integrity with Change Feed Quiz5q
- Azure AI Search Integration
- Azure AI Search Integration Quiz5q
- Azure Functions Change Feed Trigger
- Azure Functions Change Feed Trigger Quiz5q
- Consuming Change Feed with SDK
- Consuming Change Feed with SDK Quiz5q
- Change Feed Estimator
- Change Feed Estimator Quiz5q
- Denormalization via Change Feed
- Denormalization via Change Feed Quiz5q
- Aggregation Persistence with Change Feed
- Aggregation Persistence with Change Feed Quiz5q
- Read-Heavy vs Write-Heavy Indexing
- Read-Heavy vs Write-Heavy Indexing Quiz5q
- Index Type Selection
- Index Type Selection Quiz5q
- Custom Indexing Policies
- Custom Indexing Policies Quiz5q
- Composite Index Implementation
- Composite Index Implementation Quiz5q
- Index Performance Optimization
- Index Performance Optimization Quiz5q
- Response Status Codes and Metrics
- Response Status Codes and Metrics Quiz5q
- Normalized RU Consumption Monitoring
- Normalized RU Consumption Monitoring Quiz5q
- Server-Side Latency Metrics
- Server-Side Latency Metrics Quiz5q
- Data Replication Monitoring
- Data Replication Monitoring Quiz5q
- Azure Monitor Alerts Configuration
- Azure Monitor Alerts Configuration Quiz5q
- Resource Logs Implementation
- Resource Logs Implementation Quiz5q
- Partition Throughput Monitoring
- Partition Throughput Monitoring Quiz5q
- Encryption Key Management
- Encryption Key Management Quiz5q
- Network-Level Access Control
- Network-Level Access Control Quiz5q
- Data Encryption Configuration
- Data Encryption Configuration Quiz5q
- Azure RBAC for Control Plane
- Azure RBAC for Control Plane Quiz5q
- Microsoft Entra ID for Data Plane
- Microsoft Entra ID for Data Plane Quiz5q
- CORS Settings Configuration
- CORS Settings Configuration Quiz5q
- Customer-Managed Keys
- Customer-Managed Keys Quiz5q
- Always Encrypted Implementation
- Always Encrypted Implementation Quiz5q
- Data Movement Strategy Selection
- Data Movement Strategy Selection Quiz5q
- SDK Bulk Operations for Data Movement
- SDK Bulk Operations for Data Movement Quiz5q
- Azure Data Factory Pipelines
- Azure Data Factory Pipelines Quiz5q
- Kafka Connector Integration
- Kafka Connector Integration Quiz5q
- Azure Stream Analytics Integration
- Azure Stream Analytics Integration Quiz5q
- Cosmos DB Spark Connector
- Cosmos DB Spark Connector Quiz5q
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles the points you earn on every lesson and quiz so you progress twice as fast, unlocks half of every practice exam — plus full case studies — with the Learn & Exam study modes, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× points per lesson & quiz
- ✓ 50% of every exam unlocked
- ✓ Learn & Exam modes
- ✓ Distraction-free lessons