keycloak.tf 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # =====================
  2. # Keycloak VM Resources
  3. # =====================
  4. resource "time_sleep" "wait_60_seconds" {
  5. create_duration = "60s"
  6. }
  7. resource "yandex_vpc_network" "default" {
  8. name = "default-vpc"
  9. folder_id = var.folder_id
  10. depends_on = [time_sleep.wait_60_seconds]
  11. }
  12. resource "yandex_vpc_subnet" "vm_subnet" {
  13. v4_cidr_blocks = ["10.2.0.0/16"]
  14. zone = "ru-central1-a"
  15. network_id = "${yandex_vpc_network.default.id}"
  16. folder_id = var.folder_id
  17. }
  18. resource "yandex_vpc_address" "kc_addr" {
  19. name = var.vm_pub_ip_name
  20. folder_id = var.folder_id
  21. external_ipv4_address {
  22. zone_id = yandex_vpc_subnet.vm_subnet.zone
  23. }
  24. }
  25. resource "yandex_dns_recordset" "kc_dns_rec" {
  26. zone_id = data.yandex_dns_zone.dns_zone.id
  27. name = split(".",var.kc_fqdn).0
  28. type = "A"
  29. ttl = 300
  30. data = ["${yandex_vpc_address.kc_addr.external_ipv4_address[0].address}"]
  31. depends_on = [
  32. yandex_vpc_address.kc_addr
  33. ]
  34. }
  35. resource "yandex_vpc_security_group" "keycloak_sg" {
  36. name = "keycloak-sg"
  37. network_id = yandex_vpc_network.default.id
  38. folder_id = var.folder_id
  39. egress {
  40. description = "Permit ALL"
  41. protocol = "ANY"
  42. v4_cidr_blocks = ["0.0.0.0/0"]
  43. }
  44. ingress {
  45. description = "icmp"
  46. protocol = "ICMP"
  47. v4_cidr_blocks = ["0.0.0.0/0"]
  48. }
  49. ingress {
  50. description = "ssh"
  51. protocol = "TCP"
  52. port = 22
  53. v4_cidr_blocks = ["0.0.0.0/0"]
  54. }
  55. ingress {
  56. description = "https"
  57. protocol = "TCP"
  58. port = var.kc_port
  59. v4_cidr_blocks = ["0.0.0.0/0"]
  60. }
  61. }
  62. resource "yandex_compute_instance" "vm_instance" {
  63. name = var.vm_name
  64. hostname = var.vm_name
  65. zone = yandex_vpc_subnet.vm_subnet.zone
  66. folder_id = var.folder_id
  67. resources {
  68. cores = 2
  69. memory = 4
  70. }
  71. boot_disk {
  72. initialize_params {
  73. image_id = data.yandex_compute_image.vm_image.id
  74. }
  75. }
  76. network_interface {
  77. subnet_id = yandex_vpc_subnet.vm_subnet.id
  78. nat = true
  79. nat_ip_address = yandex_vpc_address.kc_addr.external_ipv4_address[0].address
  80. security_group_ids = [
  81. yandex_vpc_security_group.keycloak_sg.id
  82. ]
  83. }
  84. metadata = {
  85. #ssh-keys = "ubuntu:${file("~/.ssh/id_rsa.pub")}"
  86. ssh-keys = "ubuntu:${chomp(tls_private_key.ssh.public_key_openssh)}"
  87. }
  88. # KC provisioning data
  89. provisioner "file" {
  90. destination="kc-data.sh"
  91. content = <<EOF
  92. KC_FQDN=${var.kc_fqdn}
  93. KC_REALM=${var.kc_realm}
  94. KC_VER=${var.kc_ver}
  95. KC_PORT=${var.kc_port}
  96. KC_ADM_USER=${var.kc_adm_user}
  97. KC_ADM_PASS=${var.kc_adm_pass}
  98. KC_CERT_PATH=${var.kc_cert_path}
  99. PG_DB_HOST=${yandex_mdb_postgresql_cluster.pg_cluster.host.0.fqdn}
  100. PG_DB_NAME=${var.pg_db_name}
  101. PG_DB_USER=${var.pg_db_user}
  102. PG_DB_PASS=${var.pg_db_pass}
  103. KC_CERT_PUB="cert-pub-chain.pem"
  104. KC_CERT_PRIV="cert-priv-key.pem"
  105. KC_USERS_FN="kc-users.lst"
  106. EOF
  107. }
  108. # KC provisioning script body
  109. provisioner "file" {
  110. source = "${path.module}/kc-setup.sh"
  111. destination = "kc-setup.sh"
  112. }
  113. # KC LE certificate (public keys chain )
  114. provisioner "file" {
  115. source = "${path.module}/${var.le_cert_pub_key}"
  116. destination = "cert-pub-chain.pem"
  117. }
  118. # KC LE certificate (private key)
  119. provisioner "file" {
  120. source = "${path.module}/${var.le_cert_priv_key}"
  121. destination = "cert-priv-key.pem"
  122. }
  123. # KC User accounts file
  124. provisioner "file" {
  125. source = "${path.module}/${var.kc_user_file}"
  126. destination = "kc-users.lst"
  127. }
  128. # KC realm configuration for the import
  129. provisioner "file" {
  130. destination = "realm.json"
  131. content = templatefile("${path.module}/realm.json", {
  132. realm_name = "${var.kc_realm}"
  133. federation_id = "${yandex_organizationmanager_saml_federation.federation.id}"
  134. })
  135. }
  136. connection {
  137. type = "ssh"
  138. user = "ubuntu"
  139. #private_key = "${file("~/.ssh/id_rsa")}"
  140. private_key = "${tls_private_key.ssh.private_key_pem}"
  141. host = yandex_vpc_address.kc_addr.external_ipv4_address[0].address
  142. }
  143. provisioner "remote-exec" {
  144. inline = [
  145. "sudo bash kc-setup.sh"
  146. ]
  147. }
  148. }
  149. # Работаем с ssh ключем
  150. resource "tls_private_key" "ssh" {
  151. algorithm = "RSA"
  152. rsa_bits = "4096"
  153. }
  154. resource "local_file" "private_key" {
  155. content = tls_private_key.ssh.private_key_pem
  156. filename = "pt_key.pem"
  157. file_permission = "0600"
  158. }