|
| 1 | +# Cosmos db with role definition and assignment |
| 2 | +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). |
| 3 | + |
| 4 | +## Terraform resource types |
| 5 | +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) |
| 6 | +- [azurerm_cosmosdb_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_account) |
| 7 | +- [azurerm_cosmosdb_sql_database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_database) |
| 8 | +- [azurerm_cosmosdb_sql_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_container) |
| 9 | +- [azurerm_cosmosdb_sql_role_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_role_assignment) |
| 10 | +- [azurerm_cosmosdb_sql_role_definition](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_sql_role_definition) |
| 11 | + |
| 12 | +## Variables |
| 13 | + |
| 14 | +| Name | Description | |
| 15 | +|-|-| |
| 16 | +| `resource_group_name` | Resource group name | |
| 17 | +| `resource_group_location` | Resource group location | |
| 18 | +| `cosmosdb_account_name` | Cosmos db account name | |
| 19 | +| `cosmosdb_location` | Cosmos db primary location | |
| 20 | +| `throughput` | DB manual throughput | |
| 21 | +| `sql_container_name` | Name of sql container | |
| 22 | + |
| 23 | +## Example terraform.tfvars file |
| 24 | +``` |
| 25 | +resource_group_name = "rg-cosmosdb-101" |
| 26 | +location = "centralus" |
| 27 | +cosmosdb_account_name = "cosmosdb-dev-centralus-101" |
| 28 | +cosmosdb_account_location = "centralus" |
| 29 | +cosmosdb_sqldb_name = "sqlapidb" |
| 30 | +throughput = 400 |
| 31 | +sql_container_name = "example-container" |
| 32 | +``` |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +```bash |
| 37 | +>terraform plan |
| 38 | + |
| 39 | +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: |
| 40 | + + create |
| 41 | + |
| 42 | +Terraform will perform the following actions: |
| 43 | + |
| 44 | + # azurerm_cosmosdb_account.example will be created |
| 45 | + + resource "azurerm_cosmosdb_account" "example" { |
| 46 | + + access_key_metadata_writes_enabled = true |
| 47 | + + analytical_storage_enabled = false |
| 48 | + + connection_strings = (sensitive value) |
| 49 | + + create_mode = (known after apply) |
| 50 | + + default_identity_type = "FirstPartyIdentity" |
| 51 | + + enable_automatic_failover = false |
| 52 | + + enable_free_tier = false |
| 53 | + + enable_multiple_write_locations = false |
| 54 | + + endpoint = (known after apply) |
| 55 | + + id = (known after apply) |
| 56 | + + is_virtual_network_filter_enabled = false |
| 57 | + + kind = "GlobalDocumentDB" |
| 58 | + + local_authentication_disabled = false |
| 59 | + + location = "centralus" |
| 60 | + + mongo_server_version = (known after apply) |
| 61 | + + name = "cosmosdb-dev-centralus-101" |
| 62 | + + network_acl_bypass_for_azure_services = false |
| 63 | + + offer_type = "Standard" |
| 64 | + + primary_key = (sensitive value) |
| 65 | + + primary_readonly_key = (sensitive value) |
| 66 | + + public_network_access_enabled = true |
| 67 | + + read_endpoints = (known after apply) |
| 68 | + + resource_group_name = "rg-cosmosdb-101" |
| 69 | + + secondary_key = (sensitive value) |
| 70 | + + secondary_readonly_key = (sensitive value) |
| 71 | + + write_endpoints = (known after apply) |
| 72 | + |
| 73 | + + analytical_storage { |
| 74 | + + schema_type = (known after apply) |
| 75 | + } |
| 76 | + |
| 77 | + + backup { |
| 78 | + + interval_in_minutes = (known after apply) |
| 79 | + + retention_in_hours = (known after apply) |
| 80 | + + storage_redundancy = (known after apply) |
| 81 | + + type = (known after apply) |
| 82 | + } |
| 83 | + |
| 84 | + + capabilities { |
| 85 | + + name = (known after apply) |
| 86 | + } |
| 87 | + |
| 88 | + + capacity { |
| 89 | + + total_throughput_limit = (known after apply) |
| 90 | + } |
| 91 | + |
| 92 | + + consistency_policy { |
| 93 | + + consistency_level = "BoundedStaleness" |
| 94 | + + max_interval_in_seconds = 300 |
| 95 | + + max_staleness_prefix = 100000 |
| 96 | + } |
| 97 | + |
| 98 | + + geo_location { |
| 99 | + + failover_priority = 0 |
| 100 | + + id = (known after apply) |
| 101 | + + location = "centralus" |
| 102 | + + zone_redundant = false |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + # azurerm_cosmosdb_sql_container.example will be created |
| 107 | + + resource "azurerm_cosmosdb_sql_container" "example" { |
| 108 | + + account_name = "cosmosdb-dev-centralus-101" |
| 109 | + + database_name = "sqlapidb" |
| 110 | + + default_ttl = (known after apply) |
| 111 | + + id = (known after apply) |
| 112 | + + name = "example-container" |
| 113 | + + partition_key_path = "/definition/id" |
| 114 | + + partition_key_version = 1 |
| 115 | + + resource_group_name = "rg-cosmosdb-101" |
| 116 | + + throughput = 400 |
| 117 | + |
| 118 | + + conflict_resolution_policy { |
| 119 | + + conflict_resolution_path = (known after apply) |
| 120 | + + conflict_resolution_procedure = (known after apply) |
| 121 | + + mode = (known after apply) |
| 122 | + } |
| 123 | + |
| 124 | + + indexing_policy { |
| 125 | + + indexing_mode = "consistent" |
| 126 | + |
| 127 | + + excluded_path { |
| 128 | + + path = "/excluded/?" |
| 129 | + } |
| 130 | + |
| 131 | + + included_path { |
| 132 | + + path = "/*" |
| 133 | + } |
| 134 | + + included_path { |
| 135 | + + path = "/included/?" |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + + unique_key { |
| 140 | + + paths = [ |
| 141 | + + "/definition/idlong", |
| 142 | + + "/definition/idshort", |
| 143 | + ] |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + # azurerm_cosmosdb_sql_database.example will be created |
| 148 | + + resource "azurerm_cosmosdb_sql_database" "example" { |
| 149 | + + account_name = "cosmosdb-dev-centralus-101" |
| 150 | + + id = (known after apply) |
| 151 | + + name = "sqlapidb" |
| 152 | + + resource_group_name = "rg-cosmosdb-101" |
| 153 | + + throughput = 400 |
| 154 | + } |
| 155 | + |
| 156 | + # azurerm_cosmosdb_sql_role_assignment.example will be created |
| 157 | + + resource "azurerm_cosmosdb_sql_role_assignment" "example" { |
| 158 | + + account_name = "cosmosdb-dev-centralus-101" |
| 159 | + + id = (known after apply) |
| 160 | + + name = (known after apply) |
| 161 | + + principal_id = "1b887731-4609-4904-a699-64f06f3d380d" |
| 162 | + + resource_group_name = "rg-cosmosdb-101" |
| 163 | + + role_definition_id = (known after apply) |
| 164 | + + scope = "/subscriptions/aa86e73d-372b-4cd6-a37e-0d12ae93e964/resourceGroups/rg-cosmosdb-101/providers/Microsoft.DocumentDB/databaseAccounts/cosmosdb-dev-centralus-101" |
| 165 | + } |
| 166 | + |
| 167 | + # azurerm_cosmosdb_sql_role_definition.example will be created |
| 168 | + + resource "azurerm_cosmosdb_sql_role_definition" "example" { |
| 169 | + + account_name = "cosmosdb-dev-centralus-101" |
| 170 | + + assignable_scopes = [ |
| 171 | + + "/subscriptions/aa86e73d-372b-4cd6-a37e-0d12ae93e964/resourceGroups/rg-cosmosdb-101/providers/Microsoft.DocumentDB/databaseAccounts/cosmosdb-dev-centralus-101", |
| 172 | + ] |
| 173 | + + id = (known after apply) |
| 174 | + + name = "examplesqlroledef" |
| 175 | + + resource_group_name = "rg-cosmosdb-101" |
| 176 | + + role_definition_id = (known after apply) |
| 177 | + + type = "CustomRole" |
| 178 | + |
| 179 | + + permissions { |
| 180 | + + data_actions = [ |
| 181 | + + "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read", |
| 182 | + ] |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + # azurerm_resource_group.example will be created |
| 187 | + + resource "azurerm_resource_group" "example" { |
| 188 | + + id = (known after apply) |
| 189 | + + location = "centralus" |
| 190 | + + name = "rg-cosmosdb-101" |
| 191 | + } |
| 192 | + |
| 193 | +Plan: 6 to add, 0 to change, 0 to destroy. |
| 194 | + |
| 195 | +Changes to Outputs: |
| 196 | + + cosmosdb_account_id = (known after apply) |
| 197 | + + cosmosdb_sql_database_id = (known after apply) |
| 198 | +``` |
| 199 | + |
0 commit comments