google_compute_network

Html code posted by Y
created at 12 Aug 15:13

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
data "google_compute_network" "default" {
  name = lookup(var.gke_network_main, "vpc_name")
}

resource "random_string" "db_password" {
  length  = 16
  special = false
  lower   = true
  upper   = true
}

resource "google_sql_database_instance" "petstore" {
  count            = contains(["dev", "prod"], local.envs) ? 1 : 0
  name             = "petstore"
  database_version = "POSTGRES_14"
  region           = local.us-region

  settings {
    tier              = "db-custom-4-32768"
    availability_type = local.envs != "prod" ? "ZONAL" : "REGIONAL"
    disk_autoresize   = true
    disk_type         = "PD_SSD"

    insights_config {
      record_application_tags = false
      record_client_address   = false
      query_insights_enabled  = true
      query_string_length     = 1024
    }
    database_flags {
      name  = "cloudsql.enable_pg_cron"
      value = "on"
    }
    backup_configuration {
      enabled                        = true
      location                       = local.us-region
      point_in_time_recovery_enabled = true
      transaction_log_retention_days = 1
      backup_retention_settings {
        retained_backups = 7
      }
    }
    ip_configuration {
      ipv4_enabled    = false
      private_network = data.google_compute_network.default.id
    }
  }
}

resource "google_sql_database" "petstore_db" {
  count    = contains(["dev", "prod"], local.envs) ? 1 : 0
  name     = "petstore-db"
  instance = google_sql_database_instance.petstore.0.name
}

resource "google_sql_user" "petstore_user" {
  count    = contains(["dev", "prod"], local.envs) ? 1 : 0
  name     = "petstore-user"
  instance = google_sql_database_instance.petstore.0.name
  password = random_string.db_password.result
}

resource "kubernetes_namespace" "petstore" {
  provider = kubernetes.gke
  metadata {
    name = "petstore"
  }
}

resource "random_uuid" "uuid" {
}

resource "kubernetes_secret" "session_secret" {
  provider = kubernetes.gke
  metadata {
    name      = "session-secret"
    namespace = kubernetes_namespace.petstore.metadata[0].name
  }
  data = {
    key = replace(random_uuid.uuid.result, "-", "")
  }
  type = "Opaque"
}

resource "kubernetes_secret" "sql" {
  provider = kubernetes.gke
  count    = contains(["dev", "prod"], local.envs) ? 1 : 0
  metadata {
    name      = "petstore-db-credentials"
    namespace = kubernetes_namespace.petstore.metadata[0].name
  }
  data = {
    DB_USERNAME = google_sql_user.petstore_user.0.name
    DB_PASSWORD = google_sql_user.petstore_user.0.password
    DB_IP       = google_sql_database_instance.petstore.0.private_ip_address
    DB_NAME     = google_sql_database.petstore_db.0.name
  }
  type = "Opaque"
}

resource "google_storage_bucket" "petstore_artifacts" {
  name          = "petstore-artifacts-${local.envs}"
  location      = "US"
  force_destroy = true
}

locals {
  envs      = var.envs
  us-region = "us-central1"
  gke       = data.terraform_remote_state.core.outputs.gke
}
3.01 KB in 2 ms with coderay