Skip to content

Instantly share code, notes, and snippets.

@k-popov
Last active December 22, 2021 20:06
Show Gist options
  • Save k-popov/73045f22674325897929e45cb69b5fd9 to your computer and use it in GitHub Desktop.
Save k-popov/73045f22674325897929e45cb69b5fd9 to your computer and use it in GitHub Desktop.

Revisions

  1. k-popov revised this gist Dec 22, 2021. 9 changed files with 267 additions and 1 deletion.
    33 changes: 33 additions & 0 deletions bastion.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    resource "yandex_compute_instance" "bastion" {
    name = "bastion"
    hostname = "bastion"
    platform_id = "standard-v1"
    zone = "ru-central1-c"

    labels = {
    group = "bastion-hosts"
    vds = "bastion"
    }

    resources {
    cores = 2
    memory = 1
    core_fraction = 5
    }

    boot_disk {
    initialize_params {
    image_id = var.image_id_bastion
    }
    }

    network_interface {
    subnet_id = yandex_vpc_subnet.subnet-c.id
    nat = true
    security_group_ids = [ yandex_vpc_security_group.sec-group.id ]
    }

    metadata = {
    ssh-keys = "${var.username}:${file(var.public_key_path)}"
    }
    }
    1 change: 0 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    new file
    41 changes: 41 additions & 0 deletions network.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    resource "yandex_vpc_network" "networks" {
    for_each = { "netology-network" = "some text"}
    name = each.key
    }

    variable "subnets" {
    type = map(object({
    zone = string
    v4_cidr_blocks = list(string)
    add_default_route = bool
    }))
    default = {
    "subnet-a" = {
    zone = "ru-central1-a"
    v4_cidr_blocks = ["10.0.1.0/24"]
    add_default_route = true
    },
    "subnet-b" = {
    zone = "ru-central1-b"
    v4_cidr_blocks = ["10.0.2.0/24"]
    add_default_route = true
    }
    }
    }

    resource "yandex_vpc_subnet" "subnets" {
    for_each = var.subnets
    name = each.key
    zone = each.value.zone
    network_id = yandex_vpc_network.networks["netology-network"].id
    v4_cidr_blocks = each.value.v4_cidr_blocks
    route_table_id = each.value.add_default_route ? yandex_vpc_route_table.default_route.id : null
    }

    resource "yandex_vpc_subnet" "subnet-c" {
    name = "subnet-c"
    zone = "ru-central1-c"
    network_id = yandex_vpc_network.networks["netology-network"].id
    v4_cidr_blocks = ["10.0.3.0/24"]
    }

    16 changes: 16 additions & 0 deletions private-variables.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    variable "oauth_token" {
    type = string
    default = "AQAAAAABZgLiAATuwZCa5yregkOCjtEhFlIVi1qU"
    }
    variable "cloud_id" {
    type = string
    default = "b1gbfmcnli0qbnh558o18"
    }
    variable "folder_id" {
    type = string
    default = "b1gcb5c8lbq0p898u2leg"
    }
    variable "service_account_id" {
    type = string
    default = "b1gbfmcnli0qbnh558o38"
    }
    16 changes: 16 additions & 0 deletions provider.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    terraform {
    required_providers {
    yandex = {
    source = "yandex-cloud/yandex"
    version = "0.67.0"
    }
    }
    }

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

    68 changes: 68 additions & 0 deletions security_group.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@

    resource "yandex_vpc_security_group" "sec-group" {
    name = "sec-group"
    network_id = yandex_vpc_network.networks["netology-network"].id
    }


    resource "yandex_vpc_security_group_rule" "rule1" {
    security_group_binding = yandex_vpc_security_group.sec-group.id
    direction = "ingress"
    description = "SSH"
    v4_cidr_blocks = ["0.0.0.0/0"]
    port = 22
    protocol = "TCP"
    }

    resource "yandex_vpc_security_group_rule" "rule2" {
    security_group_binding = yandex_vpc_security_group.sec-group.id
    direction = "ingress"
    description = "web"
    v4_cidr_blocks = ["0.0.0.0/0"]
    port = 80
    protocol = "ANY"
    }

    resource "yandex_vpc_security_group_rule" "rule3" {
    security_group_binding = yandex_vpc_security_group.sec-group.id
    direction = "ingress"
    description = "grafana"
    v4_cidr_blocks = ["0.0.0.0/0"]
    port = 3000
    protocol = "TCP"
    }

    resource "yandex_vpc_security_group_rule" "rule4" {
    security_group_binding = yandex_vpc_security_group.sec-group.id
    direction = "ingress"
    description = "kibana"
    v4_cidr_blocks = ["0.0.0.0/0"]
    port = 5601
    protocol = "TCP"
    }

    resource "yandex_vpc_security_group_rule" "rule5" {
    security_group_binding = yandex_vpc_security_group.sec-group.id
    direction = "ingress"
    description = "Allow any local ingress"
    v4_cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
    protocol = "ANY"
    }

    resource "yandex_vpc_security_group_rule" "rule6" {
    security_group_binding = yandex_vpc_security_group.sec-group.id
    direction = "egress"
    description = "Allow any local egress"
    v4_cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
    protocol = "ANY"
    }

    resource "yandex_vpc_security_group_rule" "rule7" {
    security_group_binding = yandex_vpc_security_group.sec-group.id
    direction = "egress"
    description = "Allow egress traffic"
    v4_cidr_blocks = ["0.0.0.0/0"]
    protocol = "ANY"
    }


    8 changes: 8 additions & 0 deletions static_route_table.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    resource "yandex_vpc_route_table" "default_route" {
    network_id = yandex_vpc_network.networks["netology-network"].id

    static_route {
    destination_prefix = "0.0.0.0/0"
    next_hop_address = yandex_compute_instance.bastion.network_interface.0.ip_address
    }
    }
    20 changes: 20 additions & 0 deletions variables.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    variable "image_id" {
    type = string
    default = "fd8ot0k0vde438jv0t8j"
    }
    variable "image_id_bastion" {
    type = string
    default = "fd8drj7lsj7btotd7et5"
    }
    variable "username" {
    type = string
    default = "ubuntu"
    }
    variable "password" {
    type = string
    default = "qwerty"
    }
    variable "public_key_path" {
    type = string
    default = "~/.ssh/yandex-cloud.pub"
    }
    65 changes: 65 additions & 0 deletions web.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    variable "webservers" {
    type = map(object({
    name = string
    hostname = string
    zone = string
    label = string
    subnet_name = string
    }))
    default = {
    "one" = {
    name = "web1",
    hostname = "web1",
    zone = "ru-central1-a",
    label = "web1",
    subnet_name = "subnet-a"
    },
    "two" = {
    name = "web2",
    hostname = "web2",
    zone = "ru-central1-b",
    label = "web2",
    subnet_name = "subnet-b"
    }
    }
    }

    resource "yandex_compute_instance" "web1" {
    for_each = var.webservers

    name = each.value.name
    hostname = each.value.hostname
    platform_id = "standard-v1"
    zone = each.value.zone

    labels = {
    group = "webservers"
    vds = each.value.label
    }

    resources {
    cores = 2
    memory = 1
    core_fraction = 5
    }

    boot_disk {
    initialize_params {
    image_id = var.image_id
    }
    }

    network_interface {
    subnet_id = yandex_vpc_subnet.subnets[each.value.subnet_name].id
    nat_ip_address = true
    security_group_ids = [ yandex_vpc_security_group.sec-group.id ]
    }

    metadata = {
    ssh-keys = "${var.username}:${file(var.public_key_path)}"
    }

    lifecycle {
    prevent_destroy = "false"
    }
    }
  2. k-popov created this gist Dec 22, 2021.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    new file