Coder.comC
Coder.com13mo ago
6 replies
Chris2pi

Persistent AWS EBS/EC2 home directory

Problem—When I click restart in the coder web GUI, it recreates my workspace and causes the EBS volume to be recreated, causing me to lose all of the data.
I understand why that is as a terraform recreate is happening.

So, before I go down the rabbit hole of making custom scripts, etc., I just wanted to see if there was an out-of-the-box method from the coder.

I found this doco - https://coder.com/docs/admin/templates/extending-templates/resource-persistence But it doesn't seem right for what I want.

Napkin-style plan for what I think should happen -
The user hits restart.
Recreation happens, but the EBS is detached from the instance.
A new instance spins up and attaches the EBS again.


Source code based on this template - https://github.com/coder/coder/blob/main/examples/templates/aws-linux/main.tf
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
  subnet_id         = "subnet-xxxxxxxxxxxxxxxx"
  associate_public_ip_address = true

  user_data = data.cloudinit_config.user_data.rendered

    # Define the root volume storage
  root_block_device {
    volume_type           = "gp3"  # You can choose gp2, gp3, io1, etc.
    volume_size           = data.coder_parameter.disk_space.value     # Specify the volume size in GB
    encrypted             = true   # Encrypt the volume
    delete_on_termination = true
  }

  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
    Coder_Provisioned = "true"
  }
  lifecycle {
    ignore_changes = [ami]
  }
}
Control resource persistence
Resource Persistence | Coder Docs
Preview image
GitHub
Provision remote development environments via Terraform - coder/coder
coder/examples/templates/aws-linux/main.tf at main · coder/coder
Was this page helpful?