|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + coder = { |
| 4 | + source = "coder/coder" |
| 5 | + version = "~> 0.2" |
| 6 | + } |
| 7 | + google = { |
| 8 | + source = "hashicorp/google" |
| 9 | + version = "~> 4.15" |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +// variable "image" { |
| 15 | +// description = <<EOF |
| 16 | +// Google machien |
| 17 | +// EOF |
| 18 | +// sensitive = true |
| 19 | +// } |
| 20 | + |
| 21 | +variable "service_account" { |
| 22 | + description = <<EOF |
| 23 | +Coder requires a Google Cloud Service Account to provision workspaces. |
| 24 | +
|
| 25 | +1. Create a service account: |
| 26 | + https://console.cloud.google.com/projectselector/iam-admin/serviceaccounts/create |
| 27 | +2. Add the roles: |
| 28 | + - Compute Admin |
| 29 | + - Service Account User |
| 30 | +3. Click on the created key, and navigate to the "Keys" tab. |
| 31 | +4. Click "Add key", then "Create new key". |
| 32 | +5. Generate a JSON private key, and paste the contents below. |
| 33 | +EOF |
| 34 | + sensitive = true |
| 35 | +} |
| 36 | + |
| 37 | +variable "zone" { |
| 38 | + description = "What region should your workspace live in?" |
| 39 | + default = "us-central1-a" |
| 40 | + validation { |
| 41 | + condition = contains(["northamerica-northeast1-a", "us-central1-a", "us-west2-c", "europe-west4-b", "southamerica-east1-a"], var.zone) |
| 42 | + error_message = "Invalid zone!" |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +provider "google" { |
| 47 | + zone = var.zone |
| 48 | + credentials = var.service_account |
| 49 | + project = jsondecode(var.service_account).project_id |
| 50 | +} |
| 51 | + |
| 52 | +data "coder_workspace" "me" { |
| 53 | +} |
| 54 | + |
| 55 | +data "coder_agent_script" "dev" { |
| 56 | + auth = "google-instance-identity" |
| 57 | + arch = "amd64" |
| 58 | + os = "linux" |
| 59 | +} |
| 60 | + |
| 61 | +data "google_compute_default_service_account" "default" { |
| 62 | +} |
| 63 | + |
| 64 | +resource "google_compute_disk" "home" { |
| 65 | + name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-home" |
| 66 | + type = "pd-ssd" |
| 67 | + zone = var.zone |
| 68 | + size = 20 |
| 69 | + lifecycle { |
| 70 | + ignore_changes = [image] |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +resource "google_compute_instance" "dev" { |
| 75 | + zone = var.zone |
| 76 | + count = data.coder_workspace.me.transition == "start" ? 1 : 0 |
| 77 | + name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}" |
| 78 | + machine_type = "c2d-highcpu-16" |
| 79 | + network_interface { |
| 80 | + network = "default" |
| 81 | + access_config { |
| 82 | + // Ephemeral public IP |
| 83 | + } |
| 84 | + } |
| 85 | + scratch_disk { |
| 86 | + interface = "SCSI" |
| 87 | + } |
| 88 | + boot_disk { |
| 89 | + initialize_params { |
| 90 | + image = "packer-1648759576" |
| 91 | + type = "pd-ssd" |
| 92 | + } |
| 93 | + } |
| 94 | + attached_disk { |
| 95 | + source = google_compute_disk.home.self_link |
| 96 | + } |
| 97 | + service_account { |
| 98 | + email = data.google_compute_default_service_account.default.email |
| 99 | + scopes = ["cloud-platform"] |
| 100 | + } |
| 101 | + metadata = { |
| 102 | + hostname = data.coder_workspace.me.name |
| 103 | + startup-script = <<EOF |
| 104 | +USER=${data.coder_workspace.me.owner} |
| 105 | +useradd -m -s /bin/bash -G sudo -G docker $USER |
| 106 | +sudo -E -u $USER sh -c '${data.coder_agent_script.dev.value}' |
| 107 | +EOF |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +resource "coder_agent" "dev" { |
| 112 | + count = length(google_compute_instance.dev) |
| 113 | + instance_id = google_compute_instance.dev[0].instance_id |
| 114 | +} |
0 commit comments