Skip to content

fix: fix aws_linux template #9349

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 2 commits into from
Aug 31, 2023
Merged
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
16 changes: 9 additions & 7 deletions examples/templates/aws-linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ data "aws_ami" "ubuntu" {
owners = ["099720109477"] # Canonical
}

resource "coder_agent" "main" {
resource "coder_agent" "dev" {
count = data.coder_workspace.me.start_count
arch = "amd64"
auth = "aws-instance-identity"
os = "linux"
Expand Down Expand Up @@ -195,7 +196,8 @@ resource "coder_agent" "main" {
}

resource "coder_app" "code-server" {
agent_id = coder_agent.main.id
count = data.coder_workspace.me.start_count
agent_id = coder_agent.dev[0].id
slug = "code-server"
display_name = "code-server"
url = "http://localhost:13337/?folder=/home/coder"
Expand All @@ -211,9 +213,8 @@ resource "coder_app" "code-server" {
}

locals {
linux_user = "coder" # Ensure this user/group does not exist in your VM image
# User data is used to run the init_script
user_data = <<EOT
linux_user = "coder"
user_data = data.coder_workspace.me.start_count > 0 ? trimspace(<<EOT
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

Expand All @@ -239,17 +240,18 @@ Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
sudo -u ${local.linux_user} sh -c '${coder_agent.main.init_script}'
sudo -u ${local.linux_user} sh -c '${coder_agent.dev[0].init_script}'
--//--
EOT
) : ""
Copy link
Member

@bpmct bpmct Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! TIL!

You can also use try so that Terraform provides a value even if something fails.

For example:

  user_data = <<EOT
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
@@ -239,17 +240,18 @@ Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
sudo -u ${local.linux_user} sh -c '${try(coder_agent.dev[0].init_script, "N/A")}'
--//--
EOT

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. More cleaner

Copy link
Member Author

@matifali matifali Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpmct This works and is cleaner. Feel free to update the template.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

}

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_start
user_data = local.user_data
tags = {
Name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
# Required if you are using our example policy, see template README
Expand Down