Skip to content

Instantly share code, notes, and snippets.

@ammario
Created June 22, 2022 04:46
Show Gist options
  • Select an option

  • Save ammario/78c9b17ba322a70ffdc70b47a49558f0 to your computer and use it in GitHub Desktop.

Select an option

Save ammario/78c9b17ba322a70ffdc70b47a49558f0 to your computer and use it in GitHub Desktop.

Revisions

  1. ammario created this gist Jun 22, 2022.
    93 changes: 93 additions & 0 deletions template.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    terraform {
    required_providers {
    coder = {
    source = "coder/coder"
    version = "0.3.4"
    }
    google = {
    source = "hashicorp/google"
    version = "~> 4.15"
    }
    }
    }

    variable "project_id" {
    description = "Which Google Compute Project should your workspace live in?"
    }

    variable "zone" {
    description = "What region should your workspace live in?"
    default = "us-central1-a"
    validation {
    condition = contains(["northamerica-northeast1-a", "us-central1-a", "us-west2-c", "europe-west4-b", "southamerica-east1-a"], var.zone)
    error_message = "Invalid zone!"
    }
    }

    provider "google" {
    zone = var.zone
    project = var.project_id
    }

    data "google_compute_default_service_account" "default" {
    }

    data "coder_workspace" "me" {
    }

    resource "google_compute_disk" "root" {
    name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root"
    type = "pd-ssd"
    zone = var.zone
    image = "debian-cloud/debian-9"
    lifecycle {
    ignore_changes = [image]
    }
    }

    resource "coder_agent" "dev" {
    auth = "google-instance-identity"
    arch = "amd64"
    os = "linux"
    }

    resource "google_compute_instance" "dev" {
    zone = var.zone
    count = data.coder_workspace.me.start_count
    name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
    machine_type = "a2-highgpu-1g"
    network_interface {
    network = "default"
    access_config {
    // Ephemeral public IP
    }
    }
    scheduling {
    on_host_maintenance = "TERMINATE"
    }
    boot_disk {
    auto_delete = false
    source = google_compute_disk.root.name
    }
    service_account {
    email = data.google_compute_default_service_account.default.email
    scopes = ["cloud-platform"]
    }
    # The startup script runs as root with no $HOME environment set up, which can break workspace applications, so
    # instead of directly running the agent init script, setup the home directory, write the init script, and then execute
    # it.
    metadata_startup_script = <<EOMETA
    #!/usr/bin/env sh
    set -eux pipefail
    mkdir /root || true
    cat <<'EOCODER' > /root/coder_agent.sh
    ${coder_agent.dev.init_script}
    EOCODER
    chmod +x /root/coder_agent.sh
    export HOME=/root
    /root/coder_agent.sh
    EOMETA
    }