Created
May 13, 2021 20:37
-
-
Save thomd/e796971ec8bc5b5a8218cfe4821f6e58 to your computer and use it in GitHub Desktop.
Azure CosmosDB with Terraform #az #terraform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform { | |
required_providers { | |
azurerm = { | |
source = "hashicorp/azurerm" | |
version = "~> 2.26" | |
} | |
} | |
} | |
provider "azurerm" { | |
features {} | |
} | |
resource "azurerm_resource_group" "resource_group" { | |
name = "${var.project}-${var.environment}-resource-group" | |
location = var.location | |
} | |
resource "azurerm_cosmosdb_account" "acc" { | |
name = "${var.project}${var.environment}cosmosdb" | |
location = var.location | |
resource_group_name = azurerm_resource_group.resource_group.name | |
offer_type = "Standard" | |
kind = "MongoDB" | |
enable_automatic_failover = true | |
capabilities { | |
name = "EnableMongo" | |
} | |
consistency_policy { | |
consistency_level = "BoundedStaleness" | |
max_interval_in_seconds = 400 | |
max_staleness_prefix = 200000 | |
} | |
geo_location { | |
location = var.failover_location | |
failover_priority = 1 | |
} | |
geo_location { | |
location = var.location | |
failover_priority = 0 | |
} | |
} | |
resource "azurerm_cosmosdb_mongo_database" "mongodb" { | |
name = "${var.project}-${var.environment}-mongodb" | |
resource_group_name = azurerm_resource_group.resource_group.name | |
account_name = azurerm_cosmosdb_account.acc.name | |
throughput = 400 | |
} | |
resource "azurerm_cosmosdb_mongo_collection" "coll" { | |
name = "${var.project}-${var.environment}-collection" | |
resource_group_name = azurerm_resource_group.resource_group.name | |
account_name = azurerm_cosmosdb_account.acc.name | |
database_name = azurerm_cosmosdb_mongo_database.mongodb.name | |
default_ttl_seconds = "0" | |
shard_key = "uniqueKey" | |
throughput = 400 | |
lifecycle { | |
ignore_changes = [index] | |
} | |
#depends_on = [azurerm_cosmosdb_mongo_database.mongodb] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
output "connection_string" { | |
value = azurerm_cosmosdb_account.acc.connection_strings | |
sensitive = true | |
description = "CosmosDB connection string" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "project" { | |
type = string | |
description = "Project name" | |
default = "thomd" | |
} | |
variable "environment" { | |
type = string | |
description = "Environment (dev/stage/prod)" | |
default = "dev" | |
} | |
variable "location" { | |
type = string | |
description = "Azure region" | |
default = "eastus" | |
} | |
variable "failover_location" { | |
type = string | |
description = "Azure region for failover" | |
default = "westus" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment