cockroachdb / terraform-provider-cockroach

Terraform provider for CockroachDB Cloud
Apache License 2.0
56 stars 10 forks source link

Cannot create a `db` using terraform provider #36

Closed aindeev closed 1 year ago

aindeev commented 1 year ago

Any suggestions on how to create a database automatically using terraform? (currently we are just doing this through the UI)

algoflows commented 1 year ago

@aindeev I ran into a few minor issues and ended up finding the solution within the examples directory.

This is taken from one of my projects and its working just fine :)

main.tf

terraform {
  cloud {
    organization = "tec2000"
    hostname     = "app.terraform.io" # Optional; defaults to app.terraform.io

    workspaces {
      tags = ["cli-dev"]
    }
  }

    vercel = {
      source = "vercel/vercel"
    }

    cockroach = {
      source = "cockroachdb/cockroach"
    }
  }
}

# Providers
provider "vercel" {
  api_token = var.vercel_api_token
}

provider "cockroach" {
  apikey = var.cockroach_api_key
}

# Modules
module "vercel" {
  source       = "./modules/vercel"
  project_name = var.project_name
}

module "cockroach" {
  source       = "./modules/cockroach"
  project_name = var.project_name
}

variables.tf


variable "project_name" {
  type        = string
  description = "The name of the project"
  default     = "tec2000"
}

variable "ci_context" {
  type        = string
  description = "The current CI environment"
  default     = "dev"
}

variable "cockroach_api_key" {
  type        = string
  description = "The CockroachDB API key"
}

variable "vercel_api_token" {
  type        = string
  description = "Vercel api token"
}

variable "stripe_secret_key" {
  type        = string
  description = "Stripe api token"
}

/module/cockroach/main.tf

terraform {
  required_providers {
    cockroach = {
      source = "cockroachdb/cockroach"
    }
  }
}

# Available GCP regions: https://cloud.google.com/compute/docs/regions-zones
# Allowed [asia-southeast1, europe-west1, southamerica-east1, us-central1, us-east1, us-west2]
# Only one region is allowed per cluster resource even though the array supports multiple regions

# Seperated the cluster and database resources to allow for more flexibility in the future

resource "cockroach_cluster" "production" {
  name                   = "${var.project_name}-production"
  cloud_provider         = var.cloud_provider
  wait_for_cluster_ready = true
  create_spec = {
    serverless = {
      regions     = ["europe-west1"]
      spend_limit = 30000 #Amount in cents 30,000 = 30 USD
    }
  }
}

resource "cockroach_cluster" "development" {
  name                   = "${var.project_name}-development"
  cloud_provider         = var.cloud_provider
  wait_for_cluster_ready = true
  create_spec = {
    serverless = {
      regions     = ["europe-west1"]
      spend_limit = 1000 #Amount in cents 1000 = 10 USD
    }
  }
}

resource "cockroach_sql_user" "production" {
  name     = var.sql_user_name
  password = var.sql_user_password
  id       = cockroach_cluster.production.id
}

resource "cockroach_sql_user" "development" {
  name     = var.sql_user_name
  password = var.sql_user_password
  id       = cockroach_cluster.development.id
}

/modules/cockroach/variables.tf

variable "project_name" {
  type        = string
  description = "The name of the project"
}

# variable "cluster_name" {
#   type     = string
#   nullable = false
# }

variable "sql_user_name" {
  type     = string
  nullable = false
  default  = "algoflows"
}

variable "sql_user_password" {
  type      = string
  nullable  = false
  sensitive = true
  default   = "012345678910"
}

variable "serverless_spend_limit" {
  type     = number
  nullable = false
  default  = 50
}

variable "cloud_provider" {
  type     = string
  nullable = false
  default  = "GCP"
}

variable "cloud_provider_region" {
  type     = list(any)
  nullable = false
  default  = ["europe-west1"]
}

Results!

Screenshot 2022-10-27 at 17 45 34

Hope this helps :)

marksoper commented 1 year ago

Thanks @aindeev for bringing this up, and thanks @algoflows for helping out. I'm sorry for the late reply on this.

@aindeev you probably mean creating "SQL" databases within the cluster. If so, that's not yet supported in Terraform. We working toward full parity between provider and API, but not there yet. Support for managing databases via Terraform is on the near-term roadmap.