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]
}
}
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]
}
}
Resource Persistence | Coder Docs
Control resource persistence
From An unknown user
From An unknown user
GitHub
coder/examples/templates/aws-linux/main.tf at main · coder/coder
Provision remote development environments via Terraform - coder/coder
4 Replies
Codercord
Codercord2w ago
<#1326347271616598047>
Category
Help needed
Product
Coder (v2)
Platform
Linux
Logs
Please post any relevant logs/error messages.
Phorcys
Phorcys2w ago
@Chris2pi try setting the following in your volume's resource block
lifecycle {
ignore_changes = all
}
lifecycle {
ignore_changes = all
}
this is what we do with docker https://github.com/coder/coder/blob/9494afaae148b9b34643caa2d1465be8cb344311/examples/templates/docker/main.tf#L156-L181
Chris2pi
Chris2piOP2w ago
Perfect that seems to have resolved the issue I wonder how we could improve the doco with this or would this be more base terraform knowledge?
Phorcys
Phorcys6d ago
agreed, I think this would be something we can add to the docs, i've added the "improvement-needed" tag so I can look into it when I get the chance

Did you find this page helpful?