Question: How do I update my EBS volume in CloudFormation without EC2 instances being replaced?

I want to update my Amazon Elastic Block Store (Amazon EBS) volume in AWS CloudFormation without Amazon Elastic Compute Cloud (Amazon EC2) instances being replaced.

Short descriptionAs a best practice, use the AWS::EC2::Volume resource type to prevent instance replacement when updating EBS volumes in CloudFormation.Instance replacement occurs when you specify volumes in the BlockDeviceMappings property of the AWS::EC2::Instance and AWS::EC2::Template resource types. In this case, you must add a Retain DeletionPolicy attribute.Prerequisites: If you modify the volume from gp2 to gp3, then make sure that the volume that's attached to the instance is modified to gp3. Also, make sure that the instance isn't in the Optimizing or Modifying states. Before you modify the volume to gp3, check what the limitations are.Important: Before resolving the issue, take a snapshot of the volumes to create a backup of critical workloads.Resolution1.    Add the Retain DeletionPolicy to the CloudFormation stack for the instance that you want to update the volume, and then update the stack:AWSTemplateFormatVersion: '2010-09-09'Resources: Myinstance:  Type: AWS::EC2::Instance  DeletionPolicy: Retain  Properties:   BlockDeviceMappings:    - DeviceName: /dev/xvda     Ebs:      VolumeType: gp2      VolumeSize: 10      DeleteOnTermination: true   EbsOptimized: false   ImageId: ami-064ff912f78e3e561   InstanceInitiatedShutdownBehavior: stop   InstanceType: t2.micro   Monitoring: false2.    Update the CloudFormation stack again by removing the instance from the template. Note: If you have only one resource in your template, then you must create a stand-in resource, such as another instance. You can delete the resource from the template after you finished.3.    Modify the EBS volume attributes to your requirements.4.    Import the instance back into the CloudFormation stack.To import the instance back into the CloudFormation stack:1.    Open the AWS CloudFormation console.2.    On the stack page, choose Stack actions and then choose Import resources into stack.3.    Update the template:AWSTemplateFormatVersion: '2010-09-09'Resources: Myinstance:  Type: AWS::EC2::Instance  DeletionPolicy: Retain  Properties:    BlockDeviceMappings:    - DeviceName: /dev/xvda      Ebs:       VolumeType: gp3       VolumeSize: 100       DeleteOnTermination: true    EbsOptimized: false    ImageId: ami-064ff912f78e3e561    InstanceInitiatedShutdownBehavior: stop    InstanceType: t2.micro    Monitoring: false4.    Enter the instance ID value into the Identifier field.5.    Choose Import resource.After CloudFormation moves to IMPORT_COMPLETE status, the instances are part of the stack again.Note: You might receive the error, There was an error creating this change set. As part of the import operation, you cannot modify or add [Outputs]. To resolve this issue, verify that the Outputs sections of the latest CloudFormation template and the template that your stack is using are the same. If they're not, update the latest CloudFormation template to match the values in the Outputs section of the template that your stack is using. Then, update the stack again.