|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + coder = { |
| 4 | + source = "coder/coder" |
| 5 | + } |
| 6 | + oci = { |
| 7 | + source = "oracle/oci" |
| 8 | + } |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +provider "oci" { |
| 13 | + tenancy_ocid = var.tenancy_ocid |
| 14 | + user_ocid = var.user_ocid |
| 15 | + fingerprint = var.fingerprint |
| 16 | + private_key_path = var.private_key_path |
| 17 | + region = var.region |
| 18 | +} |
| 19 | + |
| 20 | +data "coder_workspace" "me" {} |
| 21 | + |
| 22 | +resource "coder_agent" "main" { |
| 23 | + auth = "token" |
| 24 | + arch = length(regexall("A1", var.instance_shape)) > 0 ? "arm64" : "amd64" |
| 25 | + os = "linux" |
| 26 | + startup_script = <<EOT |
| 27 | + #!/bin/bash |
| 28 | + set -euo pipefail |
| 29 | + # Install code-server |
| 30 | + curl -fsSL https://code-server.dev/install.sh | sh |
| 31 | + code-server --auth none --port 13337 >/dev/null 2>&1 & |
| 32 | + EOT |
| 33 | +} |
| 34 | + |
| 35 | +resource "coder_app" "code-server" { |
| 36 | + agent_id = coder_agent.main.id |
| 37 | + slug = "code-server" |
| 38 | + display_name = "code-server" |
| 39 | + url = "http://localhost:13337/?folder=/home/coder" |
| 40 | + icon = "/icon/code.svg" |
| 41 | + subdomain = false |
| 42 | + share = "owner" |
| 43 | + |
| 44 | + healthcheck { |
| 45 | + url = "http://localhost:13337/healthz" |
| 46 | + interval = 5 |
| 47 | + threshold = 6 |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +resource "oci_core_vcn" "main" { |
| 52 | + cidr_block = "10.0.0.0/16" |
| 53 | + compartment_id = var.compartment_ocid |
| 54 | + display_name = "coder-vcn" |
| 55 | +} |
| 56 | + |
| 57 | +resource "oci_core_subnet" "main" { |
| 58 | + cidr_block = "10.0.1.0/24" |
| 59 | + compartment_id = var.compartment_ocid |
| 60 | + vcn_id = oci_core_vcn.main.id |
| 61 | + display_name = "coder-subnet" |
| 62 | + route_table_id = oci_core_route_table.main.id |
| 63 | + security_list_ids = [oci_core_vcn.main.default_security_list_id] |
| 64 | +} |
| 65 | + |
| 66 | +resource "oci_core_internet_gateway" "main" { |
| 67 | + compartment_id = var.compartment_ocid |
| 68 | + vcn_id = oci_core_vcn.main.id |
| 69 | + display_name = "coder-internet-gateway" |
| 70 | +} |
| 71 | + |
| 72 | +resource "oci_core_route_table" "main" { |
| 73 | + compartment_id = var.compartment_ocid |
| 74 | + vcn_id = oci_core_vcn.main.id |
| 75 | + display_name = "coder-route-table" |
| 76 | + |
| 77 | + route_rules { |
| 78 | + destination = "0.0.0.0/0" |
| 79 | + destination_type = "CIDR_BLOCK" |
| 80 | + network_entity_id = oci_core_internet_gateway.main.id |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +data "oci_identity_availability_domains" "ads" { |
| 85 | + compartment_id = var.tenancy_ocid |
| 86 | +} |
| 87 | + |
| 88 | +resource "oci_core_instance" "main" { |
| 89 | + availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name |
| 90 | + compartment_id = var.compartment_ocid |
| 91 | + display_name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}" |
| 92 | + shape = var.instance_shape |
| 93 | + |
| 94 | + dynamic "shape_config" { |
| 95 | + for_each = length(regexall("Flex", var.instance_shape)) > 0 ? [1] : [] |
| 96 | + content { |
| 97 | + memory_in_gbs = 6 |
| 98 | + ocpus = 1 |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + create_vnic_details { |
| 103 | + subnet_id = oci_core_subnet.main.id |
| 104 | + assign_public_ip = true |
| 105 | + } |
| 106 | + |
| 107 | + source_details { |
| 108 | + source_type = "image" |
| 109 | + source_id = var.image_id |
| 110 | + } |
| 111 | + |
| 112 | + metadata = { |
| 113 | + ssh_authorized_keys = coder_agent.main.initial_runner_user_public_key |
| 114 | + user_data = base64encode(coder_agent.main.init_script) // Crucial requirement |
| 115 | + } |
| 116 | +} |
0 commit comments