Skip to content

Commit

Permalink
cosmos
Browse files Browse the repository at this point in the history
  • Loading branch information
Pwd9000-ML committed Jan 15, 2024
1 parent 5d889ef commit 8701942
Show file tree
Hide file tree
Showing 10 changed files with 455 additions and 5 deletions.
6 changes: 6 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ data "azurerm_subnet" "subnet" {
virtual_network_name = var.virtual_network_name
resource_group_name = var.network_resource_group_name
}

data "azurerm_cosmosdb_account" "mongo" {
for_each = { for each in var.cosmosdb_name : each.value => each if var.create_cosmosdb == false }
name = each.value
resource_group_name = var.cosmosdb_resource_group_name
}
23 changes: 22 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,28 @@ module "openai_networking" {
tags = var.tags
}

### Create a CosmosDB account running MongoDB to store chat data ###
### Create a CosmosDB account running MongoDB to store chat data (Optional) ###
# 6.) Create a CosmosDB account running MongoDB to store chat data (Optional).
module "openai_cosmosdb" {
count = var.create_cosmosdb ? 1 : 0
source = "./modules/cosmosdb"
cosmosdb_name = join("", var.cosmosdb_name)
cosmosdb_resource_group_name = var.cosmosdb_resource_group_name
location = var.location
cosmosdb_offer_type = var.cosmosdb_offer_type
cosmosdb_kind = var.cosmosdb_kind
cosmosdb_automatic_failover = var.cosmosdb_automatic_failover
use_cosmosdb_free_tier = var.use_cosmosdb_free_tier
cosmosdb_consistency_level = var.cosmosdb_consistency_level
cosmosdb_max_interval_in_seconds = var.cosmosdb_max_interval_in_seconds
cosmosdb_max_staleness_prefix = var.cosmosdb_max_staleness_prefix
geo_locations = var.geo_locations
capabilities = var.capabilities
virtual_network_subnets = var.create_openai_networking == true ? module.openai_networking.subnet_ids[var.cosmosdb_subnet_name].id : data.azurerm_subnet.subnet[var.cosmosdb_subnet_name].id
is_virtual_network_filter_enabled = var.is_virtual_network_filter_enabled
public_network_access_enabled = var.public_network_access_enabled
tags = var.tags
}

### Vreate the Web App ###

Expand Down
10 changes: 10 additions & 0 deletions modules/cosmosdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Module: Azure CosmosDB (Optional)

Create a new CosmosDB. (Optional)
If existing an existing CosmosDB instance is to be used, then the variables/names of the existing DB must be provided as input variables to root the module (data sources):

- Create an Azure CosmosDB instance running MongoDB API.

<!-- BEGIN_TF_DOCS -->

<!-- END_TF_DOCS -->
41 changes: 41 additions & 0 deletions modules/cosmosdb/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
resource "azurerm_cosmosdb_account" "mongo" {
name = var.cosmosdb_name
resource_group_name = var.cosmosdb_resource_group_name
location = var.location
offer_type = var.cosmosdb_offer_type
kind = var.cosmosdb_kind
enable_automatic_failover = var.cosmosdb_automatic_failover
enable_free_tier = var.use_cosmosdb_free_tier
tags = var.tags

consistency_policy {
consistency_level = var.cosmosdb_consistency_level
max_interval_in_seconds = var.cosmosdb_max_interval_in_seconds
max_staleness_prefix = var.cosmosdb_max_staleness_prefix
}

dynamic "geo_location" {
for_each = var.geo_locations
content {
location = geo_location.value.location
failover_priority = geo_location.value.failover_priority
}
}

dynamic "capabilities" {
for_each = var.capabilities
content {
name = capabilities.value
}
}

dynamic "virtual_network_rule" {
for_each = var.virtual_network_subnets
content {
id = virtual_network_rule.value
}
}

is_virtual_network_filter_enabled = var.is_virtual_network_filter_enabled
public_network_access_enabled = var.public_network_access_enabled
}
25 changes: 25 additions & 0 deletions modules/cosmosdb/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
output "cosmosdb_account_id" {
description = "The ID of the Cosmos DB account"
value = azurerm_cosmosdb_account.mongo.id
}

