Skip to content

feat: use hashicorp/cloud-init provider for AWS-linux example #15240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#cloud-config
cloud_final_modules:
- [scripts-user, always]
hostname: ${hostname}
users:
- name: ${linux_user}
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
2 changes: 2 additions & 0 deletions examples/templates/aws-linux/cloud-init/userdata.sh.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
sudo -u '${linux_user}' sh -c '${init_script}'
55 changes: 27 additions & 28 deletions examples/templates/aws-linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ provider "aws" {
region = data.coder_parameter.region.value
}

data "coder_workspace" "me" {
}
data "coder_workspace" "me" {}
data "coder_workspace_owner" "me" {}

data "aws_ami" "ubuntu" {
Expand Down Expand Up @@ -214,44 +213,44 @@ resource "coder_app" "code-server" {
}

locals {
hostname = lower(data.coder_workspace.me.name)
linux_user = "coder"
user_data = <<-EOT
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
}

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
data "cloudinit_config" "user_data" {
gzip = false
base64_encode = false

#cloud-config
cloud_final_modules:
- [scripts-user, always]
hostname: ${lower(data.coder_workspace.me.name)}
users:
- name: ${local.linux_user}
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
boundary = "//"

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
part {
filename = "cloud-config.yaml"
content_type = "text/cloud-config"

#!/bin/bash
sudo -u ${local.linux_user} sh -c '${try(coder_agent.dev[0].init_script, "")}'
--//--
EOT
content = templatefile("${path.module}/cloud-init/cloud-config.yaml.tftpl", {
hostname = local.hostname
linux_user = local.linux_user
})
}

part {
filename = "userdata.sh"
content_type = "text/x-shellscript"

content = templatefile("${path.module}/cloud-init/userdata.sh.tftpl", {
linux_user = local.linux_user

init_script = try(coder_agent.dev[0].init_script, "")
})
}
}

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

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