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