output "cosmosdb_account_endpoint" {
description = "The endpoint of the Cosmos DB account"
value = azurerm_cosmosdb_account.mongo.endpoint
}

output "cosmosdb_account_primary_master_key" {
description = "The primary master key of the Cosmos DB account"
value = azurerm_cosmosdb_account.mongo.primary_key
sensitive = true
}

output "cosmosdb_account_read_endpoints" {
description = "The read endpoints of the Cosmos DB account"
value = azurerm_cosmosdb_account.mongo.read_endpoints
}

output "cosmosdb_account_write_endpoints" {
description = "The write endpoints of the Cosmos DB account"
value = azurerm_cosmosdb_account.mongo.write_endpoints
}
110 changes: 110 additions & 0 deletions modules/cosmosdb/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
variable "cosmosdb_name" {
description = "The name of the Cosmos DB account"
type = string
default = "openaicosmosdb"
}

variable "cosmosdb_resource_group_name" {
description = "The name of the resource group in which to create the Cosmos DB account"
type = string
nullable = false
}

variable "location" {
description = "The location/region in which to create the Cosmos DB account"
type = string
default = "uksouth"
}

variable "cosmosdb_offer_type" {
description = "The offer type to use for the Cosmos DB account"
type = string
default = "Standard"
}

variable "cosmosdb_kind" {
description = "The kind of Cosmos DB to create"
type = string
default = "MongoDB"
}

variable "cosmosdb_automatic_failover" {
description = "Whether to enable automatic failover for the Cosmos DB account"
type = bool
default = false
}

variable "use_cosmosdb_free_tier" {
description = "Whether to enable the free tier for the Cosmos DB account. This needs to be false if another instance already uses free tier."
type = bool
default = true
}

variable "cosmosdb_consistency_level" {
description = "The consistency level of the Cosmos DB account"
type = string
default = "BoundedStaleness"
}

variable "cosmosdb_max_interval_in_seconds" {
description = "The maximum staleness interval in seconds for the Cosmos DB account"
type = number
default = 10
}

variable "cosmosdb_max_staleness_prefix" {
description = "The maximum staleness prefix for the Cosmos DB account"
type = number
default = 200
}

variable "geo_locations" {
description = "The geo-locations for the Cosmos DB account"
type = list(object({
location = string
failover_priority = number
}))
default = [
{
location = "uksouth"
failover_priority = 0
}
]
}

variable "capabilities" {
description = "The capabilities for the Cosmos DB account"
type = list(string)
default = [
"MongoDB"
]
}

variable "virtual_network_subnets" {
description = "The virtual network subnet ID for the Cosmos DB account"
type = list(string)
default = []
}

variable "is_virtual_network_filter_enabled" {
description = "Whether to enable virtual network filtering for the Cosmos DB account"
type = bool
default = true
}

variable "public_network_access_enabled" {
description = "Whether to enable public network access for the Cosmos DB account"
type = bool
default = true
}

variable "tags" {
type = map(string)
default = {
Terraform = "True"
Description = "OpenAI CosmosDB Resource."
Author = "Marcel Lupo"
GitHub = "https://github.com/Pwd9000-ML/terraform-azurerm-openai-private-chatgpt"
}
description = "A map of key value pairs that is used to tag resources created."
}
19 changes: 18 additions & 1 deletion tests/auto_test1/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,27 @@ module "private-chatgpt-openai" {
#Create networking for CosmosDB and Web App (Optional)
create_openai_networking = var.create_openai_networking
network_resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = var.virtual_network_name
virtual_network_name = "${var.virtual_network_name}${random_integer.number.result}"
vnet_address_space = var.vnet_address_space
subnet_config = var.subnet_config

#Create a CosmosDB account running MongoDB to store chat data (Optional)
create_cosmosdb = var.create_cosmosdb
cosmosdb_name = var.cosmosdb_name
cosmosdb_resource_group_name = var.cosmosdb_resource_group_name
cosmosdb_offer_type = var.cosmosdb_offer_type
cosmosdb_kind = var.cosmosdb_kind
cosmosdb_automatic_failover = var.cosmosdb_automatic_failover
use_cosmosdb_free_tier = var.use_cosmosdb_free_tier
cosmosdb_consistency_level = var.cosmosdb_consistency_level
cosmosdb_max_interval_in_seconds = var.cosmosdb_max_interval_in_seconds
cosmosdb_max_staleness_prefix = var.cosmosdb_max_staleness_prefix
geo_locations = var.geo_locations
capabilities = var.capabilities
is_virtual_network_filter_enabled = var.is_virtual_network_filter_enabled
public_network_access_enabled = var.public_network_access_enabled
cosmosdb_subnet_name = var.cosmosdb_subnet_name

#Create a solution log analytics workspace to store logs from our container apps instance
#laws_name = "${var.laws_name}${random_integer.number.result}"
#laws_sku = var.laws_sku
Expand Down
22 changes: 22 additions & 0 deletions tests/auto_test1/testing.auto.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ subnet_config = [
}
]

