main.tf 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //Импортим sa
  2. data "yandex_iam_service_account" "bucket_sa" {
  3. service_account_id = var.sa_id
  4. }
  5. //Создаем static key
  6. resource "yandex_iam_service_account_static_access_key" "sa_static_key" {
  7. service_account_id = data.yandex_iam_service_account.bucket_sa.id
  8. description = "static access key for object storage"
  9. }
  10. //Работаем с ssh ключем
  11. resource "tls_private_key" "ssh" {
  12. algorithm = "RSA"
  13. rsa_bits = "4096"
  14. }
  15. resource "local_file" "private_key" {
  16. content = tls_private_key.ssh.private_key_pem
  17. filename = "pt_key.pem"
  18. file_permission = "0600"
  19. }
  20. data "template_file" "cloud_init_lin" {
  21. template = file("../modules/yc-splunk-trail/cloud-init_lin.tpl.yaml")
  22. vars = {
  23. ssh_key = "${chomp(tls_private_key.ssh.public_key_openssh)}"
  24. }
  25. }
  26. //Создаем docker-declaration
  27. data "template_file" "docker-declaration" {
  28. template = file("../modules/yc-splunk-trail/docker-declaration.yaml")
  29. vars = {
  30. SPLUNK_SERVER = "${var.splunk_server}:8088"
  31. S3_BUCKET = "${var.bucket_name}"
  32. S3_FOLDER = "${var.bucket_folder}"
  33. SLEEP_TIME = "300"
  34. SPLUNK_TOKEN_ENCR = "${yandex_kms_secret_ciphertext.encrypted_pass.ciphertext}"
  35. S3_KEY_ENCR = "${yandex_kms_secret_ciphertext.encrypted_s3_key.ciphertext}"
  36. S3_SECRET_ENCR = "${yandex_kms_secret_ciphertext.encrypted_s3_secret.ciphertext}"
  37. KMS_KEY_ID = "${yandex_kms_symmetric_key.key-elk.id}"
  38. }
  39. }
  40. //Развертывание Container-optimised image
  41. data "yandex_compute_image" "container-optimized-image" {
  42. family = "container-optimized-image"
  43. }
  44. resource "yandex_compute_instance" "instance-based-on-coi" {
  45. name = "splunk-sync"
  46. hostname = "splunk-sync"
  47. zone = "ru-central1-a"
  48. service_account_id = data.yandex_iam_service_account.bucket_sa.id
  49. boot_disk {
  50. initialize_params {
  51. image_id = data.yandex_compute_image.container-optimized-image.id
  52. type = "network-ssd"
  53. size = 100
  54. }
  55. }
  56. network_interface {
  57. subnet_id = var.coi_subnet_id
  58. #не забыть включить NAT для subnet, где COI
  59. }
  60. resources {
  61. cores = 4
  62. memory = 4
  63. }
  64. metadata = {
  65. user-data = "${data.template_file.cloud_init_lin.rendered}"
  66. docker-container-declaration = "${data.template_file.docker-declaration.rendered}"
  67. }
  68. }
  69. //Создание KMS ключа
  70. resource "yandex_kms_symmetric_key" "key-elk" {
  71. name = "key-elk"
  72. description = "description for key"
  73. default_algorithm = "AES_128"
  74. }
  75. //Назначение роли на sa на расшифровку ключа
  76. resource "yandex_resourcemanager_folder_iam_binding" "binding" {
  77. folder_id = var.folder_id
  78. role = "kms.keys.encrypterDecrypter"
  79. members = [
  80. "serviceAccount:${data.yandex_iam_service_account.bucket_sa.id}",
  81. ]
  82. }
  83. resource "yandex_kms_secret_ciphertext" "encrypted_pass" {
  84. key_id = yandex_kms_symmetric_key.key-elk.id
  85. plaintext = var.splunk_token
  86. }
  87. resource "yandex_kms_secret_ciphertext" "encrypted_s3_key" {
  88. key_id = yandex_kms_symmetric_key.key-elk.id
  89. plaintext = yandex_iam_service_account_static_access_key.sa_static_key.access_key
  90. }
  91. resource "yandex_kms_secret_ciphertext" "encrypted_s3_secret" {
  92. key_id = yandex_kms_symmetric_key.key-elk.id
  93. plaintext = yandex_iam_service_account_static_access_key.sa_static_key.secret_key
  94. }