diff --git a/02/hw-02.md b/02/hw-02.md index aafadb13..80d3d4b9 100644 --- a/02/hw-02.md +++ b/02/hw-02.md @@ -30,17 +30,19 @@ 2. Создайте сервисный аккаунт и ключ. [service_account_key_file](https://terraform-provider.yandexcloud.net). 4. Сгенерируйте новый или используйте свой текущий ssh-ключ. Запишите его открытую(public) часть в переменную **vms_ssh_public_root_key**. 5. Инициализируйте проект, выполните код. Исправьте намеренно допущенные синтаксические ошибки. Ищите внимательно, посимвольно. Ответьте, в чём заключается их суть. -6. Подключитесь к консоли ВМ через ssh и выполните команду ``` curl ifconfig.me```. +> В большенстве своём параметры либо не указаны, либо указаны не верно. +7. Подключитесь к консоли ВМ через ssh и выполните команду ``` curl ifconfig.me```. Примечание: К OS ubuntu "out of a box, те из коробки" необходимо подключаться под пользователем ubuntu: ```"ssh ubuntu@vm_ip_address"```. Предварительно убедитесь, что ваш ключ добавлен в ssh-агент: ```eval $(ssh-agent) && ssh-add``` Вы познакомитесь с тем как при создании ВМ создать своего пользователя в блоке metadata в следующей лекции.; 8. Ответьте, как в процессе обучения могут пригодиться параметры ```preemptible = true``` и ```core_fraction=5``` в параметрах ВМ. В качестве решения приложите: -- скриншот ЛК Yandex Cloud с созданной ВМ, где видно внешний ip-адрес; -- скриншот консоли, curl должен отобразить тот же внешний ip-адрес; +- скриншот ЛК Yandex Cloud с созданной ВМ, где видно внешний ip-адрес;\ +![ter-homeworks-02-task1-1](https://github.com/user-attachments/assets/fafd6433-e136-4a02-b293-701592d40cd6) \ +- скриншот консоли, curl должен отобразить тот же внешний ip-адрес;\ +![ter-homeworks-02-task1-2](https://github.com/user-attachments/assets/2100b261-82ed-4ea1-b03f-937dc845ced5) \ - ответы на вопросы. - - +> Прерываемые виртуальные машины, могут быть принудительно остановлены в любой момент. Гарантированная доля CPU, остальное предоставляется по возможности. Эти параметры помогает экономить финансы. ### Задание 2 1. Замените все хардкод-**значения** для ресурсов **yandex_compute_image** и **yandex_compute_instance** на **отдельные** переменные. К названиям переменных ВМ добавьте в начало префикс **vm_web_** . Пример: **vm_web_name**. @@ -61,6 +63,8 @@ 2. Примените изменения. В качестве решения приложите вывод значений ip-адресов команды ```terraform output```. +![ter-homeworks-02-task4](https://github.com/user-attachments/assets/a8bb6758-6090-43b5-a144-d73f2546bff4) + ### Задание 5 diff --git a/02/src/locals.tf b/02/src/locals.tf index e69de29b..3c2adbde 100644 --- a/02/src/locals.tf +++ b/02/src/locals.tf @@ -0,0 +1,3 @@ +locals { + name = "${ var.project }-${ var.env }-platform" +} diff --git a/02/src/main.tf b/02/src/main.tf index 49baf600..70221c15 100644 --- a/02/src/main.tf +++ b/02/src/main.tf @@ -1,24 +1,32 @@ resource "yandex_vpc_network" "develop" { name = var.vpc_name } -resource "yandex_vpc_subnet" "develop" { - name = var.vpc_name - zone = var.default_zone + +resource "yandex_vpc_subnet" "develop-a" { + name = var.name_zone-a + zone = var.default_zone-a network_id = yandex_vpc_network.develop.id - v4_cidr_blocks = var.default_cidr + v4_cidr_blocks = var.default_cidr-zone-a +} +resource "yandex_vpc_subnet" "develop-b" { + name = var.name_zone-b + zone = var.default_zone-b + network_id = yandex_vpc_network.develop.id + v4_cidr_blocks = var.default_cidr-zone-b } - data "yandex_compute_image" "ubuntu" { - family = "ubuntu-2004-lts" + family = var.vm_yandex_compute_image_family } -resource "yandex_compute_instance" "platform" { - name = "netology-develop-platform-web" - platform_id = "standart-v4" + +resource "yandex_compute_instance" "platform-web" { + name = "${local.name}-web" + platform_id = var.vm_web_yandex_compute_instance_platform_id + zone = var.default_zone-a resources { - cores = 1 - memory = 1 - core_fraction = 5 + cores = var.vms_resources.web.cores + memory = var.vms_resources.web.memory + core_fraction = var.vms_resources.web.core_fraction } boot_disk { initialize_params { @@ -29,13 +37,38 @@ resource "yandex_compute_instance" "platform" { preemptible = true } network_interface { - subnet_id = yandex_vpc_subnet.develop.id + subnet_id = yandex_vpc_subnet.develop-a.id nat = true } - metadata = { - serial-port-enable = 1 - ssh-keys = "ubuntu:${var.vms_ssh_root_key}" + serial-port-enable = var.metadata.vm.serial-port-enable + ssh-keys = var.metadata.vm.ssh-keys } +} +resource "yandex_compute_instance" "platform-db" { + name = "${local.name}-db" + platform_id = var.vm_db_yandex_compute_instance_platform_id + zone = var.default_zone-b + resources { + cores = var.vms_resources.db.cores + memory = var.vms_resources.db.memory + core_fraction = var.vms_resources.db.core_fraction + } + boot_disk { + initialize_params { + image_id = data.yandex_compute_image.ubuntu.image_id + } + } + scheduling_policy { + preemptible = true + } + network_interface { + subnet_id = yandex_vpc_subnet.develop-b.id + nat = true + } + metadata = { + serial-port-enable = var.metadata.vm.serial-port-enable + ssh-keys = var.metadata.vm.ssh-keys + } } diff --git a/02/src/outputs.tf b/02/src/outputs.tf index e69de29b..d057b1d0 100644 --- a/02/src/outputs.tf +++ b/02/src/outputs.tf @@ -0,0 +1,28 @@ +output "develop" { + value = [ + { instance_name = [yandex_compute_instance.platform-web.name, yandex_compute_instance.platform-db.name] }, + { external_ip = [yandex_compute_instance.platform-web.network_interface.0.nat_ip_address, yandex_compute_instance.platform-db.network_interface.0.nat_ip_address] }, + { fqdn = [yandex_compute_instance.platform-web.fqdn, yandex_compute_instance.platform-db.fqdn] } + ] +} + +/* +output "platform_web_fqdn" { + value = yandex_compute_instance.platform-web.fqdn +} + +output "platform_web_external_ip" { + value = yandex_compute_instance.platform-web.network_interface.0.nat_ip_address +} + +output "platform_db_instance_name" { + value = yandex_compute_instance.platform-db.name +} + +output "platform_db_fqdn" { + value = yandex_compute_instance.platform-db.fqdn +} + +output "platform_db_external_ip" { + value = yandex_compute_instance.platform-db.network_interface.0.nat_ip_address +*/ diff --git a/02/src/providers.tf b/02/src/providers.tf index fae4dc37..aca7a0db 100644 --- a/02/src/providers.tf +++ b/02/src/providers.tf @@ -11,6 +11,6 @@ provider "yandex" { # token = var.token cloud_id = var.cloud_id folder_id = var.folder_id - zone = var.default_zone - service_account_key_file = file("~/.authorized_key.json") + zone = var.default_zone-a + service_account_key_file = file("~/key.json") } diff --git a/02/src/variables.tf b/02/src/variables.tf index 162cad0e..79bea7b8 100644 --- a/02/src/variables.tf +++ b/02/src/variables.tf @@ -19,6 +19,7 @@ variable "default_zone" { default = "ru-central1-a" description = "https://cloud.yandex.ru/docs/overview/concepts/geo-scope" } + variable "default_cidr" { type = list(string) default = ["10.0.1.0/24"] @@ -32,10 +33,39 @@ variable "vpc_name" { } -###ssh vars +variable "vm_web_yandex_compute_image_family" { + type = string + default = "ubuntu-2004-lts" +} + +variable "vm_web_yandex_compute_instance_name" { + type = string + default = "netology-develop-platform-web" +} + +variable "vm_web_yandex_compute_instance_platform_id" { + type = string + default = "standard-v1" +} +variable "vm_web_yandex_compute_instance_resources_cores" { + type = number + default = 2 +} + +variable "vm_web_yandex_compute_instance_resources_memory" { + type = number + default = 1 +} + +variable "vm_web_yandex_compute_instance_resources_core_fraction" { + type = number + default = 5 +} + +###ssh vars variable "vms_ssh_root_key" { type = string - default = "" + default = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJbFQwlXpyF5D6x8yiptgTG/Are3CfQ94MRINvltKRs2 root@nt.ksob.lan" description = "ssh-keygen -t ed25519" } diff --git a/02/src/vms_platform.tf b/02/src/vms_platform.tf new file mode 100644 index 00000000..9d7f88ec --- /dev/null +++ b/02/src/vms_platform.tf @@ -0,0 +1,152 @@ +###cloud vars +variable "token" { + type = string + description = "OAuth-token; https://cloud.yandex.ru/docs/iam/concepts/authorization/oauth-token" +} +variable "cloud_id" { + type = string + description = "https://cloud.yandex.ru/docs/resource-manager/operations/cloud/get-id" +} +variable "folder_id" { + type = string + description = "https://cloud.yandex.ru/docs/resource-manager/operations/folder/get-id" +} + +variable "vpc_name" { + type = string + default = "develop" + description = "VPC network & subnet name" +} +variable "name_zone-a" { + type = string + default = "develop-ru-central1-a" + description = "https://cloud.yandex.ru/docs/overview/concepts/geo-scope" +} +variable "default_zone-a" { + type = string + default = "ru-central1-a" + description = "https://cloud.yandex.ru/docs/overview/concepts/geo-scope" +} +variable "default_cidr-zone-a" { + type = list(string) + default = ["10.0.1.0/24"] + description = "https://cloud.yandex.ru/docs/vpc/operations/subnet-create" +} + +variable "name_zone-b" { + type = string + default = "develop-ru-central1-b" + description = "https://cloud.yandex.ru/docs/overview/concepts/geo-scope" +} +variable "default_zone-b" { + type = string + default = "ru-central1-b" + description = "https://cloud.yandex.ru/docs/overview/concepts/geo-scope" +} +variable "default_cidr-zone-b" { + type = list(string) + default = ["10.0.2.0/24"] + description = "https://cloud.yandex.ru/docs/vpc/operations/subnet-create" +} + +variable "vm_yandex_compute_image_family" { + type = string + default = "ubuntu-2004-lts" +} + +variable "project" { + type = string + default = "netology" +} +variable "env" { + type = string + default = "develop" +} + +variable "vms_resources" { + type = map(object({ + cores = number + memory = number + core_fraction = number + })) + default = { + "web" = { + cores = 2 + memory = 1 + core_fraction = 5 + } + "db" = { + cores = 2 + memory = 2 + core_fraction = 20 + } + } +} + +variable "vm_web_yandex_compute_instance_name" { + type = string + default = "netology-develop-platform-web" +} +variable "vm_web_yandex_compute_instance_platform_id" { + type = string + default = "standard-v1" +} + +/*variable "vm_web_yandex_compute_instance_resources_cores" { + type = number + default = 2 +} +variable "vm_web_yandex_compute_instance_resources_memory" { + type = number + default = 1 +} +variable "vm_web_yandex_compute_instance_resources_core_fraction" { + type = number + default = 5 +} +*/ + +variable "vm_db_yandex_compute_instance_name" { + type = string + default = "netology-develop-platform-db" +} +variable "vm_db_yandex_compute_instance_platform_id" { + type = string + default = "standard-v1" +} +/*variable "vm_db_yandex_compute_instance_resources_cores" { + type = number + default = 2 +} +variable "vm_db_yandex_compute_instance_resources_memory" { + type = number + default = 2 +} +variable "vm_db_yandex_compute_instance_resources_core_fraction" { + type = number + default = 20 +} +*/ + +variable "metadata" { + type = map(object({ + serial-port-enable = number + ssh-keys = string + })) + default = { + "vm" = { + serial-port-enable = 1 + ssh-keys = "ubuntu:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJbFQwlXpyF5D6x8yiptgTG/Are3CfQ94MRINvltKRs2 root@nt.ksob.lan" + } + } + description = "metadata" +} + +/* +###ssh vars +variable "vms_ssh_root_key" { + type = string + default = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJbFQwlXpyF5D6x8yiptgTG/Are3CfQ94MRINvltKRs2 root@nt.ksob.lan" + description = "ssh-keygen -t ed25519" +} +*/ diff --git a/05/hw-05.md b/05/hw-05.md index 0fbe1e90..b0b67be7 100644 --- a/05/hw-05.md +++ b/05/hw-05.md @@ -31,7 +31,14 @@ - из [демо к лекции 4](https://github.com/netology-code/ter-homeworks/tree/main/04/demonstration1). 2. Проверьте код с помощью tflint и checkov. Вам не нужно инициализировать этот проект. 3. Перечислите, какие **типы** ошибок обнаружены в проекте (без дублей). - +``` +TFLINT +Warning: Missing version constraint for provider "yandex" in `required_providers` +Warning: [Fixable] variable "..." is declared but not used +Checkov +Check: CKV_TF_1: "Ensure Terraform module sources use a commit hash" +Check: CKV_TF_2: "Ensure Terraform module sources use a tag with a version number" +``` ------ ### Задание 2