### cosmosdb ###
create_cosmosdb = true
cosmosdb_name = ["gptcosmos251"]
cosmosdb_resource_group_name = "TF-Module-Automated-Tests-Cognitive-GPT"
cosmosdb_offer_type = "Standard"
cosmosdb_kind = "MongoDB"
cosmosdb_automatic_failover = false
use_cosmosdb_free_tier = true
cosmosdb_consistency_level = "BoundedStaleness"
cosmosdb_max_interval_in_seconds = 10
cosmosdb_max_staleness_prefix = 200
geo_locations = [
{
location = "uksouth"
failover_priority = 0
}
]
capabilities = ["MongoDB"]
is_virtual_network_filter_enabled = true
public_network_access_enabled = true
cosmosdb_subnet_name = "app-cosmos-sub"

### log analytics workspace for container apps ###
#laws_name = "gptlaws"
#laws_sku = "PerGB2018"
Expand Down
100 changes: 100 additions & 0 deletions tests/auto_test1/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,106 @@ variable "subnet_config" {
description = "A list of subnet configuration objects to create subnets in the virtual network."
}

### cosmosdb ###
variable "create_cosmosdb" {
description = "Create a CosmosDB account running MongoDB to store chat data."
type = bool
default = false
}

variable "cosmosdb_name" {
description = "The name of the Cosmos DB account"
type = list(string)
default = ["openaicosmosdb"]
}

variable "cosmosdb_resource_group_name" {
description = "The name of the resource group in which to create the Cosmos DB account"
type = string
nullable = false
}

variable "cosmosdb_offer_type" {
description = "The offer type to use for the Cosmos DB account"
type = string
default = "Standard"
}

variable "cosmosdb_kind" {
description = "The kind of Cosmos DB to create"
type = string
default = "MongoDB"
}

variable "cosmosdb_automatic_failover" {
description = "Whether to enable automatic failover for the Cosmos DB account"
type = bool
default = false
}

variable "use_cosmosdb_free_tier" {
description = "Whether to enable the free tier for the Cosmos DB account. This needs to be false if another instance already uses free tier."
type = bool
default = true
}

variable "cosmosdb_consistency_level" {
description = "The consistency level of the Cosmos DB account"
type = string
default = "BoundedStaleness"
}

variable "cosmosdb_max_interval_in_seconds" {
description = "The maximum staleness interval in seconds for the Cosmos DB account"
type = number
default = 10
}

variable "cosmosdb_max_staleness_prefix" {
description = "The maximum staleness prefix for the Cosmos DB account"
type = number
default = 200
}

variable "geo_locations" {
description = "The geo-locations for the Cosmos DB account"
type = list(object({
location = string
failover_priority = number
}))
default = [
{
location = "uksouth"
failover_priority = 0
}
]
}

variable "capabilities" {
description = "The capabilities for the Cosmos DB account"
type = list(string)
default = [
"MongoDB"
]
}

variable "is_virtual_network_filter_enabled" {
description = "Whether to enable virtual network filtering for the Cosmos DB account"
type = bool
default = true
}

variable "public_network_access_enabled" {
description = "Whether to enable public network access for the Cosmos DB account"
type = bool
default = true
}
variable "cosmosdb_subnet_name" {
description = "The name of the subnet to create the Cosmos DB account in"
type = string
default = "app-cosmos-sub"
}

### log analytics workspace ###
#variable "laws_name" {
# type = string
Expand Down
Loading

0 comments on commit 8701942

Please sign in to comment.