Skip to content

Commit

Permalink
finalize mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlstrm committed May 9, 2024
1 parent 848d343 commit 4a93996
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
1 change: 1 addition & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ terraform {
source = "hashicorp/tls"
version = "4.0.5"
}
mongodbatlas = {
source = "mongodb/mongodbatlas"
}
}
backend "azurerm" {
container_name = "tfstate"
Expand Down Expand Up @@ -116,23 +113,6 @@ module "common" {
resource_group_location = local.resource_group_location
}

resource "random_password" "mongodb_password" {
length = 32
special = true
}

resource "mongodbatlas_database_user" "database_user" {
username = "cms"
password = random_password.mongodb_password.result
project_id = module.mongodb.project_id
auth_database_name = "admin"

roles {
role_name = "readWrite"
database_name = "cms"
}
}

module "mongodb" {
source = "./modules/mongodb"
mongodb_atlas_public_key = module.keyvault.mongodb_atlas_public_key
Expand Down
27 changes: 25 additions & 2 deletions modules/mongodb/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
}
}
}
provider "mongodbatlas" {
public_key = var.mongodb_atlas_public_key
private_key = var.mongodb_atlas_private_key
Expand Down Expand Up @@ -25,12 +32,28 @@ resource "mongodbatlas_serverless_instance" "serverless_instance" {
provider_settings_backing_provider_name = "AZURE"
provider_settings_provider_name = "SERVERLESS"
provider_settings_region_name = var.atlas_region

auto_indexing = true
auto_indexing = true
}

resource "mongodbatlas_project_ip_access_list" "test" {
project_id = mongodbatlas_project.project.id
cidr_block = "0.0.0.0/0"
comment = "Public access (yolo)"
}

resource "random_password" "mongodb_password" {
length = 32
special = false
}

resource "mongodbatlas_database_user" "database_user" {
username = "cms"
password = random_password.mongodb_password.result
project_id = mongodbatlas_project.project.id
auth_database_name = "admin"

roles {
role_name = "readWrite"
database_name = "cms"
}
}
2 changes: 1 addition & 1 deletion modules/mongodb/output.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "db_connection_string" {
value = mongodbatlas_serverless_instance.serverless_instance.connection_strings_standard_srv
value = "${replace(mongodbatlas_serverless_instance.serverless_instance.connection_strings_standard_srv, "mongodb+srv://", "mongodb+srv://cms:${random_password.mongodb_password.result}@")}/cms?retryWrites=true&w=majority"
sensitive = true
}

0 comments on commit 4a93996

Please sign in to comment.