Create CosmosDB using Terraform
2 min readJan 4, 2023
Create CosmosDB using terraform in easy steps.
This example provisions a CosmosDB Account in a single region.
To create an CosmosDB using Terraform, you will need to complete the following steps:
- Install Terraform on your local machine.
2. Create a new directory for your Azure SQL Server project and navigate to it in your terminal.
3. Create a new file called main.tf
in your project directory and add the following code to it:
main.tf
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "${var.prefix}-resources"
location = var.location
}
resource "azurerm_cosmosdb_account" "example" {
name = "${var.prefix}-cosmosdb"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
offer_type = "Standard"
kind = "GlobalDocumentDB"
consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 10
max_staleness_prefix = 200
}
geo_location {
location = azurerm_resource_group.example.location
failover_priority = 0
}
}
variables.tf
variable "prefix" {
description = "epma"
}
variable "location" {
description = "East US"
}
Terraform’s four magic words
terraform init — loads the settings and pulls the providers
terraform plan — preview the environment and show how it will look before going to production
terraform apply — apply system
terraform destroy — destroy :)