kuttleio / terraform-aws-environment

Kuttle - Blueprint - Main
https://ktl.ai
Apache License 2.0
0 stars 0 forks source link

Feature Request: Add noSQL - MongoDB Atlas #42

Open dspv opened 1 year ago

dspv commented 1 year ago

Story

Just another option to create MongoDB Atlas cluster. I have fully ready components I wanna use

dspv commented 1 year ago

We can use this

locals {
    region_name_bits   = split("-", var.clp_region)
    short_region_name  = "${local.region_name_bits[0]}${substr(local.region_name_bits[1], 0, 1)}${substr(local.region_name_bits[2], 0, 1)}"
    mongodb_region     = "${upper(local.region_name_bits[0])}_${upper(local.region_name_bits[1])}_${upper(local.region_name_bits[2])}" # us-west-2 --> US_WEST_2
    name_prefix        = "${local.short_region_name}-${var.clp_account}"
    standard_tags      = merge(var.global_tags, var.env_tags, tomap({
        Name           = "MongoDB Atlas"
        Service        = "MongoDB Atlas"
    }))

    connection_bits    = split("://", mongodbatlas_cluster.cluster.connection_strings[0].standard_srv)
}

resource mongodbatlas_cluster cluster {
    project_id                      = mongodbatlas_project.atlas.id
    name                            = "${local.name_prefix}-${var.clp_wenv}"
    cluster_type                    = "REPLICASET"
    provider_name                   = "TENANT"
    backing_provider_name           = "AWS"
    provider_region_name            = "EU_WEST_1"
    provider_instance_size_name     = "M0"
    depends_on                      = [mongodbatlas_project.atlas]
}

resource mongodbatlas_project atlas {
    name   = "${local.name_prefix}-${var.clp_wenv}"
    org_id = var.mongodb_atlas_org_id
}

# ---------------------------------------------------
#    Whitelist NAT IPs
# ---------------------------------------------------
resource mongodbatlas_project_ip_access_list vpc_nat_ips {
    project_id = mongodbatlas_project.atlas.id
    cidr_block = "${data.terraform_remote_state.vpc.outputs.nat_public_ips[0]}/32"
    comment    = "NAT Gateway IP from AWS VPC"
}

resource mongodbatlas_project_ip_access_list vpn_ip_list {
    count = length(data.terraform_remote_state.ip_list.outputs.ip_list)
    project_id = mongodbatlas_project.atlas.id
    cidr_block = data.terraform_remote_state.ip_list.outputs.ip_list[count.index]
    comment    = "VPN IP"
}

# ---------------------------------------------------
#    User
# ---------------------------------------------------
resource mongodbatlas_database_user user {
    username           = "${local.name_prefix}-${var.clp_wenv}"
    password           = random_password.user.result
    auth_database_name = "admin"
    project_id         = mongodbatlas_project.atlas.id

    roles {
        role_name     = "readWriteAnyDatabase"
        database_name = "admin"
    }

    roles {
        role_name       = "readWrite"
        database_name   = "${local.name_prefix}-${var.clp_wenv}"
        collection_name = "${local.name_prefix}-${var.clp_wenv}"
    }
}

resource random_password user {
    length  = 24
    special = false    
}
dspv commented 1 year ago

Non-free cluster

resource mongodbatlas_cluster cluster {
    project_id                    = mongodbatlas_project.atlas.id
    name                          = "${local.name_prefix}-${var.clp_wenv}"
    mongo_db_major_version        = "4.4"
    cluster_type                  = "REPLICASET"
    auto_scaling_disk_gb_enabled  = true
    provider_name                 = "AWS"
    provider_instance_size_name   = "M30"

    replication_specs {
        num_shards = 1
        regions_config {
            region_name     = local.mongodb_region
            electable_nodes = 3
            priority        = 7
            read_only_nodes = 0
        }
    }

    depends_on = [mongodbatlas_project.atlas]
}

resource mongodbatlas_project atlas {
    name   = "${local.name_prefix}-${var.clp_wenv}"
    org_id = var.mongodb_atlas_org_id
}