TemplatesModules
Back to Templates
AWS EC2 (Devcontainer) Icon

AWS EC2 (Devcontainer)

By:
Provision AWS EC2 VMs with a devcontainer 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
12module "aws_region" {
13  source  = "https://registry.coder.com/modules/aws-region"
14  default = "us-east-1"
15}
16
17data "coder_parameter" "instance_type" {
18  name         = "instance_type"
19  display_name = "Instance type"
20  description  = "What instance type should your workspace use?"
21  default      = "t3.micro"
22  mutable      = false
23  option {
24    name  = "2 vCPU, 1 GiB RAM"
25    value = "t3.micro"
26  }
27  option {
28    name  = "2 vCPU, 2 GiB RAM"
29    value = "t3.small"
30  }
31  option {
32    name  = "2 vCPU, 4 GiB RAM"
33    value = "t3.medium"
34  }
35  option {
36    name  = "2 vCPU, 8 GiB RAM"
37    value = "t3.large"
38  }
39  option {
40    name  = "4 vCPU, 16 GiB RAM"
41    value = "t3.xlarge"
42  }
43  option {
44    name  = "8 vCPU, 32 GiB RAM"
45    value = "t3.2xlarge"
46  }
47}
48
49provider "aws" {
50  region = module.aws_region.value
51}
52
53data "coder_workspace" "me" {
54}
55
56data "aws_ami" "ubuntu" {
57  most_recent = true
58  filter {
59    name   = "name"
60    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
61  }
62  filter {
63    name   = "virtualization-type"
64    values = ["hvm"]
65  }
66  owners = ["099720109477"] # Canonical
67}
68
69data "coder_parameter" "repo_url" {
70  name         = "repo_url"
71  display_name = "Repository URL"
72  default      = "https://github.com/coder/envbuilder-starter-devcontainer"
73  description  = "Repository URL"
74  mutable      = true
75}
76
77resource "coder_agent" "dev" {
78  count              = data.coder_workspace.me.start_count
79  arch               = "amd64"
80  auth               = "token"
81  os                 = "linux"
82  dir                = "/workspaces/${trimsuffix(basename(data.coder_parameter.repo_url.value), ".git")}"
83  connection_timeout = 0
84
85  metadata {
86    key          = "cpu"
87    display_name = "CPU Usage"
88    interval     = 5
89    timeout      = 5
90    script       = "coder stat cpu"
91  }
92  metadata {
93    key          = "memory"
94    display_name = "Memory Usage"
95    interval     = 5
96    timeout      = 5
97    script       = "coder stat mem"
98  }
99}
100
101module "code-server" {
102  count    = data.coder_workspace.me.start_count
103  source   = "https://registry.coder.com/modules/code-server"
104  agent_id = coder_agent.dev[0].id
105}
106
107locals {
108  linux_user = "coder"
109  user_data  = <<-EOT
110  Content-Type: multipart/mixed; boundary="//"
111  MIME-Version: 1.0
112
113  --//
114  Content-Type: text/cloud-config; charset="us-ascii"
115  MIME-Version: 1.0
116  Content-Transfer-Encoding: 7bit
117  Content-Disposition: attachment; filename="cloud-config.txt"
118
119  #cloud-config
120  cloud_final_modules:
121  - [scripts-user, always]
122  hostname: ${lower(data.coder_workspace.me.name)}
123  users:
124  - name: ${local.linux_user}
125    sudo: ALL=(ALL) NOPASSWD:ALL
126    shell: /bin/bash
127
128  --//
129  Content-Type: text/x-shellscript; charset="us-ascii"
130  MIME-Version: 1.0
131  Content-Transfer-Encoding: 7bit
132  Content-Disposition: attachment; filename="userdata.txt"
133
134  #!/bin/bash
135  # Install Docker
136  if ! command -v docker &> /dev/null
137  then
138    echo "Docker not found, installing..."
139    curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh 2>&1 >/dev/null
140    usermod -aG docker ${local.linux_user}
141    newgrp docker
142  else
143    echo "Docker is already installed."
144  fi
145
146  # Start envbuilder
147  docker run --rm \
148    -h ${lower(data.coder_workspace.me.name)} \
149    -v /home/${local.linux_user}/envbuilder:/workspaces \
150    -e CODER_AGENT_TOKEN="${try(coder_agent.dev[0].token, "")}" \
151    -e CODER_AGENT_URL="${data.coder_workspace.me.access_url}" \
152    -e GIT_URL="${data.coder_parameter.repo_url.value}" \
153    -e INIT_SCRIPT="echo ${base64encode(try(coder_agent.dev[0].init_script, ""))} | base64 -d | sh" \
154    -e FALLBACK_IMAGE="codercom/enterprise-base:ubuntu" \
155    ghcr.io/coder/envbuilder
156  --//--
157  EOT
158}
159
160resource "aws_instance" "vm" {
161  ami               = data.aws_ami.ubuntu.id
162  availability_zone = "${module.aws_region.value}a"
163  instance_type     = data.coder_parameter.instance_type.value
164  root_block_device {
165    volume_size = 30
166  }
167
168  user_data = local.user_data
169  tags = {
170    Name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
171    # Required if you are using our example policy, see template README
172    Coder_Provisioned = "true"
173  }
174  lifecycle {
175    ignore_changes = [ami]
176  }
177}
178
179resource "aws_ec2_instance_state" "vm" {
180  instance_id = aws_instance.vm.id
181  state       = data.coder_workspace.me.transition == "start" ? "running" : "stopped"
182}
183