|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + aws = { |
| 4 | + source = "hashicorp/aws" |
| 5 | + version = "~> 4.28" |
| 6 | + } |
| 7 | + coder = { |
| 8 | + source = "coder/coder" |
| 9 | + version = "~> 0.4.9" |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +variable "ecs-cluster" { |
| 15 | + description = "Input the ECS cluster ARN to host the workspace" |
| 16 | + default = "" |
| 17 | +} |
| 18 | +variable "cpu" { |
| 19 | + default = "1024" |
| 20 | +} |
| 21 | + |
| 22 | +variable "memory" { |
| 23 | + default = "2048" |
| 24 | +} |
| 25 | + |
| 26 | +# configure AWS provider with creds present on Coder server host |
| 27 | +provider "aws" { |
| 28 | + shared_config_files = ["$HOME/.aws/config"] |
| 29 | + shared_credentials_files = ["$HOME/.aws/credentials"] |
| 30 | +} |
| 31 | + |
| 32 | +# coder workspace, created as an ECS task definition |
| 33 | +resource "aws_ecs_task_definition" "workspace" { |
| 34 | + family = "coder" |
| 35 | + |
| 36 | + requires_compatibilities = ["EC2"] |
| 37 | + cpu = var.cpu |
| 38 | + memory = var.memory |
| 39 | + container_definitions = jsonencode([ |
| 40 | + { |
| 41 | + name = "coder-workspace-${data.coder_workspace.me.id}" |
| 42 | + image = "codercom/enterprise-base:ubuntu" |
| 43 | + cpu = 1024 |
| 44 | + memory = 2048 |
| 45 | + essential = true |
| 46 | + user = "coder" |
| 47 | + command = ["sh", "-c", coder_agent.coder.init_script] |
| 48 | + environment = [ |
| 49 | + { |
| 50 | + "name" = "CODER_AGENT_TOKEN" |
| 51 | + "value" = coder_agent.coder.token |
| 52 | + } |
| 53 | + ] |
| 54 | + mountPoints = [ |
| 55 | + { |
| 56 | + # the name of the volume to mount |
| 57 | + sourceVolume = "home-dir-${data.coder_workspace.me.id}" |
| 58 | + # path on the container to mount the volume at |
| 59 | + containerPath = "/home/coder" |
| 60 | + } |
| 61 | + ] |
| 62 | + portMappings = [ |
| 63 | + { |
| 64 | + containerPort = 80 |
| 65 | + hostPort = 80 |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
| 69 | + ]) |
| 70 | + |
| 71 | + # workspace persistent volume definition |
| 72 | + volume { |
| 73 | + name = "home-dir-${data.coder_workspace.me.id}" |
| 74 | + |
| 75 | + docker_volume_configuration { |
| 76 | + # "shared" ensures that the disk is persisted upon workspace restart |
| 77 | + scope = "shared" |
| 78 | + autoprovision = true |
| 79 | + driver = "local" |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +resource "aws_ecs_service" "workspace" { |
| 85 | + name = "workspace-${data.coder_workspace.me.id}" |
| 86 | + cluster = var.ecs-cluster |
| 87 | + task_definition = aws_ecs_task_definition.workspace.arn |
| 88 | + # scale the service to zero when the workspace is stopped |
| 89 | + desired_count = data.coder_workspace.me.start_count |
| 90 | +} |
| 91 | + |
| 92 | +data "coder_workspace" "me" {} |
| 93 | + |
| 94 | +resource "coder_agent" "coder" { |
| 95 | + arch = "amd64" |
| 96 | + auth = "token" |
| 97 | + os = "linux" |
| 98 | + dir = "/home/coder" |
| 99 | + startup_script = <<EOT |
| 100 | + #!/bin/bash |
| 101 | + # install and start code-server |
| 102 | + curl -fsSL https://code-server.dev/install.sh | sh | tee code-server-install.log |
| 103 | + code-server --auth none --port 13337 | tee code-server-install.log & |
| 104 | + EOT |
| 105 | +} |
| 106 | + |
| 107 | +resource "coder_app" "code-server" { |
| 108 | + agent_id = coder_agent.coder.id |
| 109 | + name = "code-server" |
| 110 | + icon = "/icon/code.svg" |
| 111 | + url = "http://localhost:13337?folder=/home/coder" |
| 112 | + relative_path = true |
| 113 | +} |
0 commit comments