简体   繁体   中英

CloudFormation / CDK: how to replace an EC2 instance and keep EBS storage?

Using AWS CDK, an EC2 instance with attached EBS can be created like this:

BlockDevice durableStorage = BlockDevice.builder()
                                        .deviceName("/dev/sdf")
                                        .volume(BlockDeviceVolume.ebs(
                                                DURABLE_STORAGE_GB,
                                                EbsDeviceOptions.builder()
                                                                .deleteOnTermination(false)
                                                                .encrypted(true)
                                                                .volumeType(EbsDeviceVolumeType.GP2)
                                                                .build()))
                                        .build();

Instance instance = new Instance(
        this,
        "MyInstance",
        InstanceProps.builder()
                     .blockDevices(List.of(durableStorage))
                     // more config here
                     .build());

If there is an update to the stack that involves a replacement to the EC2 instance, how is the EBS attachment managed? The old instance is kept until the new one has been created and only then is it destroyed, so how should one manage the transfer of the EBS volume to the new server? Is would this be managed in CloudFormation?

so how should one manage the transfer of the EBS volume to the new server? Is would this be managed in CloudFormation?

Its not managed. The update which requires replacement of instance (eg AMI id change) will fail with the following error message:

Update to resource type AWS::EC2::VolumeAttachment is not supported.

One way to deal with this is to do update in stages . First, you remove the attachment in your template (just comment it out) and update the stack to dissociate instance with volume. Then you do replacement update of your instance. Finally, you uncomment the attachment and update again. This result in reattachment of the volume to new instance.

ps

I wrote this answer based on my quick experiments for this specific scenario in CloudFormation that I did for this question. Maybe there is better way, I don't know at present.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM