Skip to content

Commit c8f68cb

Browse files
authored
feat: use hashicorp/cloud-init provider for AWS-linux example (coder#15240)
Same as coder#15050 but for the `aws-linux` template. Tested, works as expected.
1 parent 91c337a commit c8f68cb

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#cloud-config
2+
cloud_final_modules:
3+
- [scripts-user, always]
4+
hostname: ${hostname}
5+
users:
6+
- name: ${linux_user}
7+
sudo: ALL=(ALL) NOPASSWD:ALL
8+
shell: /bin/bash
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
sudo -u '${linux_user}' sh -c '${init_script}'

examples/templates/aws-linux/main.tf

+27-28
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ provider "aws" {
140140
region = data.coder_parameter.region.value
141141
}
142142

143-
data "coder_workspace" "me" {
144-
}
143+
data "coder_workspace" "me" {}
145144
data "coder_workspace_owner" "me" {}
146145

147146
data "aws_ami" "ubuntu" {
@@ -214,44 +213,44 @@ resource "coder_app" "code-server" {
214213
}
215214

216215
locals {
216+
hostname = lower(data.coder_workspace.me.name)
217217
linux_user = "coder"
218-
user_data = <<-EOT
219-
Content-Type: multipart/mixed; boundary="//"
220-
MIME-Version: 1.0
218+
}
221219

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"
220+
data "cloudinit_config" "user_data" {
221+
gzip = false
222+
base64_encode = false
227223

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
224+
boundary = "//"
236225

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"
226+
part {
227+
filename = "cloud-config.yaml"
228+
content_type = "text/cloud-config"
242229

243-
#!/bin/bash
244-
sudo -u ${local.linux_user} sh -c '${try(coder_agent.dev[0].init_script, "")}'
245-
--//--
246-
EOT
230+
content = templatefile("${path.module}/cloud-init/cloud-config.yaml.tftpl", {
231+
hostname = local.hostname
232+
linux_user = local.linux_user
233+
})
234+
}
235+
236+
part {
237+
filename = "userdata.sh"
238+
content_type = "text/x-shellscript"
239+
240+
content = templatefile("${path.module}/cloud-init/userdata.sh.tftpl", {
241+
linux_user = local.linux_user
242+
243+
init_script = try(coder_agent.dev[0].init_script, "")
244+
})
245+
}
247246
}
248247

249248
resource "aws_instance" "dev" {
250249
ami = data.aws_ami.ubuntu.id
251250
availability_zone = "${data.coder_parameter.region.value}a"
252251
instance_type = data.coder_parameter.instance_type.value
253252

254-
user_data = local.user_data
253+
user_data = data.cloudinit_config.user_data.rendered
255254
tags = {
256255
Name = "coder-${data.coder_workspace_owner.me.name}-${data.coder_workspace.me.name}"
257256
# Required if you are using our example policy, see template README

0 commit comments

Comments
 (0)