Post

Creating a Azure Cosmos DB NoSQL Account, Database, and Container using Terraform

In this tutorial, we will cover how to create the various resources required for a NoSQL instance of Azure Cosmos DB using Terraform including the database account, the database, and container.

NoSQL Azure Cosmos DB Terraform Tutorial

Creating the Terraform Configuration File

We will make use of the Azure RM Terraform Provider within our Terraform configuration file for provisioning our resources.

In this tutorial we will use the following Azure RM Terraform provider resources:

We will start by creating the Terraform configuration file with the name main.tf.

This Terraform configuration file will provision the Cosmos DB with the following key parameters:

  • A single instance account located in the one region
  • It will be a NoSQL database
  • The database will be on the Serverless plan

Copy and paste the following contents into the main.tf file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.110.0"
    }
  }
}

provider "azurerm" {
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }
  }
}

resource "azurerm_resource_group" "rg" {
  name     = "ces-tf-terraform-rg"
  location = "West US 2"

  tags = {
    environment = "dev"
    source = "terraform"
    owner = "cloudengineerskills"
  }
}

resource "azurerm_cosmosdb_account" "db_account" {
  name                      = "cloudengineerskills-tf-dbacc"
  location                  = azurerm_resource_group.rg.location
  resource_group_name       = azurerm_resource_group.rg.name
  offer_type                = "Standard"
  kind                      = "GlobalDocumentDB"

  geo_location {
    location          = azurerm_resource_group.rg.location
    failover_priority = 0
  }

  consistency_policy {
    consistency_level       = "Session"
  }

  capabilities {
    name = "EnableServerless"
  }

  depends_on = [
    azurerm_resource_group.rg
  ]

  tags = {
    environment = "dev"
    source = "terraform"
    owner = "cloudengineerskills"
  }
}

resource "azurerm_cosmosdb_sql_database" "db" {
  name                = "my-database"
  resource_group_name = azurerm_resource_group.rg.name
  account_name        = azurerm_cosmosdb_account.db_account.name
}

resource "azurerm_cosmosdb_sql_container" "db_container" {
  name                  = "my-container"
  resource_group_name   = azurerm_resource_group.rg.name
  account_name          = azurerm_cosmosdb_account.db_account.name
  database_name         = azurerm_cosmosdb_sql_database.db.name

  partition_key_paths   = ["/id"]
  partition_key_version = 1

  indexing_policy {
    indexing_mode = "consistent"

    included_path {
      path = "/*"
    }

    included_path {
      path = "/included/?"
    }

    excluded_path {
      path = "/excluded/?"
    }
  }
}

Provisioning the Azure Resources with Terraform

Terraform will use the Azure credentials associated with the Azure command line interface az login command to provision resources on your behalf on your Azure account.

Run az login and login with your Azure account and select the Azure subscription you wish to use in the command line.

1
az login

Now that we have the Terraform configuration file main.tf, the next step is to download the Terraform Azure provider which is required for provisioning the cloud resources in Azure using the terraform init command.

1
terraform init

Next we will provision the Azure resources by running the terraform apply command.

1
terraform apply

Terraform will calculate the resources it needs to create and wait for you to confirm by typing Yes before it will proceed to provision the resources on your behalf.

Checking out your Cosmos DB in the Azure Portal

  1. Go into the Azure Portal and in the Resource Groups section you should see the resource group provisioned by Azure
  2. If you select the resource group, go into the Azure Cosmos DB account resource
  3. Have a look in the Data Explorer and you should see the Azure Cosmos DB database resource
  4. Within the Azure Cosmos DB database resource you should see the Azure Cosmos DB container resource
  5. Using the Data Explorer create a new item and save it, then refresh the Azure Cosmos DB container to verify it has been created and the database works as expected

Sample items you can add to your database container

1
2
3
4
5
6
7
8
{
  "id": "1",
  "title": "Complete Project Report",
  "description": "Compile all project data and create a comprehensive report covering all phases of the project, including milestones, deliverables, and outcomes.",
  "status": "In Progress",
  "priority": "High",
  "due_date": "2024-09-15"
}
1
2
3
4
5
6
7
8
{
  "id": "2",
  "title": "Design Marketing Campaign",
  "description": "Develop a creative marketing campaign for the upcoming product launch, focusing on social media and email marketing strategies.",
  "status": "Not Started",
  "priority": "Medium",
  "due_date": "2024-10-01"
}

Cleaning up the resources

To remove all of the Azure resources provisioned using Terraform in your main.tf Terraform configuration file run the terraform destroy command.

1
terraform destroy

Terraform will calculate the resources it needs to destroy and wait for you to confirm by typing Yes before it will proceed to delete the resources on your Azure subscription.

Further Reading

This post is licensed under CC BY 4.0 by the author.