Microsoft Azure provides a powerful and flexible cloud computing platform for developers and IT professionals. One of the popular services offered by Azure is Azure Cosmos DB, a globally distributed, multi-model database service. Within Azure Cosmos DB, you can create MongoDB collections to store and manage your data. In this article, we will explore how to create MongoDB collections using Azure Bicep script.
What is Azure Bicep?
Azure Bicep is a domain-specific language (DSL) and a declarative syntax used to define and deploy Azure resources. It simplifies the process of managing Azure resources by providing a more concise and readable way to define infrastructure as code. With Azure Bicep, you can describe your desired Azure infrastructure in a .bicep file and deploy it using Azure CLI or Azure PowerShell.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- An Azure subscription
- Azure CLI or Azure PowerShell installed on your local machine
Step 1: Install Azure CLI or Azure PowerShell
If you haven't already, install Azure CLI or Azure PowerShell on your local machine. You can download and install Azure CLI from the official Azure CLI website. Alternatively, you can install Azure PowerShell by following the instructions provided on the official Azure PowerShell documentation.
Step 2: Create a Bicep file
Once you have Azure CLI or Azure PowerShell installed, create a new file with a .bicep extension. This file will contain the Azure Bicep script to create the MongoDB collection.
resource cosmosDBAccount 'Microsoft.DocumentDB/databaseAccounts@2021-06-15' = {
name: 'myCosmosDBAccount'
location: 'eastus'
kind: 'MongoDB'
properties: {
databaseAccountOfferType: 'Standard'
enableFreeTier: false
enableAutomaticFailover: true
apiProperties: {
serverVersion: '4.0'
}
}
}
resource cosmosDBDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-06-15' = {
name: 'myCosmosDBAccount/myMongoDBDatabase'
location: 'eastus'
properties: {
resource: {
id: 'myMongoDBDatabase'
}
}
}
resource cosmosDBCollection 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-06-15' = {
name: 'myCosmosDBAccount/myMongoDBDatabase/myMongoDBCollection'
location: 'eastus'
properties: {
resource: {
id: 'myMongoDBCollection'
indexingPolicy: {
includedPaths: [
{
path: '/*'
indexes: [
{
kind: 'Range'
dataType: 'Number'
precision: -1
}
]
}
]
}
}
}
}
Step 3: Deploy the Bicep script
Now that you have the Bicep script ready, it's time to deploy it using Azure CLI or Azure PowerShell.
If you are using Azure CLI, open the command prompt or terminal and navigate to the directory where you saved the .bicep file. Run the following command to deploy the Bicep script:
az deployment group create --resource-group myResourceGroup --template-file myBicepScript.bicep
If you are using Azure PowerShell, open PowerShell and navigate to the directory where you saved the .bicep file. Run the following command to deploy the Bicep script:
New-AzResourceGroupDeployment -ResourceGroupName myResourceGroup -TemplateFile myBicepScript.bicep
Replace myResourceGroup with the name of your resource group and myBicepScript.bicep with the name of your Bicep script file.
Step 4: Verify the MongoDB collection
After the deployment is complete, you can verify the creation of the MongoDB collection. You can do this by navigating to the Azure portal, opening your Azure Cosmos DB account, and checking the collections under the MongoDB database you specified in the Bicep script.
Conclusion
In this article, we have learned how to create MongoDB collections using Azure Bicep script. Azure Bicep provides a simplified and declarative way to define and deploy Azure resources. By following the steps outlined in this article, you can easily create MongoDB collections in Azure Cosmos DB using Azure Bicep.
References
| Reference | Link |
|---|---|
| Azure Bicep documentation | https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/bicep-overview |
| Azure CLI documentation | https://docs.microsoft.com/en-us/cli/azure/ |
| Azure PowerShell documentation | https://docs.microsoft.com/en-us/powershell/azure/ |
| Azure Cosmos DB documentation | https://docs.microsoft.com/en-us/azure/cosmos-db/ |