Skip to content

Instantly share code, notes, and snippets.

@thomd
Created May 13, 2021 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomd/e796971ec8bc5b5a8218cfe4821f6e58 to your computer and use it in GitHub Desktop.
Save thomd/e796971ec8bc5b5a8218cfe4821f6e58 to your computer and use it in GitHub Desktop.
Azure CosmosDB with Terraform #az #terraform
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]
}
output "connection_string" {
value = azurerm_cosmosdb_account.acc.connection_strings
sensitive = true
description = "CosmosDB connection string"
}
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