TemplatesModules
Back to Templates
AWS EC2 (Linux) Icon

AWS EC2 (Linux)

By:
Provision AWS EC2 VMs as Coder workspaces
Source
README
Resources (2)

Copy and paste the following into main.tf and run coder template push:

1terraform {
2  required_providers {
3    coder = {
4      source = "coder/coder"
5    }
6    aws = {
7      source = "hashicorp/aws"
8    }
9  }
10}
11
12# Last updated 2023-03-14
13# aws ec2 describe-regions | jq -r '[.Regions[].RegionName] | sort'
14data "coder_parameter" "region" {
15  name         = "region"
16  display_name = "Region"
17  description  = "The region to deploy the workspace in."
18  default      = "us-east-1"
19  mutable      = false
20  option {
21    name  = "Asia Pacific (Tokyo)"
22    value = "ap-northeast-1"
23    icon  = "/emojis/1f1ef-1f1f5.png"
24  }
25  option {
26    name  = "Asia Pacific (Seoul)"
27    value = "ap-northeast-2"
28    icon  = "/emojis/1f1f0-1f1f7.png"
29  }
30  option {
31    name  = "Asia Pacific (Osaka)"
32    value = "ap-northeast-3"
33    icon  = "/emojis/1f1ef-1f1f5.png"
34  }
35  option {
36    name  = "Asia Pacific (Mumbai)"
37    value = "ap-south-1"
38    icon  = "/emojis/1f1ee-1f1f3.png"
39  }
40  option {
41    name  = "Asia Pacific (Singapore)"
42    value = "ap-southeast-1"
43    icon  = "/emojis/1f1f8-1f1ec.png"
44  }
45  option {
46    name  = "Asia Pacific (Sydney)"
47    value = "ap-southeast-2"
48    icon  = "/emojis/1f1e6-1f1fa.png"
49  }
50  option {
51    name  = "Canada (Central)"
52    value = "ca-central-1"
53    icon  = "/emojis/1f1e8-1f1e6.png"
54  }
55  option {
56    name  = "EU (Frankfurt)"
57    value = "eu-central-1"
58    icon  = "/emojis/1f1ea-1f1fa.png"
59  }
60  option {
61    name  = "EU (Stockholm)"
62    value = "eu-north-1"
63    icon  = "/emojis/1f1ea-1f1fa.png"
64  }
65  option {
66    name  = "EU (Ireland)"
67    value = "eu-west-1"
68    icon  = "/emojis/1f1ea-1f1fa.png"
69  }
70  option {
71    name  = "EU (London)"
72    value = "eu-west-2"
73    icon  = "/emojis/1f1ea-1f1fa.png"
74  }
75  option {
76    name  = "EU (Paris)"
77    value = "eu-west-3"
78    icon  = "/emojis/1f1ea-1f1fa.png"
79  }
80  option {
81    name  = "South America (São Paulo)"
82    value = "sa-east-1"
83    icon  = "/emojis/1f1e7-1f1f7.png"
84  }
85  option {
86    name  = "US East (N. Virginia)"
87    value = "us-east-1"
88    icon  = "/emojis/1f1fa-1f1f8.png"
89  }
90  option {
91    name  = "US East (Ohio)"
92    value = "us-east-2"
93    icon  = "/emojis/1f1fa-1f1f8.png"
94  }
95  option {
96    name  = "US West (N. California)"
97    value = "us-west-1"
98    icon  = "/emojis/1f1fa-1f1f8.png"
99  }
100  option {
101    name  = "US West (Oregon)"
102    value = "us-west-2"
103    icon  = "/emojis/1f1fa-1f1f8.png"
104  }
105}
106
107data "coder_parameter" "instance_type" {
108  name         = "instance_type"
109  display_name = "Instance type"
110  description  = "What instance type should your workspace use?"
111  default      = "t3.micro"
112  mutable      = false
113  option {
114    name  = "2 vCPU, 1 GiB RAM"
115    value = "t3.micro"
116  }
117  option {
118    name  = "2 vCPU, 2 GiB RAM"
119    value = "t3.small"
120  }
121  option {
122    name  = "2 vCPU, 4 GiB RAM"
123    value = "t3.medium"
124  }
125  option {
126    name  = "2 vCPU, 8 GiB RAM"
127    value = "t3.large"
128  }
129  option {
130    name  = "4 vCPU, 16 GiB RAM"
131    value = "t3.xlarge"
132  }
133  option {
134    name  = "8 vCPU, 32 GiB RAM"
135    value = "t3.2xlarge"
136  }
137}
138
139provider "aws" {
140  region = data.coder_parameter.region.value
141}
142
143data "coder_workspace" "me" {
144}
145data "coder_workspace_owner" "me" {}
146
147data "aws_ami" "ubuntu" {
148  most_recent = true
149  filter {
150    name   = "name"
151    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
152  }
153  filter {
154    name   = "virtualization-type"
155    values = ["hvm"]
156  }
157  owners = ["099720109477"] # Canonical
158}
159
160resource "coder_agent" "dev" {
161  count          = data.coder_workspace.me.start_count
162  arch           = "amd64"
163  auth           = "aws-instance-identity"
164  os             = "linux"
165  startup_script = <<-EOT
166    set -e
167
168    # Install the latest code-server.
169    # Append "--version x.x.x" to install a specific version of code-server.
170    curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server
171
172    # Start code-server in the background.
173    /tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
174  EOT
175
176  metadata {
177    key          = "cpu"
178    display_name = "CPU Usage"
179    interval     = 5
180    timeout      = 5
181    script       = "coder stat cpu"
182  }
183  metadata {
184    key          = "memory"
185    display_name = "Memory Usage"
186    interval     = 5
187    timeout      = 5
188    script       = "coder stat mem"
189  }
190  metadata {
191    key          = "disk"
192    display_name = "Disk Usage"
193    interval     = 600 # every 10 minutes
194    timeout      = 30  # df can take a while on large filesystems
195    script       = "coder stat disk --path $HOME"
196  }
197}
198
199resource "coder_app" "code-server" {
200  count        = data.coder_workspace.me.start_count
201  agent_id     = coder_agent.dev[0].id
202  slug         = "code-server"
203  display_name = "code-server"
204  url          = "http://localhost:13337/?folder=/home/coder"
205  icon         = "/icon/code.svg"
206  subdomain    = false
207  share        = "owner"
208
209  healthcheck {
210    url       = "http://localhost:13337/healthz"
211    interval  = 3
212    threshold = 10
213  }
214}
215
216locals {
217  linux_user = "coder"
218  user_data  = <<-EOT
219  Content-Type: multipart/mixed; boundary="//"
220  MIME-Version: 1.0
221
222  --//
223  Content-Type: text/cloud-config; charset="us-ascii"
224  MIME-Version: 1.0
225  Content-Transfer-Encoding: 7bit
226  Content-Disposition: attachment; filename="cloud-config.txt"
227
228  #cloud-config
229  cloud_final_modules:
230  - [scripts-user, always]
231  hostname: ${lower(data.coder_workspace.me.name)}
232  users:
233  - name: ${local.linux_user}
234    sudo: ALL=(ALL) NOPASSWD:ALL
235    shell: /bin/bash
236
237  --//
238  Content-Type: text/x-shellscript; charset="us-ascii"
239  MIME-Version: 1.0
240  Content-Transfer-Encoding: 7bit
241  Content-Disposition: attachment; filename="userdata.txt"
242
243  #!/bin/bash
244  sudo -u ${local.linux_user} sh -c '${try(coder_agent.dev[0].init_script, "")}'
245  --//--
246  EOT
247}
248
249resource "aws_instance" "dev" {
250  ami               = data.aws_ami.ubuntu.id
251  availability_zone = "${data.coder_parameter.region.value}a"
252  instance_type     = data.coder_parameter.instance_type.value
253
254  user_data = local.user_data
255  tags = {
256    Name = "coder-${data.coder_workspace_owner.me.name}-${data.coder_workspace.me.name}"
257    # Required if you are using our example policy, see template README
258    Coder_Provisioned = "true"
259  }
260  lifecycle {
261    ignore_changes = [ami]
262  }
263}
264
265resource "coder_metadata" "workspace_info" {
266  resource_id = aws_instance.dev.id
267  item {
268    key   = "region"
269    value = data.coder_parameter.region.value
270  }
271  item {
272    key   = "instance type"
273    value = aws_instance.dev.instance_type
274  }
275  item {
276    key   = "disk"
277    value = "${aws_instance.dev.root_block_device[0].volume_size} GiB"
278  }
279}
280
281resource "aws_ec2_instance_state" "dev" {
282  instance_id = aws_instance.dev.id
283  state       = data.coder_workspace.me.transition == "start" ? "running" : "stopped"
284}
285