Untitled

mail@pastecode.io avatar
unknown
terraform
2 months ago
2.7 kB
2
Indexable
Never
terraform {
  required_providers {
    twc = {
      source = "tf.timeweb.cloud/timeweb-cloud/timeweb-cloud"
    }
  }
  required_version = ">= 0.13"
}

// Timeweb entities
data "twc_projects" "project" {
  name = "Auction"
}

resource "twc_ssh_key" "key" {
  name = "auction-ssh-key"
  body = file("~/.ssh/auction-server.pub")
}

locals {
  ssh = {
    type        = "ssh"
    user        = "root"
    private_key = file("~/.ssh/auction-server")
  }
}

data "twc_configurator" "configurator" {
  location  = "ru-1"
  disk_type = "nvme"
}

data "twc_os" "os" {
  name    = "ubuntu"
  version = "22.04"
}

data "twc_software" "docker-software" {
  name = "Docker"

  os {
    family  = "linux"
    name    = "ubuntu"
    version = "22.04"
  }
}

resource "twc_server" "auction-server" {
  name        = "Auction server"
  project_id  = data.twc_projects.project.id
  os_id       = data.twc_os.os.id
  software_id = data.twc_software.docker-software.id

  ssh_keys_ids = [twc_ssh_key.key.id]

  configuration {
    configurator_id = data.twc_configurator.configurator.id
    disk            = 1024 * 10
    cpu             = 1
    ram             = 1024
  }
}

resource "null_resource" "setup-scripts" {

  depends_on = [twc_server.auction-server, twc_ssh_key.key]
  provisioner "remote-exec" {
    connection {
      host        = twc_server.auction-server.main_ipv4
      type        = local.ssh.type
      user        = local.ssh.user
      private_key = local.ssh.private_key
    }
    inline = ["mkdir -p /app"]
  }

  provisioner "file" {
    connection {
      host        = twc_server.auction-server.main_ipv4
      type        = local.ssh.type
      user        = local.ssh.user
      private_key = local.ssh.private_key
    }
    source      = "./start-app.sh"
    destination = "/app/start-app.sh"
  }

  provisioner "file" {
    connection {
      host        = twc_server.auction-server.main_ipv4
      type        = local.ssh.type
      user        = local.ssh.user
      private_key = local.ssh.private_key
    }
    source      = "./docker-compose.yml"
    destination = "/app/docker-compose.yml"
  }


  provisioner "file" {
    connection {
      host        = twc_server.auction-server.main_ipv4
      type        = local.ssh.type
      user        = local.ssh.user
      private_key = local.ssh.private_key
    }
    source      = "./nginx.conf"
    destination = "/app/nginx.conf"
  }

  provisioner "remote-exec" {
    connection {
      host        = twc_server.auction-server.main_ipv4
      type        = local.ssh.type
      user        = local.ssh.user
      private_key = local.ssh.private_key
    }
    inline = [
      "chmod +x /app/start-app.sh",
      "bash /app/start-app.sh"
    ]
  }
}
Leave a Comment