Skip to content

Commit 9c5b879

Browse files
ericpaulsenbpmct
andauthored
add: ECS example template (#3915)
* add: ECS example template * fix: empty main.tf * cleanup * rm: cluster & compute * set CPU & memory vars Co-authored-by: Ben Potter <ben@coder.com> Co-authored-by: Ben Potter <ben@coder.com>
1 parent 2c41343 commit 9c5b879

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: Develop in an ECS-hosted container
3+
description: Get started with Linux development on AWS ECS.
4+
tags: [cloud, aws]
5+
---
6+
7+
# aws-ecs
8+
9+
This is a sample template for running a Coder workspace on ECS. It assumes there
10+
is a pre-existing ECS cluster with EC2-based compute to host the workspace.
11+
12+
## Architecture
13+
14+
This workspace is built using the following AWS resources:
15+
16+
- Task definition - the container definition, includes the image, command, volume(s)
17+
- ECS service - manages the task definition
18+
19+
## code-server
20+
21+
`code-server` is installed via the `startup_script` argument in the `coder_agent`
22+
resource block. The `coder_app` resource is defined to access `code-server` through
23+
the dashboard UI over `localhost:13337`.
+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)