Skip to content

example: aws-linux: resize and use non-root user #2186

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 3 commits into from
Jun 9, 2022
Merged
Changes from 1 commit
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
Next Next commit
example: aws-linux: resize and use non-root user
  • Loading branch information
bpmct committed Jun 8, 2022
commit 556fbacf93f8a02332dbdb4ef1761339f9c2079c
22 changes: 7 additions & 15 deletions examples/templates/aws-linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ variable "region" {
}
}

variable "disk_size" {
description = "Specify your disk size (GiBs)"
default = "20"
type = number
validation {
condition = (
var.disk_size >= 8 &&
var.disk_size <= 256
)
error_message = "Disk size must be between 8 and 256."
}
}
Comment on lines -39 to -50
Copy link
Member Author

Choose a reason for hiding this comment

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

this was never used


provider "aws" {
region = var.region
}
Expand Down Expand Up @@ -93,6 +80,11 @@ Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
hostname: ${lower(data.coder_workspace.me.name)}
users:
- name: ${lower(data.coder_workspace.me.owner)}
Copy link
Contributor

Choose a reason for hiding this comment

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

what restrictions do we have on coder usernames? Is it a subset of the valid Linux usernames?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm actually not sure here. I could change it to "coder" to be safe

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, so a little googling: Linux usernames can have uppercase, lowercase, digits, hyphens and underscores -- we restrict our usernames to this minus the hyphens, so all good on character set. However, Linux usernames can only be 32 characters long, at least in some flavors/system utilities and we allow unlimited. I think it's fine to just truncate if the name is too long.

Copy link
Member Author

@bpmct bpmct Jun 9, 2022

Choose a reason for hiding this comment

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

Cool, thanks for looking into this. Usernames are now truncated, if longer than 32 characters using Terraform's substr. If usernames are shorter, nothing happens

sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash

--//
Content-Type: text/x-shellscript; charset="us-ascii"
Expand All @@ -101,7 +93,7 @@ Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
sudo -u ubuntu sh -c '${coder_agent.dev.init_script}'
sudo -u ${lower(data.coder_workspace.me.owner)} sh -c '${coder_agent.dev.init_script}'
--//--
EOT

Expand Down Expand Up @@ -134,7 +126,7 @@ EOT
resource "aws_instance" "dev" {
ami = data.aws_ami.ubuntu.id
availability_zone = "${var.region}a"
instance_type = "t3.micro"
instance_type = "t3.medium"

user_data = data.coder_workspace.me.transition == "start" ? local.user_data_start : local.user_data_end
tags = {
Expand Down