Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. terraform {
  2. backend "gcs" {
  3. credentials = "credential.json"
  4. bucket = "demo"
  5. prefix = "terraform/state"
  6. }
  7. }
  8.  
  9. provider "google-beta" {
  10. credentials = "${file("credential.json")}"
  11. project = "${var.project}"
  12. region = "${var.region}"
  13. zone = "${var.zone}"
  14. }
  15.  
  16. resource "random_id" "db_name_suffix" {
  17. byte_length = 4
  18. }
  19.  
  20. resource "google_service_networking_connection" "vpc_connection" {
  21. provider = "google-beta"
  22.  
  23. network = "${var.private_network.network}"
  24. reserved_peering_ranges = ["${var.private_network.peering_ranges}"]
  25. service = "${var.private_network.vpc_service}"
  26.  
  27.  
  28. lifecycle {
  29. prevent_destroy = false
  30. }
  31. }
  32.  
  33. variable "database_version" {
  34. type = "string"
  35. default = "MYSQL_5_7"
  36. }
  37.  
  38. resource "google_sql_database_instance" "production" {
  39. provider = "google-beta"
  40.  
  41. name = "demo-${random_id.db_name_suffix.hex}"
  42. database_version = "${var.database_version}"
  43. depends_on = [
  44. "google_service_networking_connection.vpc_connection"
  45. ]
  46.  
  47. settings {
  48. activation_policy = "ALWAYS"
  49. disk_autoresize = true
  50. disk_size = 10
  51. disk_type = "PD_SSD"
  52. tier = "db-n1-standard-1"
  53.  
  54. user_labels = {
  55. project = "demo"
  56. role = "production"
  57. service = "database"
  58. }
  59.  
  60. database_flags {
  61. name = "default_time_zone"
  62. value = "+00:00"
  63. }
  64.  
  65. backup_configuration {
  66. binary_log_enabled = true
  67. enabled = true
  68. start_time = "18:00"
  69. }
  70.  
  71. ip_configuration {
  72. ipv4_enabled = true
  73. private_network = "${var.private_network.network}"
  74. require_ssl = true
  75.  
  76. authorized_networks {
  77. name = "Public"
  78. value = "0.0.0.0/0"
  79. }
  80. }
  81.  
  82. maintenance_window {
  83. day = 7
  84. update_track = "stable"
  85. }
  86. }
  87.  
  88. lifecycle {
  89. prevent_destroy = false
  90. }
  91. }
  92.  
  93. ##########
  94. ##########
  95. resource "google_sql_database" "production" {
  96. provider = "google-beta"
  97.  
  98. name = "demo"
  99. instance = "${google_sql_database_instance.production.name}"
  100. charset = "${var.db_setting.charset}"
  101. collation = "${var.db_setting.collation}"
  102. depends_on = [
  103. "google_sql_database_instance.production"
  104. ]
  105.  
  106. lifecycle {
  107. prevent_destroy = false
  108. }
  109. }
  110.  
  111. ######
  112. ######
  113. resource "google_sql_user" "root" {
  114. provider = "google-beta"
  115.  
  116. name = "root"
  117. instance = "${google_sql_database_instance.production.name}"
  118. password = "123456"
  119. host = "%"
  120. depends_on = [
  121. "google_sql_database_instance.production"
  122. ]
  123. lifecycle {
  124. prevent_destroy = false
  125. }
  126. }
  127. resource "google_sql_ssl_cert" "root_cert" {
  128. provider = "google-beta"
  129.  
  130. common_name = "root"
  131. instance = "${google_sql_database_instance.production.name}"
  132. depends_on = [
  133. "google_sql_database_instance.production"
  134. ]
  135. lifecycle {
  136. prevent_destroy = false
  137. }
  138. }
  139.  
  140.  
  141.  
  142. resource "google_sql_database_instance" "read-replica" {
  143. provider = "google-beta"
  144.  
  145. name = "demo-replica-${random_id.db_name_suffix.hex}"
  146. database_version = "${var.database_version}"
  147. replication_type = "ASYNCHRONOUS"
  148. master_instance_name = "${google_sql_database_instance.production.name}"
  149. depends_on = [
  150. "google_sql_database_instance.production"
  151. ]
  152.  
  153. replica_configuration {
  154. failover_target = false
  155. }
  156.  
  157. settings {
  158. activation_policy = "ALWAYS"
  159. crash_safe_replication = true
  160. replication_type = "ASYNCHRONOUS"
  161. tier = "db-n1-standard-1"
  162.  
  163. user_labels = {
  164. project = "demo"
  165. role = "read-replica"
  166. service = "database"
  167. }
  168.  
  169. database_flags {
  170. name = "default_time_zone"
  171. value = "+00:00"
  172. }
  173.  
  174. backup_configuration {
  175. binary_log_enabled = true
  176. }
  177.  
  178. ip_configuration {
  179. ipv4_enabled = true
  180. private_network = "${var.private_network.network}"
  181. require_ssl = true
  182.  
  183. authorized_networks {
  184. name = "Public"
  185. value = "0.0.0.0/0"
  186. }
  187. }
  188. }
  189.  
  190. lifecycle {
  191. prevent_destroy = false
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement