@@ -2,7 +2,7 @@ terraform {
2
2
required_providers {
3
3
coder = {
4
4
source = " coder/coder"
5
- version = " ~> 0.7 .0"
5
+ version = " ~> 0.11 .0"
6
6
}
7
7
aws = {
8
8
source = " hashicorp/aws"
@@ -30,24 +30,24 @@ data "coder_parameter" "region" {
30
30
icon = " /emojis/1f1f0-1f1f7.png"
31
31
}
32
32
option {
33
- name = " Asia Pacific (Osaka-Local )"
33
+ name = " Asia Pacific (Osaka)"
34
34
value = " ap-northeast-3"
35
- icon = " /emojis/1f1f0-1f1f7 .png"
35
+ icon = " /emojis/1f1ef-1f1f5 .png"
36
36
}
37
37
option {
38
38
name = " Asia Pacific (Mumbai)"
39
39
value = " ap-south-1"
40
- icon = " /emojis/1f1f0-1f1f7 .png"
40
+ icon = " /emojis/1f1ee-1f1f3 .png"
41
41
}
42
42
option {
43
43
name = " Asia Pacific (Singapore)"
44
44
value = " ap-southeast-1"
45
- icon = " /emojis/1f1f0-1f1f7 .png"
45
+ icon = " /emojis/1f1f8-1f1ec .png"
46
46
}
47
47
option {
48
48
name = " Asia Pacific (Sydney)"
49
49
value = " ap-southeast-2"
50
- icon = " /emojis/1f1f0-1f1f7 .png"
50
+ icon = " /emojis/1f1e6-1f1fa .png"
51
51
}
52
52
option {
53
53
name = " Canada (Central)"
@@ -176,33 +176,21 @@ resource "coder_agent" "main" {
176
176
display_name = " CPU Usage"
177
177
interval = 5
178
178
timeout = 5
179
- script = <<- EOT
180
- #!/bin/bash
181
- set -e
182
- top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4 "%"}'
183
- EOT
179
+ script = " coder stat cpu"
184
180
}
185
181
metadata {
186
182
key = " memory"
187
183
display_name = " Memory Usage"
188
184
interval = 5
189
185
timeout = 5
190
- script = <<- EOT
191
- #!/bin/bash
192
- set -e
193
- free -m | awk 'NR==2{printf "%.2f%%\t", $3*100/$2 }'
194
- EOT
186
+ script = " coder stat mem"
195
187
}
196
188
metadata {
197
189
key = " disk"
198
190
display_name = " Disk Usage"
199
191
interval = 600 # every 10 minutes
200
192
timeout = 30 # df can take a while on large filesystems
201
- script = <<- EOT
202
- #!/bin/bash
203
- set -e
204
- df /home/coder | awk '$NF=="/"{printf "%s", $5}'
205
- EOT
193
+ script = " coder stat disk --path $HOME"
206
194
}
207
195
}
208
196
@@ -223,11 +211,9 @@ resource "coder_app" "code-server" {
223
211
}
224
212
225
213
locals {
226
-
227
- # User data is used to stop/start AWS instances. See:
228
- # https://github.com/hashicorp/terraform-provider-aws/issues/22
229
-
230
- user_data_start = << EOT
214
+ linux_user = " coder" # Ensure this user/group does not exist in your VM image
215
+ # User data is used to run the init_script
216
+ user_data = << EOT
231
217
Content-Type: multipart/mixed; boundary="//"
232
218
MIME-Version: 1.0
233
219
@@ -256,42 +242,14 @@ Content-Disposition: attachment; filename="userdata.txt"
256
242
sudo -u ${ local . linux_user } sh -c '${ coder_agent . main . init_script } '
257
243
--//--
258
244
EOT
259
-
260
- user_data_end = << EOT
261
- Content-Type: multipart/mixed; boundary="//"
262
- MIME-Version: 1.0
263
-
264
- --//
265
- Content-Type: text/cloud-config; charset="us-ascii"
266
- MIME-Version: 1.0
267
- Content-Transfer-Encoding: 7bit
268
- Content-Disposition: attachment; filename="cloud-config.txt"
269
-
270
- #cloud-config
271
- cloud_final_modules:
272
- - [scripts-user, always]
273
-
274
- --//
275
- Content-Type: text/x-shellscript; charset="us-ascii"
276
- MIME-Version: 1.0
277
- Content-Transfer-Encoding: 7bit
278
- Content-Disposition: attachment; filename="userdata.txt"
279
-
280
- #!/bin/bash
281
- sudo shutdown -h now
282
- --//--
283
- EOT
284
-
285
- linux_user = " coder" # Ensure this user/group does not exist in your VM image
286
-
287
245
}
288
246
289
247
resource "aws_instance" "dev" {
290
248
ami = data. aws_ami . ubuntu . id
291
249
availability_zone = " ${ data . coder_parameter . region . value } a"
292
250
instance_type = data. coder_parameter . instance_type . value
293
251
294
- user_data = data . coder_workspace . me . transition == " start " ? local. user_data_start : local . user_data_end
252
+ user_data = local. user_data_start
295
253
tags = {
296
254
Name = " coder-${ data . coder_workspace . me . owner } -${ data . coder_workspace . me . name } "
297
255
# Required if you are using our example policy, see template README
@@ -314,3 +272,8 @@ resource "coder_metadata" "workspace_info" {
314
272
value = " ${ aws_instance . dev . root_block_device [0 ]. volume_size } GiB"
315
273
}
316
274
}
275
+
276
+ resource "aws_ec2_instance_state" "dev" {
277
+ instance_id = aws_instance. dev . id
278
+ state = data. coder_workspace . me . transition == " start" ? " running" : " stopped"
279
+ }
0 commit comments