Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Terraform] Lesson 9: Relational DB – Cloud SQL & Spanner with Terraform

Posted on April 16, 2022April 18, 2022 By nim No Comments on [Terraform] Lesson 9: Relational DB – Cloud SQL & Spanner with Terraform

Contents

Toggle
  • 1) Cloud Spanner with Console
  • 2) Cloud Spanner with Terraform
  • 3) Cloud SQL – Console
  • 4) Cloud SQL with Terraform

1) Cloud Spanner with Console

Các bạn nhờ enable nhé!

Cloud Spanner is a managed, mission-critical, globally consistent and scalable relational database service.

Giờ click create

2) Cloud Spanner with Terraform

Giờ mình cung cấp code terraform
file main.tf

resource "google_spanner_instance" "spanner_tf" {
  name = "spannertf"
  config = "regional-asia-south1"
  display_name = "Spanner from TF"
  num_nodes = 1
  labels = {
    "env" = "learningtf"
  }
  
}


resource "google_spanner_database" "db1"{
    name = "db1"
    instance =  google_spanner_instance.spanner_tf.name
}

giờ bạn terraform init rổi đến apply

Và đương nhiên là chúng ta sẽ gặp lỗi:

Error creating Instance: googleapi: Error 403: Caller is missing IAM permission spanner.instances.create on resource projects/xxxxx

lại vào role kiểm tra và add thêm cho user IAM

giờ thực hiện apply thôi:

Console đã báo thành công
Lên UI kiểm tra.

Chỗ này nếu bạn chạy terraform destroy sẽ bị fail vì mình chưa set deletion_protection=false
Và sẽ bị lỗi bên dưới.
các từ tìm hiểu thêm vào nhé!

Error: cannot destroy instance without setting deletion_protection=false and running `terraform apply`

Còn hông lên UI của GCP xoá thôi.

3) Cloud SQL – Console

Ở đây mình chọn tạo mysql

Giờ chúng ta chọn RAM, CPU, storage, …

Chỗ này bạn chọn style
mỗi style sẽ có định sẵn 1 lượng ram, cpu,… khác nhau
If enabled, whenever you are nearing capacity, storage will be incrementally (and permanently) increased. Learn more
Đây là config network
Chúng ta view 1 chút sem backup có j?

Thôi chúng ta nhấn nut create thôi

Chờ thôi
DB đã tạo xong chúng ta có thể check thử qua IP public
gcloud sql connect mysql-nimtechnology-console --user=root --quiet

ERROR: (gcloud.sql.connect) PERMISSION_DENIED: Cloud SQL Admin API has not been used in project 90059718136 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/sqladmin.googleapis.com/overview?project=90059718136 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

Nếu bạn bị lỗi như ảnh thì click vào đường link trong thông báo.

Hiện thị như dưới là ngon.

GIờ gõ lại command nhé.

4) Cloud SQL with Terraform

resource "google_sql_database_instance" "mysql-from-tf" {
  name = "mysql-from-tf"
  deletion_protection = false
  region = "us-central1"
  
  settings {
    tier = "db-f1-micro"
  }

}

resource "google_sql_user" "myuser" {
  name = "ankit"
  password = "ankit@123"
  instance = google_sql_database_instance.mysql-from-tf.name
}

google_sql_database_instance

Tìm hiểu về tier:
(Required) The machine type to use. See tiers for more details and supported versions. Postgres supports only shared-core machine types, and custom machine types such as db-custom-2-13312. See the Custom Machine Type Documentation to learn about specifying custom machine types.

https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/tiers/list

https://codebeautify.org/jsonviewer

Bạn để ý rằng mỗi tier đã gắn sẵn với một mức RAM, type Database(mysql or sql or …), disk là bao nhiêu!

Ta chỉ cần chọn tier như thế nào là phù hợp
ví dụ mình chọn tier: db-f1-micro

chúng ta cũng đã hiểu tier là gì rồi thì terraform apply thôi

CHúng ta bị lỗi:
Error: Error, failed to create instance mysql-from-tf: googleapi: Error 403: The client is not authorized to make this request., notAuthorized

Giờ apply tiếp.

Đã tạo được mysql 5.6

Còn rất nhiều config chắc các bạn phải tự đọc

GCP - Google Cloud, Terraform

Post navigation

Previous Post: [Terraform] Lesson 8: Cloud PubSub with Terraform
Next Post: [Terraform] Lesson 10: NoSQL Database in GCP with Terraform

More Related Articles

[Terraform/Init] Starting to learn Terraform easily. Terraform
[Terraform] DRY in Terraform Configurations With Dynamic Blocks Terraform
[AWS] – Terraform Beginner – Lesson 1: Create a Free Account of AWS AWS - Amazon Web Service
[Terraform] Manage Secrets in Terraform (2023) Terraform
[Terraform] Online Lab Terraform (FREE). Terraform
[Terraform] Lesson 5: Google Cloud Run with Terraform GCP - Google Cloud

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Laravel] Laravel Helpful June 26, 2025
  • [VScode] Hướng dẫn điều chỉnh font cho terminal June 20, 2025
  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025
  • [Teamcity] How to transfer the value from pipeline A to pipeline B June 9, 2025

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.