I want to use Terraform for my existing AWS infrastructure. Therefore, I imported an existing EC2 instance with EBS block devices. Then I wrote the Terraform code. But Terraform wants to replace all EBS block devices. Is there any way to not replace the EBS block devices?
Import
I imported my AWS EC2 instance as described in Import:
Import
Instances can be imported using the id, e.g.,
$ terraform import aws_instance.web i-12345678
Code
After I imported the state file, I wrote my Terraform code.
This is the part for the EBS block device:
ebs_block_device {
device_name = "/dev/sdf"
volume_type = "gp2"
volume_size = 20
tags = {
Name = "my-test-docker"
}
}
Plan
After I wrote the code, I run the plan command, see Command: plan.
This is the part for the EBS block device:
- ebs_block_device { # forces replacement
- delete_on_termination = false -> null
- device_name = "/dev/sdf" -> null
- encrypted = false -> null
- iops = 100 -> null
- snapshot_id = "snap-0e22b434e3106fd51" -> null
- tags = {
- "Name" = "my-test-docker"
} -> null
- throughput = 0 -> null
- volume_id = "vol-065138961fea23bf4" -> null
- volume_size = 20 -> null
- volume_type = "gp2" -> null
}
+ ebs_block_device { # forces replacement
+ delete_on_termination = true
+ device_name = "/dev/sdf"
+ encrypted = (known after apply)
+ iops = (known after apply)
+ kms_key_id = (known after apply)
+ snapshot_id = (known after apply)
+ tags = {
+ "Name" = "my-test-docker"
}
+ throughput = (known after apply)
+ volume_id = (known after apply)
+ volume_size = 20
+ volume_type = "gp2"
}
Research
I tried to set the volume_id
in my code, but Terraform didn't allow to set this property.
Actually, the EBS block device shouldn't be replaced after creation, see ebs_block_device:
- ebs_block_device - (Optional) One or more configuration blocks with additional EBS block devices to attach to the instance. Block device configurations only apply on resource creation. See Block Devices below for details on attributes and drift detection. When accessing this as an attribute reference, it is a set of objects.
Background
One of the reasons for using ebs_block_device
instead of aws_ebs_volume
with aws_volume_attachment
was aws_volume_attachment Error waiting for Volume .
Another reason was
- Ebs volumes are not attached and available at boot time for userdata script to mount them.
- AWS Attach/Format device with CLOUDINIT
- fs_setup/disk_setup: option to wait for the device to exist before continuing
ebs_block_device
is still working fine for new (not imported) resources.
The formating and mounting part of my cloud-init user data script:
fs_setup:
- label: docker
filesystem: ext4
device: /dev/xvdf
overwrite: false
mounts:
- [ "/dev/xvdf", "/mnt/docker-data", "auto", "defaults,nofail", "0", "2" ]
Question
Is there any way to give Terraform a hint to match EBS block device in code with EBS block devices in the state file?