Skip to content

Commit ec208b5

Browse files
added windows eng-dev dcv template
1 parent 7c10abd commit ec208b5

File tree

3 files changed

+348
-0
lines changed

3 files changed

+348
-0
lines changed

templates/vm-aws-base-windows/main.tf

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
terraform {
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'
14+
data "coder_parameter" "region" {
15+
name = "region"
16+
display_name = "Region"
17+
description = "The region to deploy the workspace in."
18+
default = "us-east-2"
19+
mutable = false
20+
option {
21+
name = "US East (N. Virginia)"
22+
value = "us-east-1"
23+
icon = "/emojis/1f1fa-1f1f8.png"
24+
}
25+
option {
26+
name = "US East (Ohio)"
27+
value = "us-east-2"
28+
icon = "/emojis/1f1fa-1f1f8.png"
29+
}
30+
option {
31+
name = "US West (N. California)"
32+
value = "us-west-1"
33+
icon = "/emojis/1f1fa-1f1f8.png"
34+
}
35+
option {
36+
name = "US West (Oregon)"
37+
value = "us-west-2"
38+
icon = "/emojis/1f1fa-1f1f8.png"
39+
}
40+
option {
41+
name = "Asia Pacific (Tokyo)"
42+
value = "ap-northeast-1"
43+
icon = "/emojis/1f1ef-1f1f5.png"
44+
}
45+
option {
46+
name = "Asia Pacific (Seoul)"
47+
value = "ap-northeast-2"
48+
icon = "/emojis/1f1f0-1f1f7.png"
49+
}
50+
option {
51+
name = "Asia Pacific (Osaka-Local)"
52+
value = "ap-northeast-3"
53+
icon = "/emojis/1f1f0-1f1f7.png"
54+
}
55+
option {
56+
name = "Asia Pacific (Mumbai)"
57+
value = "ap-south-1"
58+
icon = "/emojis/1f1f0-1f1f7.png"
59+
}
60+
option {
61+
name = "Asia Pacific (Singapore)"
62+
value = "ap-southeast-1"
63+
icon = "/emojis/1f1f0-1f1f7.png"
64+
}
65+
option {
66+
name = "Asia Pacific (Sydney)"
67+
value = "ap-southeast-2"
68+
icon = "/emojis/1f1f0-1f1f7.png"
69+
}
70+
option {
71+
name = "Canada (Central)"
72+
value = "ca-central-1"
73+
icon = "/emojis/1f1e8-1f1e6.png"
74+
}
75+
option {
76+
name = "EU (Frankfurt)"
77+
value = "eu-central-1"
78+
icon = "/emojis/1f1ea-1f1fa.png"
79+
}
80+
option {
81+
name = "EU (Stockholm)"
82+
value = "eu-north-1"
83+
icon = "/emojis/1f1ea-1f1fa.png"
84+
}
85+
option {
86+
name = "EU (Ireland)"
87+
value = "eu-west-1"
88+
icon = "/emojis/1f1ea-1f1fa.png"
89+
}
90+
option {
91+
name = "EU (London)"
92+
value = "eu-west-2"
93+
icon = "/emojis/1f1ea-1f1fa.png"
94+
}
95+
option {
96+
name = "EU (Paris)"
97+
value = "eu-west-3"
98+
icon = "/emojis/1f1ea-1f1fa.png"
99+
}
100+
option {
101+
name = "South America (São Paulo)"
102+
value = "sa-east-1"
103+
icon = "/emojis/1f1e7-1f1f7.png"
104+
}
105+
}
106+
107+
data "coder_parameter" "home_disk_size" {
108+
name = "home_disk_size"
109+
display_name = "Home disk size"
110+
description = "The size of the home disk in GB"
111+
default = "50"
112+
type = "number"
113+
icon = "/emojis/1f4be.png"
114+
mutable = false
115+
validation {
116+
min = 50
117+
max = 300
118+
}
119+
}
120+
121+
data "coder_parameter" "instance_type" {
122+
name = "instance_type"
123+
display_name = "Instance type"
124+
description = "What instance type should your workspace use?"
125+
default = "t3.large"
126+
mutable = false
127+
option {
128+
name = "2 vCPU, 1 GiB RAM"
129+
value = "t3.micro"
130+
}
131+
option {
132+
name = "2 vCPU, 2 GiB RAM"
133+
value = "t3.small"
134+
}
135+
option {
136+
name = "2 vCPU, 4 GiB RAM"
137+
value = "t3.medium"
138+
}
139+
option {
140+
name = "2 vCPU, 8 GiB RAM"
141+
value = "t3.large"
142+
}
143+
option {
144+
name = "4 vCPU, 16 GiB RAM"
145+
value = "t3.xlarge"
146+
}
147+
option {
148+
name = "8 vCPU, 32 GiB RAM"
149+
value = "t3.2xlarge"
150+
}
151+
}
152+
153+
provider "aws" {
154+
region = data.coder_parameter.region.value
155+
}
156+
157+
data "coder_workspace" "me" {}
158+
data "coder_workspace_owner" "me" {}
159+
160+
data "aws_ami" "windows" {
161+
most_recent = true
162+
owners = ["amazon"]
163+
164+
filter {
165+
name = "name"
166+
values = ["Windows_Server-2022-English-Full-Base-*"]
167+
}
168+
}
169+
170+
resource "coder_agent" "dev" {
171+
count = data.coder_workspace.me.start_count
172+
arch = "amd64"
173+
auth = "aws-instance-identity"
174+
os = "windows"
175+
}
176+
177+
locals {
178+
# User data is used to stop/start AWS instances. See:
179+
# https://github.com/hashicorp/terraform-provider-aws/issues/22
180+
user_data_start = <<EOT
181+
<powershell>
182+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
183+
${try(coder_agent.dev[0].init_script, "")}
184+
</powershell>
185+
<persist>true</persist>
186+
EOT
187+
# user_data_end = <<EOT
188+
# <powershell>
189+
# shutdown /s
190+
# </powershell>
191+
# EOT
192+
user_data_end = ""
193+
}
194+
195+
resource "aws_instance" "dev" {
196+
ami = data.aws_ami.windows.id
197+
availability_zone = "${data.coder_parameter.region.value}a"
198+
instance_type = data.coder_parameter.instance_type.value
199+
200+
user_data = data.coder_workspace.me.transition == "start" ? local.user_data_start : local.user_data_end
201+
tags = {
202+
Name = "coder-${data.coder_workspace_owner.me.name}-${data.coder_workspace.me.name}"
203+
# Required if you are using our example policy, see template README
204+
Coder_Provisioned = "true"
205+
}
206+
207+
root_block_device {
208+
volume_size = data.coder_parameter.home_disk_size.value
209+
}
210+
211+
lifecycle {
212+
ignore_changes = [
213+
ami,
214+
user_data
215+
]
216+
}
217+
}
218+
219+
resource "aws_ec2_instance_state" "dev" {
220+
instance_id = aws_instance.dev.id
221+
state = data.coder_workspace.me.transition == "start" ? "running" : "stopped"
222+
}
223+
224+
module "dcv" {
225+
count = data.coder_workspace.me.start_count
226+
source = "github.com/coder/modules//amazon-dcv-windows?ref=main"
227+
agent_id = resource.coder_agent.dev[count.index].id
228+
subdomain = true
229+
}
230+
231+
module "vscode-on-ws" {
232+
count = data.coder_workspace.me.start_count
233+
source = "./vscode-on-ws"
234+
agent_id = resource.coder_agent.dev[count.index].id
235+
}
236+
237+
resource "coder_metadata" "workspace_info" {
238+
count = data.coder_workspace.me.start_count
239+
resource_id = aws_instance.dev.id
240+
item {
241+
key = "region"
242+
value = data.coder_parameter.region.value
243+
}
244+
item {
245+
key = "instance type"
246+
value = aws_instance.dev.instance_type
247+
}
248+
item {
249+
key = "disk"
250+
value = "${aws_instance.dev.root_block_device[0].volume_size} GiB"
251+
}
252+
item {
253+
key = "DCV client instructions"
254+
value = "Run `coder port-forward ${data.coder_workspace.me.name} -p ${module.dcv[count.index].port}` and connect to **localhost:${module.dcv[count.index].port}${module.dcv[count.index].web_url_path}**"
255+
}
256+
item {
257+
key = "username"
258+
value = module.dcv[count.index].username
259+
}
260+
item {
261+
key = "password"
262+
value = module.dcv[count.index].password
263+
sensitive = true
264+
}
265+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function Download-VSCode {
2+
param ()
3+
Write-Output "[INFO] Starting Download-VSCode function"
4+
5+
$downloads = @(
6+
@{
7+
Name = "VSCode"
8+
Required = $true
9+
Path = "C:\Users\Administrator\VSCodeSetup-x64-1.95.0.exe"
10+
Uri = "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64"
11+
}
12+
)
13+
14+
foreach ($download in $downloads) {
15+
if ($download.Required -and -not (Test-Path $download.Path)) {
16+
try {
17+
Write-Output "[INFO] Downloading $($download.Name)"
18+
19+
# Display progress manually (no events)
20+
$progressActivity = "Downloading $($download.Name)"
21+
$progressStatus = "Starting download..."
22+
Write-Progress -Activity $progressActivity -Status $progressStatus -PercentComplete 0
23+
24+
# Synchronously download the file
25+
$webClient = New-Object System.Net.WebClient
26+
$webClient.DownloadFile($download.Uri, $download.Path)
27+
28+
# Update progress
29+
Write-Progress -Activity $progressActivity -Status "Completed" -PercentComplete 100
30+
31+
Write-Output "[INFO] $($download.Name) downloaded successfully."
32+
} catch {
33+
Write-Output "[ERROR] Failed to download $($download.Name): $_"
34+
throw
35+
}
36+
} else {
37+
Write-Output "[INFO] $($download.Name) already exists. Skipping download."
38+
}
39+
}
40+
41+
Write-Output "[INFO] All downloads completed"
42+
Read-Host "[DEBUG] Press Enter to proceed to the next step"
43+
}
44+
45+
46+
function Install-VSCode {
47+
param ()
48+
Write-Output "[INFO] Starting Install-VSCode function"
49+
50+
$download_path = "C:\Users\Administrator\VSCodeSetup-x64-1.95.0.exe"
51+
52+
Write-Output "[INFO] Installing VSCode"
53+
Start-Process -FilePath $download_path -Argument "/VERYSILENT /MERGETASKS=!runcode,desktopicon" -Wait
54+
}
55+
56+
# Main Script Execution
57+
Write-Output "[INFO] Starting script"
58+
Download-VSCode
59+
Install-VSCode
60+
Write-Output "[INFO] Script completed"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
coder = {
6+
source = "coder/coder"
7+
version = ">= 0.17"
8+
}
9+
}
10+
}
11+
12+
variable "agent_id" {
13+
type = string
14+
description = "The ID of a Coder agent."
15+
}
16+
17+
resource "coder_script" "install-vscode" {
18+
agent_id = var.agent_id
19+
display_name = "Install VS Code"
20+
icon = "/icon/vscode.svg"
21+
run_on_start = true
22+
script = templatefile("${path.module}/install.ps1", {})
23+
}

0 commit comments

Comments
 (0)