一千萬個為什麽

搜索

如何分離現有卷並使用Terraform附加新卷?



有沒有辦法從現有EC2實例中分離EBS卷,並使用 Terraform v0.9.2 附加新的EBS卷?

我更喜歡更換音量,而不是嘗試銷毀實例並用新音量創建新音量。

在分離卷之前,將文件系統從操作系統內的設備卸載,並且從AWS控制臺分離卷也沒有問題。

resource "aws_ebs_volume" "create_volume" {
  availability_zone = "eu-central-1a"
  snapshot_id = "${data.aws_ebs_snapshot.mysql.id}"
  type = "gp2"
  tags {
    Name = "${var.instance_name}"
  }
}

resource "aws_volume_attachment" "mysql_data" {
  depends_on = ["null_resource.stop_mysql_service2"]
  device_name = "/dev/xvdf"
  volume_id = "${aws_ebs_volume.create_volume.0.id}"
  instance_id = "i-0d48be4266da"
  skip_destroy = true
  force_detach = true
}

$ ./terraform apply
data.aws_ebs_snapshot.mysql: Refreshing state...
aws_ebs_volume.create_volume: Creating...
  availability_zone: "" => "eu-central-1a"
  encrypted:         "" => ""
  iops:              "" => ""
  kms_key_id:        "" => ""
  size:              "" => ""
  snapshot_id:       "" => "snap-0afb2303c60f"
  tags.%:            "" => "1"
  tags.Name:         "" => "mysql"
  type:              "" => "gp2"
aws_ebs_volume.create_volume: Still creating... (10s elapsed)
aws_ebs_volume.create_volume: Creation complete (ID: vol-04c7f4)
null_resource.stop_mysql_service2: Creating...
null_resource.stop_mysql_service2: Provisioning with 'remote-exec'...
null_resource.stop_mysql_service2 (remote-exec): Connecting to remote host via SSH...
null_resource.stop_mysql_service2 (remote-exec):   Host: mysql
null_resource.stop_mysql_service2 (remote-exec):   User: ubuntu
null_resource.stop_mysql_service2 (remote-exec):   Password: false
null_resource.stop_mysql_service2 (remote-exec):   Private key: true
null_resource.stop_mysql_service2 (remote-exec):   SSH Agent: true
null_resource.stop_mysql_service2 (remote-exec): Connected!
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2: Still creating... (10s elapsed)
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec): .
null_resource.stop_mysql_service2 (remote-exec):  * Attempt to shutdown MySQL Community Server 5.6.35 timed out
null_resource.stop_mysql_service2: Still creating... (20s elapsed)
null_resource.stop_mysql_service2: Still creating... (30s elapsed)
null_resource.stop_mysql_service2: Creation complete (ID: 1850529311883)
aws_volume_attachment.mysql_data: Creating...
  device_name:  "" => "/dev/xvdf"
  force_detach: "" => "true"
  instance_id:  "" => "i-0d48be4266da"
  skip_destroy: "" => "true"
  volume_id:    "" => "vol-04c7f4"
aws_volume_attachment.mysql_data: Still creating... (10s elapsed)
Error applying plan:

1 error(s) occurred:

* aws_volume_attachment.mysql_data: 1 error(s) occurred:

* aws_volume_attachment.mysql_data: [WARN] Error attaching volume (vol-04c7f4) to instance (i-0d48be4266da), 
message: "Invalid value '/dev/xvdf' for unixDevice. Attachment point /dev/xvdf is already in use", code: "InvalidParameterValue"

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

轉載註明原文: 如何分離現有卷並使用Terraform附加新卷?

一共有 1 個回答:

舊卷從不分離,因為 skip_destroy = true 。從terraform文檔:

skip_destroy - (可選,布爾型)如果您不希望在銷毀時將卷從其所連接的實例中分離出來,並將該附件從Terraform狀態中刪除,請將其設置為true。這在銷毀具有通過附加的其他方式創建的卷的實例時非常有用。

如果刪除該附件,則舊附件將被銷毀,並且附件點應可用於新附件。

您可能需要手動刪除舊的附件,我相信Terraform可能已經從狀態中刪除了它。