Skip to content

Commit 72e42ad

Browse files
authored
feat(examples/templates): add aws vm devcontainer template
1 parent 3f6096b commit 72e42ad

File tree

1 file changed

+175
-0
lines changed
  • examples/templates/aws-devcontainer

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
}
6+
aws = {
7+
source = "hashicorp/aws"
8+
}
9+
}
10+
}
11+
12+
module "aws_region" {
13+
source = "https://registry.coder.com/modules/aws-region"
14+
default = "us-east-1"
15+
}
16+
17+
data "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+
49+
provider "aws" {
50+
region = module.aws_region.value
51+
}
52+
53+
data "coder_workspace" "me" {
54+
}
55+
56+
data "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+
69+
data "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+
77+
resource "coder_agent" "dev" {
78+
count = data.coder_workspace.me.start_count
79+
arch = "amd64"
80+
auth = "token"
81+
os = "linux"
82+
dir = "/worskpaces"
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+
101+
locals {
102+
linux_user = "coder"
103+
user_data = <<-EOT
104+
Content-Type: multipart/mixed; boundary="//"
105+
MIME-Version: 1.0
106+
107+
--//
108+
Content-Type: text/cloud-config; charset="us-ascii"
109+
MIME-Version: 1.0
110+
Content-Transfer-Encoding: 7bit
111+
Content-Disposition: attachment; filename="cloud-config.txt"
112+
113+
#cloud-config
114+
cloud_final_modules:
115+
- [scripts-user, always]
116+
hostname: ${lower(data.coder_workspace.me.name)}
117+
users:
118+
- name: ${local.linux_user}
119+
sudo: ALL=(ALL) NOPASSWD:ALL
120+
shell: /bin/bash
121+
122+
--//
123+
Content-Type: text/x-shellscript; charset="us-ascii"
124+
MIME-Version: 1.0
125+
Content-Transfer-Encoding: 7bit
126+
Content-Disposition: attachment; filename="userdata.txt"
127+
128+
#!/bin/bash
129+
# Install Docker
130+
if ! command -v docker &> /dev/null
131+
then
132+
echo "Docker not found, installing..."
133+
curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh 2>&1 >/dev/null
134+
usermod -aG docker ${local.linux_user}
135+
newgrp docker
136+
else
137+
echo "Docker is already installed."
138+
fi
139+
140+
# Start envbuilder
141+
docker run --rm \
142+
-v /tmp/envbuilder:/workspaces \
143+
-e CODER_AGENT_TOKEN="${try(coder_agent.dev[0].token, "")}" \
144+
-e CODER_AGENT_URL="${data.coder_workspace.me.access_url}" \
145+
-e GIT_URL="${data.coder_parameter.repo_url.value}" \
146+
-e INIT_SCRIPT="echo ${base64encode(try(coder_agent.dev[0].init_script, ""))} | base64 -d | sh" \
147+
-e FALLBACK_IMAGE="codercom/enterprise-base:ubuntu" \
148+
ghcr.io/coder/envbuilder
149+
--//--
150+
EOT
151+
}
152+
153+
resource "aws_instance" "vm" {
154+
ami = data.aws_ami.ubuntu.id
155+
availability_zone = "${module.aws_region.value}a"
156+
instance_type = data.coder_parameter.instance_type.value
157+
root_block_device {
158+
volume_size = 30
159+
}
160+
161+
user_data = local.user_data
162+
tags = {
163+
Name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
164+
# Required if you are using our example policy, see template README
165+
Coder_Provisioned = "true"
166+
}
167+
lifecycle {
168+
ignore_changes = [ami]
169+
}
170+
}
171+
172+
resource "aws_ec2_instance_state" "vm" {
173+
instance_id = aws_instance.vm.id
174+
state = data.coder_workspace.me.transition == "start" ? "running" : "stopped"
175+
}

0 commit comments

Comments
 (0)