Skip to content

Commit 7e9b7fb

Browse files
committed
add fly.io example
1 parent 1cc10f2 commit 7e9b7fb

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Coder Docker Image fly.io Template
2+
3+
This template provisions a [code-server](https://github.com/coder/code-server) instance on [fly.io](https://fly.io) using the [codercom/code-server](https://hub.docker.com/r/codercom/code-server) image.
4+
5+
## Prerequisites
6+
7+
- [flyctl](https://fly.io/docs/getting-started/installing-flyctl/) installed.
8+
- [Coder](https://coder.com/) already setup and running with coder-cli installed locally.
9+
10+
## Deploy
11+
12+
1. Clone this repo and cd into `fly-docker-image` directory.
13+
2. Add a secret or environment variable to your Coder deployment with the name `FLY_API_TOKEN` and the value of your fly.io API token.
14+
> This is needed to deploy the workspace to fly.io.
15+
16+
```shell
17+
flyctl auth login
18+
export FLY_API_TOKEN=$(flyctl auth token)
19+
```
20+
21+
Add this to your Coder deployment's environment variables. (e.g. `/etc/coder.d/coder.env`)
22+
23+
Or, If Coder is running as a fly.io app, you can set the secret directly:
24+
25+
```shell
26+
flyctl secrets set FLY_API_TOKEN=$(flyctl auth token) -a <coder-app-name>
27+
# where <coder-app-name> is the name of the Coder app on fly.io
28+
```
29+
30+
> Read our blog [post](coder.com/blog/deploying-coder-on-fly-io) to learn more about how to deploy Coder on fly.io.
31+
32+
3. Run `coder templates create fly-docker-image` to create a template in Coder.
33+
4. Create a new workspace from the template.
34+
35+
This is all. You should now have a code-server instance running on fly.io.
36+
37+
> Note: Change the image and the startup command to suit your needs.
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
resource "fly_app" "workspace" {
2+
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
3+
org = var.fly_org
4+
}
5+
6+
resource "fly_ip" "workspace-ip4" {
7+
app = fly_app.workspace.name
8+
type = "v4"
9+
}
10+
11+
resource "fly_ip" "workspace-ip6" {
12+
app = fly_app.workspace.name
13+
type = "v6"
14+
}
15+
16+
resource "fly_volume" "home-volume" {
17+
app = fly_app.workspace.name
18+
name = "coder_${data.coder_workspace.me.owner}_${lower(data.coder_workspace.me.name)}_home"
19+
size = data.coder_parameter.volume-size.value
20+
region = data.coder_parameter.region.value
21+
}
22+
23+
resource "fly_machine" "workspace" {
24+
count = data.coder_workspace.me.start_count
25+
app = fly_app.workspace.name
26+
region = data.coder_parameter.region.value
27+
name = data.coder_workspace.me.name
28+
image = data.coder_parameter.docker-image.value
29+
cpus = data.coder_parameter.cpu.value
30+
memorymb = data.coder_parameter.memory.value * 1024
31+
env = {
32+
CODER_AGENT_TOKEN = "${coder_agent.main.token}"
33+
}
34+
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "${fly_ip.workspace-ip4.address}")] # replace localhost with the IP of the workspace
35+
services = [
36+
{
37+
ports = [
38+
{
39+
port = 443
40+
handlers = ["tls", "http"]
41+
},
42+
{
43+
port = 80
44+
handlers = ["http"]
45+
}
46+
47+
]
48+
protocol = "tcp",
49+
"internal_port" = 80
50+
},
51+
{
52+
ports = [
53+
{
54+
port = 8080
55+
handlers = ["tls", "http"]
56+
}
57+
]
58+
protocol = "tcp",
59+
"internal_port" = 8080
60+
}
61+
]
62+
mounts = [
63+
{
64+
volume = fly_volume.home-volume.id
65+
path = "/home/coder"
66+
}
67+
]
68+
}
69+
70+
variable "fly_org" {
71+
type = string
72+
default = "coder-409"
73+
description = "The Fly.io organization to deploy the workspace in"
74+
}
75+
76+
data "coder_parameter" "docker-image" {
77+
name = "Docker Image"
78+
description = "The docker image to use for the workspace"
79+
default = "codercom/code-server:latest"
80+
icon = "https://raw.githubusercontent.com/matifali/logos/main/docker.svg"
81+
}
82+
83+
data "coder_parameter" "cpu" {
84+
name = "CPU"
85+
description = "The number of CPUs to allocate to the workspace (1-8)"
86+
type = "number"
87+
default = "1"
88+
icon = "https://raw.githubusercontent.com/matifali/logos/main/cpu-3.svg"
89+
mutable = true
90+
validation {
91+
min = 1
92+
max = 8
93+
}
94+
}
95+
96+
data "coder_parameter" "memory" {
97+
name = "Memory (GB)"
98+
description = "The amount of memory to allocate to the workspace in GB (1-8)"
99+
type = "number"
100+
default = "1"
101+
icon = "/icon/memory.svg"
102+
mutable = true
103+
validation {
104+
min = 1
105+
max = 8
106+
}
107+
}
108+
109+
data "coder_parameter" "volume-size" {
110+
name = "Volume Size"
111+
description = "The size of the volume to create for the workspace in GB (1-20)"
112+
type = "number"
113+
default = "3"
114+
icon = "https://raw.githubusercontent.com/matifali/logos/main/database.svg"
115+
validation {
116+
min = 1
117+
max = 20
118+
}
119+
}
120+
121+
# You can see all available regions here: https://fly.io/docs/reference/regions/
122+
data "coder_parameter" "region" {
123+
name = "Region"
124+
description = "The region to deploy the workspace in"
125+
default = "ams"
126+
icon = "/emojis/1f30e.png"
127+
option {
128+
name = "Amsterdam, Netherlands"
129+
value = "ams"
130+
icon = "/emojis/1f1f3-1f1f1.png"
131+
}
132+
option {
133+
name = "Frankfurt, Germany"
134+
value = "fra"
135+
icon = "/emojis/1f1e9-1f1ea.png"
136+
}
137+
option {
138+
name = "Paris, France"
139+
value = "cdg"
140+
icon = "/emojis/1f1eb-1f1f7.png"
141+
}
142+
option {
143+
name = "Denver, Colorado (US)"
144+
value = "den"
145+
icon = "/emojis/1f1fa-1f1f8.png"
146+
}
147+
option {
148+
name = "Dallas, Texas (US)"
149+
value = "dal"
150+
icon = "/emojis/1f1fa-1f1f8.png"
151+
}
152+
option {
153+
name = "Hong Kong"
154+
value = "hkg"
155+
icon = "/emojis/1f1ed-1f1f0.png"
156+
}
157+
option {
158+
name = "Los Angeles, California (US)"
159+
value = "lax"
160+
icon = "/emojis/1f1fa-1f1f8.png"
161+
}
162+
option {
163+
name = "London, United Kingdom"
164+
value = "lhr"
165+
icon = "/emojis/1f1ec-1f1e7.png"
166+
}
167+
option {
168+
name = "Chennai, India"
169+
value = "maa"
170+
icon = "/emojis/1f1ee-1f1f3.png"
171+
}
172+
option {
173+
name = "Tokyo, Japan"
174+
value = "nrt"
175+
icon = "/emojis/1f1ef-1f1f5.png"
176+
}
177+
option {
178+
name = "Chicago, Illinois (US)"
179+
value = "ord"
180+
icon = "/emojis/1f1fa-1f1f8.png"
181+
}
182+
option {
183+
name = "Seattle, Washington (US)"
184+
value = "sea"
185+
icon = "/emojis/1f1fa-1f1f8.png"
186+
}
187+
option {
188+
name = "Singapore"
189+
value = "sin"
190+
icon = "/emojis/1f1f8-1f1ec.png"
191+
}
192+
option {
193+
name = "Sydney, Australia"
194+
value = "syd"
195+
icon = "/emojis/1f1e6-1f1fa.png"
196+
}
197+
option {
198+
name = "Toronto, Canada"
199+
value = "yyz"
200+
icon = "/emojis/1f1e8-1f1e6.png"
201+
}
202+
}
203+
204+
resource "coder_app" "code-server" {
205+
count = 1
206+
agent_id = coder_agent.main.id
207+
display_name = "Code Server"
208+
slug = "code-server"
209+
url = "http://localhost:8080?folder=/home/coder/"
210+
icon = "/icon/code.svg"
211+
subdomain = false
212+
share = "owner"
213+
214+
healthcheck {
215+
url = "http://localhost:8080/healthz"
216+
interval = 3
217+
threshold = 10
218+
}
219+
}
220+
221+
resource "coder_agent" "main" {
222+
arch = data.coder_provisioner.me.arch
223+
os = "linux"
224+
login_before_ready = false
225+
startup_script_timeout = 180
226+
startup_script = <<-EOT
227+
set -e
228+
# Start code-server
229+
code-server --auth none >/tmp/code-server.log 2>&1 &
230+
# Set the hostname to the workspace name
231+
sudo hostname -b "${data.coder_workspace.me.name}-fly"
232+
EOT
233+
}
234+
235+
data "coder_provisioner" "me" {
236+
}
237+
238+
data "coder_workspace" "me" {
239+
}

0 commit comments

Comments
 (0)