task_a.tf

Скрипт для задачи A.
 avatar
unknown
terraform
3 months ago
3.7 kB
7
Indexable
terraform {
  required_providers {
    yandex = {
      source = "yandex-cloud/yandex"
    }
  }
}

provider "yandex" {
  token     = var.token
  cloud_id  = var.cloud_id
  folder_id = var.folder_id
  zone      = "ru-central1-a"
}

variable "token" {
  type      = string
  sensitive = true
}

variable "cloud_id" {
  type = string
}

variable "folder_id" {
  type = string
}

data "yandex_compute_image" "ubuntu" {
  family = "ubuntu-2204-lts"
}

resource "yandex_vpc_network" "net" {
  name = "lb-net"
}

resource "yandex_vpc_subnet" "subnet" {
  name           = "lb-subnet"
  zone           = "ru-central1-a"
  network_id     = yandex_vpc_network.net.id
  v4_cidr_blocks = ["10.0.1.0/24"]
}

resource "yandex_iam_service_account" "sa" {
  name = "ig-sa"
}

resource "yandex_resourcemanager_folder_iam_member" "sa_role" {
  folder_id = var.folder_id
  role      = "editor"
  member    = "serviceAccount:${yandex_iam_service_account.sa.id}"
}

resource "yandex_compute_instance_group" "static_vms" {
  name               = "static-group"
  service_account_id = yandex_iam_service_account.sa.id

  instance_template {
    platform_id = "standard-v3"
    resources {
      cores  = 2
      memory = 2
    }
    boot_disk {
      initialize_params {
        image_id = data.yandex_compute_image.ubuntu.id
        size     = 15
      }
    }
    network_interface {
      network_id = yandex_vpc_network.net.id
      subnet_ids = [yandex_vpc_subnet.subnet.id]
      nat        = true
    }
    scheduling_policy {
      preemptible = false
    }
  }

  scale_policy {
    fixed_scale {
      size = 3
    }
  }

  allocation_policy {
    zones = ["ru-central1-a"]
  }

  deploy_policy {
    max_unavailable = 1
    max_expansion   = 1
  }

  load_balancer {
    target_group_name = "static-tg"
  }
}

resource "yandex_compute_instance_group" "preemptible_vms" {
  name               = "preemptible-group"
  service_account_id = yandex_iam_service_account.sa.id

  instance_template {
    platform_id = "standard-v3"
    resources {
      cores  = 2
      memory = 2
    }
    boot_disk {
      initialize_params {
        image_id = data.yandex_compute_image.ubuntu.id
        size     = 15
      }
    }
    network_interface {
      network_id = yandex_vpc_network.net.id
      subnet_ids = [yandex_vpc_subnet.subnet.id]
      nat        = true
    }
    scheduling_policy {
      preemptible = true
    }
  }

  scale_policy {
    auto_scale {
      initial_size           = 1
      min_zone_size          = 0
      max_size               = 3
      measurement_duration   = 120
      warmup_duration        = 120
      stabilization_duration = 180
      cpu_utilization_target = 60
    }
  }

  allocation_policy {
    zones = ["ru-central1-a"]
  }

  deploy_policy {
    max_unavailable = 1
    max_expansion   = 1
  }

  load_balancer {
    target_group_name = "preemptible-tg"
  }
}

resource "yandex_lb_network_load_balancer" "lb" {
  name = "web-lb"

  listener {
    name        = "http"
    port        = 80
    target_port = 80
    protocol    = "tcp"
    external_address_spec {
      ip_version = "ipv4"
    }
  }

  attached_target_group {
    target_group_id = yandex_compute_instance_group.static_vms.load_balancer[0].target_group_id
    healthcheck {
      name = "hc"
      http_options {
        port = 80
        path = "/"
      }
    }
  }

  attached_target_group {
    target_group_id = yandex_compute_instance_group.preemptible_vms.load_balancer[0].target_group_id
    healthcheck {
      name = "hc-pre"
      http_options {
        port = 80
        path = "/"
      }
    }
  }
}

output "lb_ip" {
  value = yandex_lb_network_load_balancer.lb.listener
}
Editor is loading...
Leave a Comment