|
| 1 | +resource "google_service_account" "this" { |
| 2 | + account_id = "${var.name}-agent" |
| 3 | + display_name = "Saturn Agent" |
| 4 | + description = "Service account for performing Saturn actions" |
| 5 | +} |
| 6 | + |
| 7 | +resource "google_project_iam_member" "log" { |
| 8 | + project = var.gcp_project |
| 9 | + role = "roles/logging.logWriter" |
| 10 | + member = "serviceAccount:${google_service_account.this.email}" |
| 11 | +} |
| 12 | + |
| 13 | +resource "google_project_iam_member" "monitoring" { |
| 14 | + project = var.gcp_project |
| 15 | + role = "roles/monitoring.metricWriter" |
| 16 | + member = "serviceAccount:${google_service_account.this.email}" |
| 17 | +} |
| 18 | + |
| 19 | +resource "google_secret_manager_secret_iam_member" "this" { |
| 20 | + secret_id = var.secret_id |
| 21 | + role = "roles/secretmanager.secretAccessor" |
| 22 | + member = "serviceAccount:${google_service_account.this.email}" |
| 23 | +} |
| 24 | + |
| 25 | +# Create an isolated VPC for the cpw VM |
| 26 | +resource "google_compute_network" "cpw" { |
| 27 | + name = "${var.name}-vpc" |
| 28 | + auto_create_subnetworks = false |
| 29 | + routing_mode = "REGIONAL" |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +# Create a subnet within the isolated VPC |
| 34 | +resource "google_compute_subnetwork" "this" { |
| 35 | + name = var.name |
| 36 | + ip_cidr_range = var.subnetwork_ip_cidr |
| 37 | + region = var.gcp_region |
| 38 | + network = google_compute_network.cpw.id # Use the new VPC |
| 39 | + stack_type = "IPV4_ONLY" |
| 40 | +} |
| 41 | + |
| 42 | +# Create a static external IP |
| 43 | +resource "google_compute_address" "static" { |
| 44 | + name = "${var.name}-ip" |
| 45 | + region = var.gcp_region |
| 46 | +} |
| 47 | + |
| 48 | +# Allow SSH access |
| 49 | +resource "google_compute_firewall" "ssh" { |
| 50 | + name = "${var.name}-ssh" |
| 51 | + network = google_compute_network.cpw.id |
| 52 | + |
| 53 | + allow { |
| 54 | + protocol = "tcp" |
| 55 | + ports = ["22"] |
| 56 | + } |
| 57 | + |
| 58 | + source_ranges = ["0.0.0.0/0"] # Consider restricting this for security |
| 59 | + target_tags = ["${var.name}-ssh"] |
| 60 | +} |
| 61 | + |
| 62 | +# Allow websocket connections from cpw.battlecode.org |
| 63 | +resource "google_compute_firewall" "websocket" { |
| 64 | + name = "${var.name}-websocket" |
| 65 | + network = google_compute_network.cpw.id |
| 66 | + |
| 67 | + allow { |
| 68 | + protocol = "tcp" |
| 69 | + ports = ["80", "443", "8080", "8001"] # Adjust ports as needed for your websocket server |
| 70 | + } |
| 71 | + |
| 72 | + source_ranges = ["0.0.0.0/0"] |
| 73 | + target_tags = ["${var.name}-websocket"] |
| 74 | +} |
| 75 | + |
| 76 | +module "container" { |
| 77 | + source = "terraform-google-modules/container-vm/google" |
| 78 | + version = "~> 2.0" |
| 79 | + |
| 80 | + container = { |
| 81 | + image = var.image |
| 82 | + args = [ |
| 83 | + ] |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +resource "google_compute_instance" "this" { |
| 88 | + name = var.name |
| 89 | + machine_type = var.machine_type |
| 90 | + zone = var.gcp_zone |
| 91 | + tags = ["${var.name}-ssh", "${var.name}-websocket"] |
| 92 | + labels = var.labels |
| 93 | + |
| 94 | + boot_disk { |
| 95 | + initialize_params { |
| 96 | + image = module.container.source_image |
| 97 | + size = var.disk_size |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + network_interface { |
| 102 | + subnetwork = google_compute_subnetwork.this.name |
| 103 | + |
| 104 | + access_config { |
| 105 | + nat_ip = google_compute_address.static.address |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + service_account { |
| 110 | + email = google_service_account.this.email |
| 111 | + scopes = ["cloud-platform"] |
| 112 | + } |
| 113 | + |
| 114 | + scheduling { |
| 115 | + automatic_restart = false |
| 116 | + on_host_maintenance = "TERMINATE" |
| 117 | + preemptible = true |
| 118 | + } |
| 119 | + |
| 120 | + metadata = { |
| 121 | + gce-container-declaration = module.container.metadata_value |
| 122 | + google-logging-enabled = true |
| 123 | + google-monitoring-enabled = true |
| 124 | + ssh-keys = "ubuntu:${tls_private_key.ssh.public_key_openssh}" |
| 125 | + } |
| 126 | + |
| 127 | + depends_on = [ |
| 128 | + google_secret_manager_secret_iam_member.this, |
| 129 | + ] |
| 130 | +} |
| 131 | + |
| 132 | +resource "tls_private_key" "ssh" { |
| 133 | + algorithm = "RSA" |
| 134 | + rsa_bits = 4096 |
| 135 | +} |
| 136 | + |
| 137 | +# Create a secret for the SSH private key |
| 138 | +resource "google_secret_manager_secret" "ssh_private_key" { |
| 139 | + secret_id = "${var.name}-ssh-private-key" |
| 140 | + |
| 141 | + replication { |
| 142 | + automatic = true |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +# Store the SSH private key in the secret |
| 147 | +resource "google_secret_manager_secret_version" "ssh_private_key" { |
| 148 | + secret = google_secret_manager_secret.ssh_private_key.id |
| 149 | + secret_data = tls_private_key.ssh.private_key_pem |
| 150 | +} |
0 commit comments