From 40c33d528fe396722635c7db96ef212a783dfea8 Mon Sep 17 00:00:00 2001 From: Gin Date: Fri, 1 Jul 2022 05:14:52 -0400 Subject: [PATCH] Cosmosdb l100 examples (#100) --- quickstart/101-cosmos-db-aad-rbac/README.md | 199 +++++++++++++++++ quickstart/101-cosmos-db-aad-rbac/main.tf | 86 ++++++++ quickstart/101-cosmos-db-aad-rbac/outputs.tf | 7 + .../101-cosmos-db-aad-rbac/providers.tf | 12 + .../101-cosmos-db-aad-rbac/variables.tf | 42 ++++ .../101-cosmos-db-analyticalstore/README.md | 172 +++++++++++++++ .../101-cosmos-db-analyticalstore/main.tf | 66 ++++++ .../101-cosmos-db-analyticalstore/outputs.tf | 7 + .../providers.tf | 12 + .../variables.tf | 47 ++++ quickstart/101-cosmos-db-autoscale/README.md | 175 +++++++++++++++ quickstart/101-cosmos-db-autoscale/main.tf | 66 ++++++ quickstart/101-cosmos-db-autoscale/outputs.tf | 7 + .../101-cosmos-db-autoscale/providers.tf | 12 + .../101-cosmos-db-autoscale/variables.tf | 43 ++++ quickstart/101-cosmos-db-free-tier/README.md | 168 ++++++++++++++ quickstart/101-cosmos-db-free-tier/main.tf | 63 ++++++ quickstart/101-cosmos-db-free-tier/outputs.tf | 7 + .../101-cosmos-db-free-tier/providers.tf | 12 + .../101-cosmos-db-free-tier/variables.tf | 42 ++++ .../101-cosmos-db-manualscale/README.md | 167 ++++++++++++++ quickstart/101-cosmos-db-manualscale/main.tf | 62 ++++++ .../101-cosmos-db-manualscale/outputs.tf | 7 + .../101-cosmos-db-manualscale/providers.tf | 12 + .../101-cosmos-db-manualscale/variables.tf | 42 ++++ .../README.md | 208 ++++++++++++++++++ .../main.tf | 87 ++++++++ .../outputs.tf | 7 + .../providers.tf | 12 + .../variables.tf | 57 +++++ 30 files changed, 1906 insertions(+) create mode 100644 quickstart/101-cosmos-db-aad-rbac/README.md create mode 100644 quickstart/101-cosmos-db-aad-rbac/main.tf create mode 100644 quickstart/101-cosmos-db-aad-rbac/outputs.tf create mode 100644 quickstart/101-cosmos-db-aad-rbac/providers.tf create mode 100644 quickstart/101-cosmos-db-aad-rbac/variables.tf create mode 100644 quickstart/101-cosmos-db-analyticalstore/README.md create mode 100644 quickstart/101-cosmos-db-analyticalstore/main.tf create mode 100644 quickstart/101-cosmos-db-analyticalstore/outputs.tf create mode 100644 quickstart/101-cosmos-db-analyticalstore/providers.tf create mode 100644 quickstart/101-cosmos-db-analyticalstore/variables.tf create mode 100644 quickstart/101-cosmos-db-autoscale/README.md create mode 100644 quickstart/101-cosmos-db-autoscale/main.tf create mode 100644 quickstart/101-cosmos-db-autoscale/outputs.tf create mode 100644 quickstart/101-cosmos-db-autoscale/providers.tf create mode 100644 quickstart/101-cosmos-db-autoscale/variables.tf create mode 100644 quickstart/101-cosmos-db-free-tier/README.md create mode 100644 quickstart/101-cosmos-db-free-tier/main.tf create mode 100644 quickstart/101-cosmos-db-free-tier/outputs.tf create mode 100644 quickstart/101-cosmos-db-free-tier/providers.tf create mode 100644 quickstart/101-cosmos-db-free-tier/variables.tf create mode 100644 quickstart/101-cosmos-db-manualscale/README.md create mode 100644 quickstart/101-cosmos-db-manualscale/main.tf create mode 100644 quickstart/101-cosmos-db-manualscale/outputs.tf create mode 100644 quickstart/101-cosmos-db-manualscale/providers.tf create mode 100644 quickstart/101-cosmos-db-manualscale/variables.tf create mode 100644 quickstart/101-cosmos-db-serverside-functionality/README.md create mode 100644 quickstart/101-cosmos-db-serverside-functionality/main.tf create mode 100644 quickstart/101-cosmos-db-serverside-functionality/outputs.tf create mode 100644 quickstart/101-cosmos-db-serverside-functionality/providers.tf create mode 100644 quickstart/101-cosmos-db-serverside-functionality/variables.tf diff --git a/quickstart/101-cosmos-db-aad-rbac/README.md b/quickstart/101-cosmos-db-aad-rbac/README.md new file mode 100644 index 00000000..fdedc4d4 --- /dev/null +++ b/quickstart/101-cosmos-db-aad-rbac/README.md @@ -0,0 +1,199 @@ +# Cosmos db with role definition and assignment +This template deploys a cosmos db account with sql db and aad role definition and assignment. A similar example can be created using the [azurerm/cosmosdb module](https://github.com/azure/terraform-azurerm-cosmosdb). + +## Terraform resource types +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [azurerm_cosmosdb_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_account) +- [azurerm_cosmosdb_sql_database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_database) +- [azurerm_cosmosdb_sql_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_container) +- [azurerm_cosmosdb_sql_role_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_role_assignment) +- [azurerm_cosmosdb_sql_role_definition](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_role_definition) + +## Variables + +| Name | Description | +|-|-| +| `resource_group_name` | Resource group name | +| `resource_group_location` | Resource group location | +| `cosmosdb_account_name` | Cosmos db account name | +| `cosmosdb_location` | Cosmos db primary location | +| `throughput` | DB manual throughput | +| `sql_container_name` | Name of sql container | + +## Example terraform.tfvars file +``` +resource_group_name = "rg-cosmosdb-101" +location = "centralus" +cosmosdb_account_name = "cosmosdb-dev-centralus-101" +cosmosdb_account_location = "centralus" +cosmosdb_sqldb_name = "sqlapidb" +throughput = 400 +sql_container_name = "example-container" +``` + +## Usage + +```bash +>terraform plan + +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + + create + +Terraform will perform the following actions: + + # azurerm_cosmosdb_account.example will be created + + resource "azurerm_cosmosdb_account" "example" { + + access_key_metadata_writes_enabled = true + + analytical_storage_enabled = false + + connection_strings = (sensitive value) + + create_mode = (known after apply) + + default_identity_type = "FirstPartyIdentity" + + enable_automatic_failover = false + + enable_free_tier = false + + enable_multiple_write_locations = false + + endpoint = (known after apply) + + id = (known after apply) + + is_virtual_network_filter_enabled = false + + kind = "GlobalDocumentDB" + + local_authentication_disabled = false + + location = "centralus" + + mongo_server_version = (known after apply) + + name = "cosmosdb-dev-centralus-101" + + network_acl_bypass_for_azure_services = false + + offer_type = "Standard" + + primary_key = (sensitive value) + + primary_readonly_key = (sensitive value) + + public_network_access_enabled = true + + read_endpoints = (known after apply) + + resource_group_name = "rg-cosmosdb-101" + + secondary_key = (sensitive value) + + secondary_readonly_key = (sensitive value) + + write_endpoints = (known after apply) + + + analytical_storage { + + schema_type = (known after apply) + } + + + backup { + + interval_in_minutes = (known after apply) + + retention_in_hours = (known after apply) + + storage_redundancy = (known after apply) + + type = (known after apply) + } + + + capabilities { + + name = (known after apply) + } + + + capacity { + + total_throughput_limit = (known after apply) + } + + + consistency_policy { + + consistency_level = "BoundedStaleness" + + max_interval_in_seconds = 300 + + max_staleness_prefix = 100000 + } + + + geo_location { + + failover_priority = 0 + + id = (known after apply) + + location = "centralus" + + zone_redundant = false + } + } + + # azurerm_cosmosdb_sql_container.example will be created + + resource "azurerm_cosmosdb_sql_container" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + database_name = "sqlapidb" + + default_ttl = (known after apply) + + id = (known after apply) + + name = "example-container" + + partition_key_path = "/definition/id" + + partition_key_version = 1 + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + + + conflict_resolution_policy { + + conflict_resolution_path = (known after apply) + + conflict_resolution_procedure = (known after apply) + + mode = (known after apply) + } + + + indexing_policy { + + indexing_mode = "consistent" + + + excluded_path { + + path = "/excluded/?" + } + + + included_path { + + path = "/*" + } + + included_path { + + path = "/included/?" + } + } + + + unique_key { + + paths = [ + + "/definition/idlong", + + "/definition/idshort", + ] + } + } + + # azurerm_cosmosdb_sql_database.example will be created + + resource "azurerm_cosmosdb_sql_database" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + id = (known after apply) + + name = "sqlapidb" + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + } + + # azurerm_cosmosdb_sql_role_assignment.example will be created + + resource "azurerm_cosmosdb_sql_role_assignment" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + id = (known after apply) + + name = (known after apply) + + principal_id = "1b887731-4609-4904-a699-64f06f3d380d" + + resource_group_name = "rg-cosmosdb-101" + + role_definition_id = (known after apply) + + scope = "/subscriptions/aa86e73d-372b-4cd6-a37e-0d12ae93e964/resourceGroups/rg-cosmosdb-101/providers/Microsoft.DocumentDB/databaseAccounts/cosmosdb-dev-centralus-101" + } + + # azurerm_cosmosdb_sql_role_definition.example will be created + + resource "azurerm_cosmosdb_sql_role_definition" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + assignable_scopes = [ + + "/subscriptions/aa86e73d-372b-4cd6-a37e-0d12ae93e964/resourceGroups/rg-cosmosdb-101/providers/Microsoft.DocumentDB/databaseAccounts/cosmosdb-dev-centralus-101", + ] + + id = (known after apply) + + name = "examplesqlroledef" + + resource_group_name = "rg-cosmosdb-101" + + role_definition_id = (known after apply) + + type = "CustomRole" + + + permissions { + + data_actions = [ + + "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read", + ] + } + } + + # azurerm_resource_group.example will be created + + resource "azurerm_resource_group" "example" { + + id = (known after apply) + + location = "centralus" + + name = "rg-cosmosdb-101" + } + +Plan: 6 to add, 0 to change, 0 to destroy. + +Changes to Outputs: + + cosmosdb_account_id = (known after apply) + + cosmosdb_sql_database_id = (known after apply) +``` + diff --git a/quickstart/101-cosmos-db-aad-rbac/main.tf b/quickstart/101-cosmos-db-aad-rbac/main.tf new file mode 100644 index 00000000..dea815c7 --- /dev/null +++ b/quickstart/101-cosmos-db-aad-rbac/main.tf @@ -0,0 +1,86 @@ +data "azurerm_client_config" "current" {} + +resource "azurerm_resource_group" "example" { + name = var.resource_group_name + location = var.location +} + +resource "azurerm_cosmosdb_account" "example" { + name = var.cosmosdb_account_name + location = var.cosmosdb_account_location + resource_group_name = azurerm_resource_group.example.name + offer_type = "Standard" + kind = "GlobalDocumentDB" + enable_automatic_failover = false + geo_location { + location = var.location + failover_priority = 0 + } + + consistency_policy { + consistency_level = "BoundedStaleness" + max_interval_in_seconds = 300 + max_staleness_prefix = 100000 + } + + depends_on = [ + azurerm_resource_group.example + ] +} + +resource "azurerm_cosmosdb_sql_database" "example" { + name = var.cosmosdb_sqldb_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + throughput = var.throughput +} + +resource "azurerm_cosmosdb_sql_container" "example" { + name = var.sql_container_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + database_name = azurerm_cosmosdb_sql_database.example.name + partition_key_path = "/definition/id" + partition_key_version = 1 + throughput = 400 + + indexing_policy { + indexing_mode = "consistent" + + included_path { + path = "/*" + } + + included_path { + path = "/included/?" + } + + excluded_path { + path = "/excluded/?" + } + } + + unique_key { + paths = ["/definition/idlong", "/definition/idshort"] + } +} + +resource "azurerm_cosmosdb_sql_role_definition" "example" { + name = "examplesqlroledef" + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + type = "CustomRole" + assignable_scopes = ["/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${azurerm_resource_group.example.name}/providers/Microsoft.DocumentDB/databaseAccounts/${azurerm_cosmosdb_account.example.name}"] + + permissions { + data_actions = ["Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read"] + } +} + +resource "azurerm_cosmosdb_sql_role_assignment" "example" { + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + role_definition_id = azurerm_cosmosdb_sql_role_definition.example.id + principal_id = data.azurerm_client_config.current.object_id + scope = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${azurerm_resource_group.example.name}/providers/Microsoft.DocumentDB/databaseAccounts/${azurerm_cosmosdb_account.example.name}" +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-aad-rbac/outputs.tf b/quickstart/101-cosmos-db-aad-rbac/outputs.tf new file mode 100644 index 00000000..7f78abd8 --- /dev/null +++ b/quickstart/101-cosmos-db-aad-rbac/outputs.tf @@ -0,0 +1,7 @@ +output "cosmosdb_account_id" { + value = azurerm_cosmosdb_account.example.id +} + +output "cosmosdb_sql_database_id" { + value = azurerm_cosmosdb_sql_database.example.id +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-aad-rbac/providers.tf b/quickstart/101-cosmos-db-aad-rbac/providers.tf new file mode 100644 index 00000000..47306772 --- /dev/null +++ b/quickstart/101-cosmos-db-aad-rbac/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.0.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-aad-rbac/variables.tf b/quickstart/101-cosmos-db-aad-rbac/variables.tf new file mode 100644 index 00000000..5a11979f --- /dev/null +++ b/quickstart/101-cosmos-db-aad-rbac/variables.tf @@ -0,0 +1,42 @@ +variable "resource_group_name" { + type = string + description = "Resource group name" +} + +variable "location" { + type = string + description = "Resource group location" +} + +variable "cosmosdb_account_name" { + type = string + description = "Cosmos db account name" +} + +variable "cosmosdb_account_location" { + type = string + description = "Cosmos db account location" +} + +variable "cosmosdb_sqldb_name" { + type = string + description = "value" +} + +variable "throughput" { + type = number + description = "Cosmos db database throughput" + validation { + condition = var.throughput >= 400 && var.throughput <= 1000000 + error_message = "Cosmos db manual throughput should be equal to or greater than 400 and less than or equal to 1000000." + } + validation { + condition = var.throughput % 100 == 0 + error_message = "Cosmos db throughput should be in increments of 100." + } +} + +variable "sql_container_name" { + type = string + description = "SQL API container name." +} diff --git a/quickstart/101-cosmos-db-analyticalstore/README.md b/quickstart/101-cosmos-db-analyticalstore/README.md new file mode 100644 index 00000000..b5938706 --- /dev/null +++ b/quickstart/101-cosmos-db-analyticalstore/README.md @@ -0,0 +1,172 @@ +# Cosmos db analytical storage with SQL DB +This template deploys a cosmos db account with sql db and analytical store enabled. A similar example can be created using the [azurerm/cosmosdb module](https://github.com/azure/terraform-azurerm-cosmosdb). + +## Terraform resource types +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [azurerm_cosmosdb_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_account) +- [azurerm_cosmosdb_sql_database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_database) +- [azurerm_cosmosdb_sql_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_container) + +## Variables + +| Name | Description | +|-|-| +| `resource_group_name` | Resource group name | +| `resource_group_location` | Resource group location | +| `cosmosdb_account_name` | Cosmos db account name | +| `cosmosdb_location` | Cosmos db primary location | +| `throughput` | DB manual throughput | +| `sql_container_name` | Name of sql container | +| `analytical_storage_ttl` | TTL of items in container | + +## Example terraform.tfvars file +``` +resource_group_name = "rg-cosmosdb-101" +location = "centralus" +cosmosdb_account_name = "cosmosdb-dev-centralus-101" +cosmosdb_account_location = "centralus" +cosmosdb_sqldb_name = "sqlapidb" +throughput = 400 +sql_container_name = "example-container" +analytical_storage_ttl = 600 +``` + +## Usage + +```bash +>terraform plan + + +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + + create + +Terraform will perform the following actions: + + # azurerm_cosmosdb_account.example will be created + + resource "azurerm_cosmosdb_account" "example" { + + access_key_metadata_writes_enabled = true + + analytical_storage_enabled = true + + connection_strings = (sensitive value) + + create_mode = (known after apply) + + default_identity_type = "FirstPartyIdentity" + + enable_automatic_failover = false + + enable_free_tier = false + + enable_multiple_write_locations = false + + endpoint = (known after apply) + + id = (known after apply) + + is_virtual_network_filter_enabled = false + + kind = "GlobalDocumentDB" + + local_authentication_disabled = false + + location = "centralus" + + mongo_server_version = (known after apply) + + name = "cosmosdb-dev-centralus-101" + + network_acl_bypass_for_azure_services = false + + offer_type = "Standard" + + primary_key = (sensitive value) + + primary_readonly_key = (sensitive value) + + public_network_access_enabled = true + + read_endpoints = (known after apply) + + resource_group_name = "rg-cosmosdb-101" + + secondary_key = (sensitive value) + + secondary_readonly_key = (sensitive value) + + write_endpoints = (known after apply) + + + analytical_storage { + + schema_type = (known after apply) + } + + + backup { + + interval_in_minutes = (known after apply) + + retention_in_hours = (known after apply) + + storage_redundancy = (known after apply) + + type = (known after apply) + } + + + capabilities { + + name = (known after apply) + } + + + capacity { + + total_throughput_limit = (known after apply) + } + + + consistency_policy { + + consistency_level = "BoundedStaleness" + + max_interval_in_seconds = 300 + + max_staleness_prefix = 100000 + } + + + geo_location { + + failover_priority = 0 + + id = (known after apply) + + location = "centralus" + + zone_redundant = false + } + } + + # azurerm_cosmosdb_sql_container.example will be created + + resource "azurerm_cosmosdb_sql_container" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + analytical_storage_ttl = 600 + + database_name = "sqlapidb" + + default_ttl = (known after apply) + + id = (known after apply) + + name = "example-container" + + partition_key_path = "/definition/id" + + partition_key_version = 1 + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + + + conflict_resolution_policy { + + conflict_resolution_path = (known after apply) + + conflict_resolution_procedure = (known after apply) + + mode = (known after apply) + } + + + indexing_policy { + + indexing_mode = "consistent" + + + excluded_path { + + path = "/excluded/?" + } + + + included_path { + + path = "/*" + } + + included_path { + + path = "/included/?" + } + } + + + unique_key { + + paths = [ + + "/definition/idlong", + + "/definition/idshort", + ] + } + } + + # azurerm_cosmosdb_sql_database.example will be created + + resource "azurerm_cosmosdb_sql_database" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + id = (known after apply) + + name = "sqlapidb" + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + } + + # azurerm_resource_group.example will be created + + resource "azurerm_resource_group" "example" { + + id = (known after apply) + + location = "centralus" + + name = "rg-cosmosdb-101" + } + +Plan: 4 to add, 0 to change, 0 to destroy. + +Changes to Outputs: + + cosmosdb_account_id = (known after apply) + + cosmosdb_sql_database_id = (known after apply) + +``` + diff --git a/quickstart/101-cosmos-db-analyticalstore/main.tf b/quickstart/101-cosmos-db-analyticalstore/main.tf new file mode 100644 index 00000000..ebd6dc5b --- /dev/null +++ b/quickstart/101-cosmos-db-analyticalstore/main.tf @@ -0,0 +1,66 @@ +resource "azurerm_resource_group" "example" { + name = var.resource_group_name + location = var.location +} + +resource "azurerm_cosmosdb_account" "example" { + name = var.cosmosdb_account_name + location = var.cosmosdb_account_location + resource_group_name = azurerm_resource_group.example.name + offer_type = "Standard" + kind = "GlobalDocumentDB" + enable_automatic_failover = false + analytical_storage_enabled = true + geo_location { + location = var.location + failover_priority = 0 + } + + consistency_policy { + consistency_level = "BoundedStaleness" + max_interval_in_seconds = 300 + max_staleness_prefix = 100000 + } + + depends_on = [ + azurerm_resource_group.example + ] +} + +resource "azurerm_cosmosdb_sql_database" "example" { + name = var.cosmosdb_sqldb_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + throughput = var.throughput +} + +resource "azurerm_cosmosdb_sql_container" "example" { + name = var.sql_container_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + database_name = azurerm_cosmosdb_sql_database.example.name + partition_key_path = "/definition/id" + partition_key_version = 1 + throughput = 400 + analytical_storage_ttl = var.analytical_storage_ttl + + indexing_policy { + indexing_mode = "consistent" + + included_path { + path = "/*" + } + + included_path { + path = "/included/?" + } + + excluded_path { + path = "/excluded/?" + } + } + + unique_key { + paths = ["/definition/idlong", "/definition/idshort"] + } +} diff --git a/quickstart/101-cosmos-db-analyticalstore/outputs.tf b/quickstart/101-cosmos-db-analyticalstore/outputs.tf new file mode 100644 index 00000000..7f78abd8 --- /dev/null +++ b/quickstart/101-cosmos-db-analyticalstore/outputs.tf @@ -0,0 +1,7 @@ +output "cosmosdb_account_id" { + value = azurerm_cosmosdb_account.example.id +} + +output "cosmosdb_sql_database_id" { + value = azurerm_cosmosdb_sql_database.example.id +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-analyticalstore/providers.tf b/quickstart/101-cosmos-db-analyticalstore/providers.tf new file mode 100644 index 00000000..47306772 --- /dev/null +++ b/quickstart/101-cosmos-db-analyticalstore/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.0.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-analyticalstore/variables.tf b/quickstart/101-cosmos-db-analyticalstore/variables.tf new file mode 100644 index 00000000..2f6392be --- /dev/null +++ b/quickstart/101-cosmos-db-analyticalstore/variables.tf @@ -0,0 +1,47 @@ +variable "resource_group_name" { + type = string + description = "Resource group name" +} + +variable "location" { + type = string + description = "Resource group location" +} + +variable "cosmosdb_account_name" { + type = string + description = "Cosmos db account name" +} + +variable "cosmosdb_account_location" { + type = string + description = "Cosmos db account location" +} + +variable "cosmosdb_sqldb_name" { + type = string + description = "value" +} + +variable "throughput" { + type = number + description = "Cosmos db database throughput" + validation { + condition = var.throughput >= 400 && var.throughput <= 1000000 + error_message = "Cosmos db manual throughput should be equal to or greater than 400 and less than or equal to 1000000." + } + validation { + condition = var.throughput % 100 == 0 + error_message = "Cosmos db throughput should be in increments of 100." + } +} + +variable "sql_container_name" { + type = string + description = "SQL API container name." +} + +variable "analytical_storage_ttl" { + type = number + description = "Analytical Storage TTL in seconds." +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-autoscale/README.md b/quickstart/101-cosmos-db-autoscale/README.md new file mode 100644 index 00000000..16239260 --- /dev/null +++ b/quickstart/101-cosmos-db-autoscale/README.md @@ -0,0 +1,175 @@ +# Cosmos db autoscaling with SQL DB + +This template deploys a cosmos db account with sql db and autoscaling enabled. A similar example can be created using the [azurerm/cosmosdb module](https://github.com/azure/terraform-azurerm-cosmosdb). + +## Terraform resource types +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [azurerm_cosmosdb_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_account) +- [azurerm_cosmosdb_sql_database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_database) +- [azurerm_cosmosdb_sql_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_container) + +## Variables + +| Name | Description | +|-|-| +| `resource_group_name` | Resource group name | +| `resource_group_location` | Resource group location | +| `cosmosdb_account_name` | Cosmos db account name | +| `cosmosdb_location` | Cosmos db primary location | +| `sql_container_name` | Name of sql container | +| `max_throughput` | DB Max throughput for autoscaling | + +## Example terraform.tfvars file +``` +resource_group_name = "rg-cosmosdb-101" +location = "centralus" +cosmosdb_account_name = "cosmosdb-dev-centralus-101" +cosmosdb_account_location = "centralus" +sql_container_name = "example-container" +cosmosdb_sqldb_name = "sqlapidb" +max_throughput = 4000 +``` + +## Usage + +```bash +>terraform plan +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + + create + +Terraform will perform the following actions: + + # azurerm_cosmosdb_account.example will be created + + resource "azurerm_cosmosdb_account" "example" { + + access_key_metadata_writes_enabled = true + + analytical_storage_enabled = false + + connection_strings = (sensitive value) + + create_mode = (known after apply) + + default_identity_type = "FirstPartyIdentity" + + enable_automatic_failover = false + + enable_free_tier = false + + enable_multiple_write_locations = false + + endpoint = (known after apply) + + id = (known after apply) + + is_virtual_network_filter_enabled = false + + kind = "GlobalDocumentDB" + + local_authentication_disabled = false + + location = "centralus" + + mongo_server_version = (known after apply) + + name = "cosmosdb-dev-centralus-101" + + network_acl_bypass_for_azure_services = false + + offer_type = "Standard" + + primary_key = (sensitive value) + + primary_readonly_key = (sensitive value) + + public_network_access_enabled = true + + read_endpoints = (known after apply) + + resource_group_name = "rg-cosmosdb-101" + + secondary_key = (sensitive value) + + secondary_readonly_key = (sensitive value) + + write_endpoints = (known after apply) + + + analytical_storage { + + schema_type = (known after apply) + } + + + backup { + + interval_in_minutes = (known after apply) + + retention_in_hours = (known after apply) + + storage_redundancy = (known after apply) + + type = (known after apply) + } + + + capabilities { + + name = (known after apply) + } + + + capacity { + + total_throughput_limit = (known after apply) + } + + + consistency_policy { + + consistency_level = "BoundedStaleness" + + max_interval_in_seconds = 300 + + max_staleness_prefix = 100000 + } + + + geo_location { + + failover_priority = 0 + + id = (known after apply) + + location = "centralus" + + zone_redundant = false + } + } + + # azurerm_cosmosdb_sql_container.example will be created + + resource "azurerm_cosmosdb_sql_container" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + database_name = "sqlapidb" + + default_ttl = (known after apply) + + id = (known after apply) + + name = "example-container" + + partition_key_path = "/definition/id" + + partition_key_version = 1 + + resource_group_name = "rg-cosmosdb-101" + + throughput = (known after apply) + + + autoscale_settings { + + max_throughput = 4000 + } + + + conflict_resolution_policy { + + conflict_resolution_path = (known after apply) + + conflict_resolution_procedure = (known after apply) + + mode = (known after apply) + } + + + indexing_policy { + + indexing_mode = "consistent" + + + excluded_path { + + path = "/excluded/?" + } + + + included_path { + + path = "/*" + } + + included_path { + + path = "/included/?" + } + } + + + unique_key { + + paths = [ + + "/definition/idlong", + + "/definition/idshort", + ] + } + } + + # azurerm_cosmosdb_sql_database.example will be created + + resource "azurerm_cosmosdb_sql_database" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + id = (known after apply) + + name = "sqlapidb" + + resource_group_name = "rg-cosmosdb-101" + + throughput = (known after apply) + + + autoscale_settings { + + max_throughput = 4000 + } + } + + # azurerm_resource_group.example will be created + + resource "azurerm_resource_group" "example" { + + id = (known after apply) + + location = "centralus" + + name = "rg-cosmosdb-101" + } + +Plan: 4 to add, 0 to change, 0 to destroy. + +Changes to Outputs: + + cosmosdb_account_id = (known after apply) + + cosmosdb_sql_database_id = (known after apply) +``` + diff --git a/quickstart/101-cosmos-db-autoscale/main.tf b/quickstart/101-cosmos-db-autoscale/main.tf new file mode 100644 index 00000000..e44cfa0d --- /dev/null +++ b/quickstart/101-cosmos-db-autoscale/main.tf @@ -0,0 +1,66 @@ +resource "azurerm_resource_group" "example" { + name = var.resource_group_name + location = var.location +} + +resource "azurerm_cosmosdb_account" "example" { + name = var.cosmosdb_account_name + location = var.cosmosdb_account_location + resource_group_name = azurerm_resource_group.example.name + offer_type = "Standard" + kind = "GlobalDocumentDB" + enable_automatic_failover = false + geo_location { + location = var.location + failover_priority = 0 + } + consistency_policy { + consistency_level = "BoundedStaleness" + max_interval_in_seconds = 300 + max_staleness_prefix = 100000 + } + depends_on = [ + azurerm_resource_group.example + ] +} + +resource "azurerm_cosmosdb_sql_database" "example" { + name = var.cosmosdb_sqldb_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + autoscale_settings { + max_throughput = var.max_throughput + } +} + +resource "azurerm_cosmosdb_sql_container" "example" { + name = var.sql_container_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + database_name = azurerm_cosmosdb_sql_database.example.name + partition_key_path = "/definition/id" + partition_key_version = 1 + autoscale_settings { + max_throughput = var.max_throughput + } + + indexing_policy { + indexing_mode = "consistent" + + included_path { + path = "/*" + } + + included_path { + path = "/included/?" + } + + excluded_path { + path = "/excluded/?" + } + } + + unique_key { + paths = ["/definition/idlong", "/definition/idshort"] + } +} diff --git a/quickstart/101-cosmos-db-autoscale/outputs.tf b/quickstart/101-cosmos-db-autoscale/outputs.tf new file mode 100644 index 00000000..7f78abd8 --- /dev/null +++ b/quickstart/101-cosmos-db-autoscale/outputs.tf @@ -0,0 +1,7 @@ +output "cosmosdb_account_id" { + value = azurerm_cosmosdb_account.example.id +} + +output "cosmosdb_sql_database_id" { + value = azurerm_cosmosdb_sql_database.example.id +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-autoscale/providers.tf b/quickstart/101-cosmos-db-autoscale/providers.tf new file mode 100644 index 00000000..47306772 --- /dev/null +++ b/quickstart/101-cosmos-db-autoscale/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.0.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-autoscale/variables.tf b/quickstart/101-cosmos-db-autoscale/variables.tf new file mode 100644 index 00000000..9613347f --- /dev/null +++ b/quickstart/101-cosmos-db-autoscale/variables.tf @@ -0,0 +1,43 @@ +variable "resource_group_name" { + type = string + description = "Resource group name" +} + +variable "location" { + type = string + description = "Resource group location" +} + +variable "cosmosdb_account_name" { + type = string + description = "Cosmos db account name" +} + +variable "cosmosdb_account_location" { + type = string + description = "Cosmos db account location" +} + +variable "cosmosdb_sqldb_name" { + type = string + description = "value" +} + +variable "sql_container_name" { + type = string + description = "SQL API container name." +} + +variable "max_throughput" { + type = number + description = "Cosmos db database max throughput" + validation { + condition = var.max_throughput >= 4000 && var.max_throughput <= 1000000 + error_message = "Cosmos db autoscale max throughput should be equal to or greater than 4000 and less than or equal to 1000000." + } + validation { + condition = var.max_throughput % 100 == 0 + error_message = "Cosmos db max throughput should be in increments of 100." + } +} + diff --git a/quickstart/101-cosmos-db-free-tier/README.md b/quickstart/101-cosmos-db-free-tier/README.md new file mode 100644 index 00000000..7434de69 --- /dev/null +++ b/quickstart/101-cosmos-db-free-tier/README.md @@ -0,0 +1,168 @@ +# Cosmos db free tier +This template deploys a cosmos db account with sql db and manual scaling in free tier. A similar example can be created using the [azurerm/cosmosdb module](https://github.com/azure/terraform-azurerm-cosmosdb). + +## Terraform resource types +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [azurerm_cosmosdb_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_account) +- [azurerm_cosmosdb_sql_database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_database) +- [azurerm_cosmosdb_sql_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_container) + +## Variables + +| Name | Description | +|-|-| +| `resource_group_name` | Resource group name | +| `resource_group_location` | Resource group location | +| `cosmosdb_account_name` | Cosmos db account name | +| `cosmosdb_location` | Cosmos db primary location | +| `sql_container_name` | Name of sql container | +| `throughput` | DB manual throughput | + +## Example terraform.tfvars file +``` +resource_group_name = "rg-cosmosdb-101" +location = "centralus" +cosmosdb_account_name = "cosmosdb-dev-centralus-101" +cosmosdb_account_location = "centralus" +cosmosdb_sqldb_name = "sqlapidb" +sql_container_name = "example-container" +throughput = 400 +``` + +## Usage + +```bash +>terraform plan + + +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + + create + +Terraform will perform the following actions: + + # azurerm_cosmosdb_account.example will be created + + resource "azurerm_cosmosdb_account" "example" { + + access_key_metadata_writes_enabled = true + + analytical_storage_enabled = false + + connection_strings = (sensitive value) + + create_mode = (known after apply) + + default_identity_type = "FirstPartyIdentity" + + enable_automatic_failover = false + + enable_free_tier = true + + enable_multiple_write_locations = false + + endpoint = (known after apply) + + id = (known after apply) + + is_virtual_network_filter_enabled = false + + kind = "GlobalDocumentDB" + + local_authentication_disabled = false + + location = "centralus" + + mongo_server_version = (known after apply) + + name = "cosmosdb-dev-centralus-101" + + network_acl_bypass_for_azure_services = false + + offer_type = "Standard" + + primary_key = (sensitive value) + + primary_readonly_key = (sensitive value) + + public_network_access_enabled = true + + read_endpoints = (known after apply) + + resource_group_name = "rg-cosmosdb-101" + + secondary_key = (sensitive value) + + secondary_readonly_key = (sensitive value) + + write_endpoints = (known after apply) + + + analytical_storage { + + schema_type = (known after apply) + } + + + backup { + + interval_in_minutes = (known after apply) + + retention_in_hours = (known after apply) + + storage_redundancy = (known after apply) + + type = (known after apply) + } + + + capabilities { + + name = (known after apply) + } + + + capacity { + + total_throughput_limit = (known after apply) + } + + + consistency_policy { + + consistency_level = "BoundedStaleness" + + max_interval_in_seconds = 300 + + max_staleness_prefix = 100000 + } + + + geo_location { + + failover_priority = 0 + + id = (known after apply) + + location = "centralus" + + zone_redundant = false + } + } + + # azurerm_cosmosdb_sql_container.example will be created + + resource "azurerm_cosmosdb_sql_container" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + database_name = "sqlapidb" + + default_ttl = (known after apply) + + id = (known after apply) + + name = "example-container" + + partition_key_path = "/definition/id" + + partition_key_version = 1 + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + + + conflict_resolution_policy { + + conflict_resolution_path = (known after apply) + + conflict_resolution_procedure = (known after apply) + + mode = (known after apply) + } + + + indexing_policy { + + indexing_mode = "consistent" + + + excluded_path { + + path = "/excluded/?" + } + + + included_path { + + path = "/*" + } + + included_path { + + path = "/included/?" + } + } + + + unique_key { + + paths = [ + + "/definition/idlong", + + "/definition/idshort", + ] + } + } + + # azurerm_cosmosdb_sql_database.example will be created + + resource "azurerm_cosmosdb_sql_database" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + id = (known after apply) + + name = "sqlapidb" + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + } + + # azurerm_resource_group.example will be created + + resource "azurerm_resource_group" "example" { + + id = (known after apply) + + location = "centralus" + + name = "rg-cosmosdb-101" + } + +Plan: 4 to add, 0 to change, 0 to destroy. + +Changes to Outputs: + + cosmosdb_account_id = (known after apply) + + cosmosdb_sql_database_id = (known after apply) + +``` \ No newline at end of file diff --git a/quickstart/101-cosmos-db-free-tier/main.tf b/quickstart/101-cosmos-db-free-tier/main.tf new file mode 100644 index 00000000..195dc39d --- /dev/null +++ b/quickstart/101-cosmos-db-free-tier/main.tf @@ -0,0 +1,63 @@ +resource "azurerm_resource_group" "example" { + name = var.resource_group_name + location = var.location +} + +resource "azurerm_cosmosdb_account" "example" { + name = var.cosmosdb_account_name + location = var.cosmosdb_account_location + resource_group_name = azurerm_resource_group.example.name + offer_type = "Standard" + kind = "GlobalDocumentDB" + enable_automatic_failover = false + enable_free_tier = true + geo_location { + location = var.location + failover_priority = 0 + } + consistency_policy { + consistency_level = "BoundedStaleness" + max_interval_in_seconds = 300 + max_staleness_prefix = 100000 + } + depends_on = [ + azurerm_resource_group.example + ] +} + +resource "azurerm_cosmosdb_sql_database" "example" { + name = var.cosmosdb_sqldb_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + throughput = var.throughput +} + +resource "azurerm_cosmosdb_sql_container" "example" { + name = var.sql_container_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + database_name = azurerm_cosmosdb_sql_database.example.name + partition_key_path = "/definition/id" + partition_key_version = 1 + throughput = var.throughput + + indexing_policy { + indexing_mode = "consistent" + + included_path { + path = "/*" + } + + included_path { + path = "/included/?" + } + + excluded_path { + path = "/excluded/?" + } + } + + unique_key { + paths = ["/definition/idlong", "/definition/idshort"] + } +} diff --git a/quickstart/101-cosmos-db-free-tier/outputs.tf b/quickstart/101-cosmos-db-free-tier/outputs.tf new file mode 100644 index 00000000..7f78abd8 --- /dev/null +++ b/quickstart/101-cosmos-db-free-tier/outputs.tf @@ -0,0 +1,7 @@ +output "cosmosdb_account_id" { + value = azurerm_cosmosdb_account.example.id +} + +output "cosmosdb_sql_database_id" { + value = azurerm_cosmosdb_sql_database.example.id +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-free-tier/providers.tf b/quickstart/101-cosmos-db-free-tier/providers.tf new file mode 100644 index 00000000..47306772 --- /dev/null +++ b/quickstart/101-cosmos-db-free-tier/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.0.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-free-tier/variables.tf b/quickstart/101-cosmos-db-free-tier/variables.tf new file mode 100644 index 00000000..7387aa38 --- /dev/null +++ b/quickstart/101-cosmos-db-free-tier/variables.tf @@ -0,0 +1,42 @@ +variable "resource_group_name" { + type = string + description = "Resource group name" +} + +variable "location" { + type = string + description = "Resource group location" +} + +variable "cosmosdb_account_name" { + type = string + description = "Cosmos db account name" +} + +variable "cosmosdb_account_location" { + type = string + description = "Cosmos db account location" +} + +variable "cosmosdb_sqldb_name" { + type = string + description = "value" +} + +variable "sql_container_name" { + type = string + description = "SQL API container name." +} + +variable "throughput" { + type = number + description = "Cosmos db database throughput" + validation { + condition = var.throughput >= 400 && var.throughput <= 1000000 + error_message = "Cosmos db manual throughput should be equal to or greater than 400 and less than or equal to 1000000." + } + validation { + condition = var.throughput % 100 == 0 + error_message = "Cosmos db throughput should be in increments of 100." + } +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-manualscale/README.md b/quickstart/101-cosmos-db-manualscale/README.md new file mode 100644 index 00000000..6ab5ea3d --- /dev/null +++ b/quickstart/101-cosmos-db-manualscale/README.md @@ -0,0 +1,167 @@ +# Cosmos db manual scaling with SQL DB +This template deploys a cosmos db account with sql db and manual scaling. A similar example can be created using the [azurerm/cosmosdb module](https://github.com/azure/terraform-azurerm-cosmosdb). + +## Terraform resource types +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [azurerm_cosmosdb_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_account) +- [azurerm_cosmosdb_sql_database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_database) +- [azurerm_cosmosdb_sql_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_container) + +## Variables + +| Name | Description | +|-|-| +| `resource_group_name` | Resource group name | +| `resource_group_location` | Resource group location | +| `cosmosdb_account_name` | Cosmos db account name | +| `cosmosdb_location` | Cosmos db primary location | +| `sql_container_name` | Name of sql container | +| `throughput` | DB manual throughput | + +## Example terraform.tfvars file +``` +resource_group_name = "rg-cosmosdb-101" +location = "centralus" +cosmosdb_account_name = "cosmosdb-dev-centralus-101" +cosmosdb_account_location = "centralus" +cosmosdb_sqldb_name = "sqlapidb" +sql_container_name = "example-container" +throughput = 400 +``` + +## Usage + +```bash +>terraform plan +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + + create + +Terraform will perform the following actions: + + # azurerm_cosmosdb_account.example will be created + + resource "azurerm_cosmosdb_account" "example" { + + access_key_metadata_writes_enabled = true + + analytical_storage_enabled = false + + connection_strings = (sensitive value) + + create_mode = (known after apply) + + default_identity_type = "FirstPartyIdentity" + + enable_automatic_failover = false + + enable_free_tier = false + + enable_multiple_write_locations = false + + endpoint = (known after apply) + + id = (known after apply) + + is_virtual_network_filter_enabled = false + + kind = "GlobalDocumentDB" + + local_authentication_disabled = false + + location = "centralus" + + mongo_server_version = (known after apply) + + name = "cosmosdb-dev-centralus-101" + + network_acl_bypass_for_azure_services = false + + offer_type = "Standard" + + primary_key = (sensitive value) + + primary_readonly_key = (sensitive value) + + public_network_access_enabled = true + + read_endpoints = (known after apply) + + resource_group_name = "rg-cosmosdb-101" + + secondary_key = (sensitive value) + + secondary_readonly_key = (sensitive value) + + write_endpoints = (known after apply) + + + analytical_storage { + + schema_type = (known after apply) + } + + + backup { + + interval_in_minutes = (known after apply) + + retention_in_hours = (known after apply) + + storage_redundancy = (known after apply) + + type = (known after apply) + } + + + capabilities { + + name = (known after apply) + } + + + capacity { + + total_throughput_limit = (known after apply) + } + + + consistency_policy { + + consistency_level = "BoundedStaleness" + + max_interval_in_seconds = 300 + + max_staleness_prefix = 100000 + } + + + geo_location { + + failover_priority = 0 + + id = (known after apply) + + location = "centralus" + + zone_redundant = false + } + } + + # azurerm_cosmosdb_sql_container.example will be created + + resource "azurerm_cosmosdb_sql_container" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + database_name = "sqlapidb" + + default_ttl = (known after apply) + + id = (known after apply) + + name = "example-container" + + partition_key_path = "/definition/id" + + partition_key_version = 1 + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + + + conflict_resolution_policy { + + conflict_resolution_path = (known after apply) + + conflict_resolution_procedure = (known after apply) + + mode = (known after apply) + } + + + indexing_policy { + + indexing_mode = "consistent" + + + excluded_path { + + path = "/excluded/?" + } + + + included_path { + + path = "/*" + } + + included_path { + + path = "/included/?" + } + } + + + unique_key { + + paths = [ + + "/definition/idlong", + + "/definition/idshort", + ] + } + } + + # azurerm_cosmosdb_sql_database.example will be created + + resource "azurerm_cosmosdb_sql_database" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + id = (known after apply) + + name = "sqlapidb" + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + } + + # azurerm_resource_group.example will be created + + resource "azurerm_resource_group" "example" { + + id = (known after apply) + + location = "centralus" + + name = "rg-cosmosdb-101" + } + +Plan: 4 to add, 0 to change, 0 to destroy. + +Changes to Outputs: + + cosmosdb_account_id = (known after apply) + + cosmosdb_sql_database_id = (known after apply) + +``` + diff --git a/quickstart/101-cosmos-db-manualscale/main.tf b/quickstart/101-cosmos-db-manualscale/main.tf new file mode 100644 index 00000000..a1e48857 --- /dev/null +++ b/quickstart/101-cosmos-db-manualscale/main.tf @@ -0,0 +1,62 @@ +resource "azurerm_resource_group" "example" { + name = var.resource_group_name + location = var.location +} + +resource "azurerm_cosmosdb_account" "example" { + name = var.cosmosdb_account_name + location = var.cosmosdb_account_location + resource_group_name = azurerm_resource_group.example.name + offer_type = "Standard" + kind = "GlobalDocumentDB" + enable_automatic_failover = false + geo_location { + location = var.location + failover_priority = 0 + } + consistency_policy { + consistency_level = "BoundedStaleness" + max_interval_in_seconds = 300 + max_staleness_prefix = 100000 + } + depends_on = [ + azurerm_resource_group.example + ] +} + +resource "azurerm_cosmosdb_sql_database" "example" { + name = var.cosmosdb_sqldb_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + throughput = var.throughput +} + +resource "azurerm_cosmosdb_sql_container" "example" { + name = var.sql_container_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + database_name = azurerm_cosmosdb_sql_database.example.name + partition_key_path = "/definition/id" + partition_key_version = 1 + throughput = var.throughput + + indexing_policy { + indexing_mode = "consistent" + + included_path { + path = "/*" + } + + included_path { + path = "/included/?" + } + + excluded_path { + path = "/excluded/?" + } + } + + unique_key { + paths = ["/definition/idlong", "/definition/idshort"] + } +} diff --git a/quickstart/101-cosmos-db-manualscale/outputs.tf b/quickstart/101-cosmos-db-manualscale/outputs.tf new file mode 100644 index 00000000..7f78abd8 --- /dev/null +++ b/quickstart/101-cosmos-db-manualscale/outputs.tf @@ -0,0 +1,7 @@ +output "cosmosdb_account_id" { + value = azurerm_cosmosdb_account.example.id +} + +output "cosmosdb_sql_database_id" { + value = azurerm_cosmosdb_sql_database.example.id +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-manualscale/providers.tf b/quickstart/101-cosmos-db-manualscale/providers.tf new file mode 100644 index 00000000..47306772 --- /dev/null +++ b/quickstart/101-cosmos-db-manualscale/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.0.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-manualscale/variables.tf b/quickstart/101-cosmos-db-manualscale/variables.tf new file mode 100644 index 00000000..7387aa38 --- /dev/null +++ b/quickstart/101-cosmos-db-manualscale/variables.tf @@ -0,0 +1,42 @@ +variable "resource_group_name" { + type = string + description = "Resource group name" +} + +variable "location" { + type = string + description = "Resource group location" +} + +variable "cosmosdb_account_name" { + type = string + description = "Cosmos db account name" +} + +variable "cosmosdb_account_location" { + type = string + description = "Cosmos db account location" +} + +variable "cosmosdb_sqldb_name" { + type = string + description = "value" +} + +variable "sql_container_name" { + type = string + description = "SQL API container name." +} + +variable "throughput" { + type = number + description = "Cosmos db database throughput" + validation { + condition = var.throughput >= 400 && var.throughput <= 1000000 + error_message = "Cosmos db manual throughput should be equal to or greater than 400 and less than or equal to 1000000." + } + validation { + condition = var.throughput % 100 == 0 + error_message = "Cosmos db throughput should be in increments of 100." + } +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-serverside-functionality/README.md b/quickstart/101-cosmos-db-serverside-functionality/README.md new file mode 100644 index 00000000..92e5d999 --- /dev/null +++ b/quickstart/101-cosmos-db-serverside-functionality/README.md @@ -0,0 +1,208 @@ +# Cosmos db serverside functionality with SQL DB +This template deploys a cosmos db account with sql db and serverside functionality. A similar example can be created using the [azurerm/cosmosdb module](https://github.com/azure/terraform-azurerm-cosmosdb). + +## Terraform resource types +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [azurerm_cosmosdb_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_account) +- [azurerm_cosmosdb_sql_database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_database) +- [azurerm_cosmosdb_sql_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_container) +- [azurerm_cosmosdb_sql_stored_procedure](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_stored_procedure) +- [azurerm_cosmosdb_sql_trigger](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_trigger) +- [azurerm_cosmosdb_sql_function](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_function) + +## Variables + +| Name | Description | +|-|-| +| `resource_group_name` | Resource group name | +| `resource_group_location` | Resource group location | +| `cosmosdb_account_name` | Cosmos db account name | +| `cosmosdb_location` | Cosmos db primary location | +| `throughput` | DB manual throughput | +| `sql_container_name` | Name of sql container | +| `sql_stored_procedure_name` | Name of stored procedure | +| `sql_trigger_name` | Name of trigger | +| `sql_function_name` | Name of function name | + +## Example terraform.tfvars file +``` +resource_group_name = "rg-cosmosdb-101" +location = "centralus" +cosmosdb_account_name = "cosmosdb-dev-centralus-101" +cosmosdb_account_location = "centralus" +cosmosdb_sqldb_name = "sqlapidb" +throughput = 400 +sql_container_name = "example-container" +sql_stored_procedure_name = "myStoredProcedure" +sql_trigger_name = "myPreTrigger" +sql_function_name = "myUserDefinedFunction" +``` + +## Usage + +```bash +>terraform plan + +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + + create + +Terraform will perform the following actions: + # azurerm_cosmosdb_account.example will be created + + resource "azurerm_cosmosdb_account" "example" { + + access_key_metadata_writes_enabled = true + + analytical_storage_enabled = false + + connection_strings = (sensitive value) + + create_mode = (known after apply) + + default_identity_type = "FirstPartyIdentity" + + enable_automatic_failover = false + + enable_free_tier = false + + enable_multiple_write_locations = false + + endpoint = (known after apply) + + id = (known after apply) + + is_virtual_network_filter_enabled = false + + kind = "GlobalDocumentDB" + + local_authentication_disabled = false + + location = "centralus" + + mongo_server_version = (known after apply) + + name = "cosmosdb-dev-centralus-101" + + network_acl_bypass_for_azure_services = false + + offer_type = "Standard" + + primary_key = (sensitive value) + + primary_readonly_key = (sensitive value) + + public_network_access_enabled = true + + read_endpoints = (known after apply) + + resource_group_name = "rg-cosmosdb-101" + + secondary_key = (sensitive value) + + secondary_readonly_key = (sensitive value) + + write_endpoints = (known after apply) + + + analytical_storage { + + schema_type = (known after apply) + } + + + backup { + + interval_in_minutes = (known after apply) + + retention_in_hours = (known after apply) + + storage_redundancy = (known after apply) + + type = (known after apply) + } + + + capabilities { + + name = (known after apply) + } + + + capacity { + + total_throughput_limit = (known after apply) + } + + + consistency_policy { + + consistency_level = "BoundedStaleness" + + max_interval_in_seconds = 300 + + max_staleness_prefix = 100000 + } + + + geo_location { + + failover_priority = 0 + + id = (known after apply) + + location = "centralus" + + zone_redundant = false + } + } + + # azurerm_cosmosdb_sql_container.example will be created + + resource "azurerm_cosmosdb_sql_container" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + database_name = "sqlapidb" + + default_ttl = (known after apply) + + id = (known after apply) + + name = "example-container" + + partition_key_path = "/definition/id" + + partition_key_version = 1 + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + + + conflict_resolution_policy { + + conflict_resolution_path = (known after apply) + + conflict_resolution_procedure = (known after apply) + + mode = (known after apply) + } + + + indexing_policy { + + indexing_mode = "consistent" + + + excluded_path { + + path = "/excluded/?" + } + + + included_path { + + path = "/*" + } + + included_path { + + path = "/included/?" + } + } + + + unique_key { + + paths = [ + + "/definition/idlong", + + "/definition/idshort", + ] + } + } + + # azurerm_cosmosdb_sql_database.example will be created + + resource "azurerm_cosmosdb_sql_database" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + id = (known after apply) + + name = "sqlapidb" + + resource_group_name = "rg-cosmosdb-101" + + throughput = 400 + } + + # azurerm_cosmosdb_sql_function.example will be created + + resource "azurerm_cosmosdb_sql_function" "example" { + + body = "function tax(income){if(income==undefined)throw'no input';if(income<1000)return income*0.1;else if(income<10000)return income*0.2;else return income*0.4;}" + + container_id = (known after apply) + + id = (known after apply) + + name = "myUserDefinedFunction" + } + + # azurerm_cosmosdb_sql_stored_procedure.example will be created + + resource "azurerm_cosmosdb_sql_stored_procedure" "example" { + + account_name = "cosmosdb-dev-centralus-101" + + body = <<-EOT + function () { var context = getContext(); var response = context.getResponse(); response.setBody('Hello, World'); } + EOT + + container_name = "example-container" + + database_name = "sqlapidb" + + id = (known after apply) + + name = "myStoredProcedure" + + resource_group_name = "rg-cosmosdb-101" + } + + # azurerm_cosmosdb_sql_trigger.example will be created + + resource "azurerm_cosmosdb_sql_trigger" "example" { + + body = "function validateToDoItemTimestamp(){var context=getContext();var request=context.getRequest();var itemToCreate=request.getBody();if(!('timestamp'in itemToCreate)){var ts=new Date();itemToCreate['timestamp']=ts.getTime();}request.setBody(itemToCreate);}" + + container_id = (known after apply) + + id = (known after apply) + + name = "myPreTrigger" + + operation = "Create" + + type = "Pre" + } + + # azurerm_resource_group.example will be created + + resource "azurerm_resource_group" "example" { + + id = (known after apply) + + location = "centralus" + + name = "rg-cosmosdb-101" + } + +Plan: 7 to add, 0 to change, 0 to destroy. + +Changes to Outputs: + + cosmosdb_account_id = (known after apply) + + cosmosdb_sql_database_id = (known after apply) + + +``` + diff --git a/quickstart/101-cosmos-db-serverside-functionality/main.tf b/quickstart/101-cosmos-db-serverside-functionality/main.tf new file mode 100644 index 00000000..b14bcec6 --- /dev/null +++ b/quickstart/101-cosmos-db-serverside-functionality/main.tf @@ -0,0 +1,87 @@ +resource "azurerm_resource_group" "example" { + name = var.resource_group_name + location = var.location +} + +resource "azurerm_cosmosdb_account" "example" { + name = var.cosmosdb_account_name + location = var.cosmosdb_account_location + resource_group_name = azurerm_resource_group.example.name + offer_type = "Standard" + kind = "GlobalDocumentDB" + enable_automatic_failover = false + geo_location { + location = var.location + failover_priority = 0 + } + + consistency_policy { + consistency_level = "BoundedStaleness" + max_interval_in_seconds = 300 + max_staleness_prefix = 100000 + } + + depends_on = [ + azurerm_resource_group.example + ] +} + +resource "azurerm_cosmosdb_sql_database" "example" { + name = var.cosmosdb_sqldb_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + throughput = var.throughput +} + +resource "azurerm_cosmosdb_sql_container" "example" { + name = var.sql_container_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + database_name = azurerm_cosmosdb_sql_database.example.name + partition_key_path = "/definition/id" + partition_key_version = 1 + throughput = 400 + + indexing_policy { + indexing_mode = "consistent" + + included_path { + path = "/*" + } + + included_path { + path = "/included/?" + } + + excluded_path { + path = "/excluded/?" + } + } + + unique_key { + paths = ["/definition/idlong", "/definition/idshort"] + } +} + +resource "azurerm_cosmosdb_sql_stored_procedure" "example" { + name = var.sql_stored_procedure_name + resource_group_name = azurerm_resource_group.example.name + account_name = azurerm_cosmosdb_account.example.name + database_name = azurerm_cosmosdb_sql_database.example.name + container_name = azurerm_cosmosdb_sql_container.example.name + body = "function () { var context = getContext(); var response = context.getResponse(); response.setBody('Hello, World'); }" +} + +resource "azurerm_cosmosdb_sql_trigger" "example" { + name = var.sql_trigger_name + container_id = azurerm_cosmosdb_sql_container.example.id + body = "function validateToDoItemTimestamp(){var context=getContext();var request=context.getRequest();var itemToCreate=request.getBody();if(!('timestamp'in itemToCreate)){var ts=new Date();itemToCreate['timestamp']=ts.getTime();}request.setBody(itemToCreate);}" + operation = "Create" + type = "Pre" +} + +resource "azurerm_cosmosdb_sql_function" "example" { + name = var.sql_function_name + container_id = azurerm_cosmosdb_sql_container.example.id + body = "function tax(income){if(income==undefined)throw'no input';if(income<1000)return income*0.1;else if(income<10000)return income*0.2;else return income*0.4;}" +} diff --git a/quickstart/101-cosmos-db-serverside-functionality/outputs.tf b/quickstart/101-cosmos-db-serverside-functionality/outputs.tf new file mode 100644 index 00000000..7f78abd8 --- /dev/null +++ b/quickstart/101-cosmos-db-serverside-functionality/outputs.tf @@ -0,0 +1,7 @@ +output "cosmosdb_account_id" { + value = azurerm_cosmosdb_account.example.id +} + +output "cosmosdb_sql_database_id" { + value = azurerm_cosmosdb_sql_database.example.id +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-serverside-functionality/providers.tf b/quickstart/101-cosmos-db-serverside-functionality/providers.tf new file mode 100644 index 00000000..47306772 --- /dev/null +++ b/quickstart/101-cosmos-db-serverside-functionality/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.0.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-cosmos-db-serverside-functionality/variables.tf b/quickstart/101-cosmos-db-serverside-functionality/variables.tf new file mode 100644 index 00000000..7604c89a --- /dev/null +++ b/quickstart/101-cosmos-db-serverside-functionality/variables.tf @@ -0,0 +1,57 @@ +variable "resource_group_name" { + type = string + description = "Resource group name" +} + +variable "location" { + type = string + description = "Resource group location" +} + +variable "cosmosdb_account_name" { + type = string + description = "Cosmos db account name" +} + +variable "cosmosdb_account_location" { + type = string + description = "Cosmos db account location" +} + +variable "cosmosdb_sqldb_name" { + type = string + description = "value" +} + +variable "throughput" { + type = number + description = "Cosmos db database throughput" + validation { + condition = var.throughput >= 400 && var.throughput <= 1000000 + error_message = "Cosmos db manual throughput should be equal to or greater than 400 and less than or equal to 1000000." + } + validation { + condition = var.throughput % 100 == 0 + error_message = "Cosmos db throughput should be in increments of 100." + } +} + +variable "sql_container_name" { + type = string + description = "SQL API container name." +} + +variable "sql_stored_procedure_name" { + type = string + description = "Stored procedure name" +} + +variable "sql_trigger_name" { + type = string + description = "Trigger name" +} + +variable "sql_function_name" { + type = string + description = "User defined function name" +} \ No newline at end of file