Coder.comC
Coder.com2y ago
1 reply
Suffocator

issues with stopping workspaces using spot instance

When trying to stop a workspace that is using a spot instance, it seems that the instance is being recreated and then stopped again, however it’s in a broken state and when trying to restart it errors.

This is the relevant template sections, pulled mostly from the spot instance example

resource "aws_spot_instance_request" "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-xxxx"
  instance_interruption_behavior = "stop"
  iam_instance_profile = "CoderWorkspaceInstanceRole"
  wait_for_fulfillment = true

  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
    Coder_Provisioned = "true"
  }
  volume_tags = {
    # Required if you are using our example policy, see template README
    Coder_Provisioned = "true"
  }

  root_block_device {
    volume_size = 50
    volume_type = "gp3"
  }

  lifecycle {
    ignore_changes = [ami]
  }
}

resource "aws_ec2_instance_state" "dev" {
  instance_id = aws_spot_instance_request.dev.spot_instance_id
  state       = data.coder_workspace.me.transition == "start" ? "running" : "stopped"
}


And this is the logs when the workspace stops
https://pastebin.com/0LruH1FJ


You can see the aws_ec2_instance_state.dev is destroyed along with the request, but then they are recreated straight away.

I’m thinking this is because it is trying to get the instance state to stopped, and not terminated

Any ideas? Or examples of a working AWS spot setup?
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Was this page helpful?