I want to update my Amazon Elastic Block Store (Amazon EBS) volume in AWS CloudFormation. However, I don't want to replace my Amazon Elastic Compute Cloud (Amazon EC2) instances.
Resolution
To prevent instance replacement, it's a best practice to use the AWS::EC2::Volume resource type when you update 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 scenario, you must add a retain DeletionPolicy attribute.
Important: If you manually modify the volume, such as from gp2 to gp3, then you must also modify the volume that's attached to the instance. Verify that the instance isn't in the Optimizing or Modifying state. Before you modify the volume to gp3, make sure that you adhere to the volume modification requirements.
To avoid instance replacement when you modify volumes that you specify with the BlockDeviceMappings property, complete the following steps.
Add a retain DeletionPolicy attribute to the target AWS::EC2::Instance template resource
Complete the following steps:
-
Take a snapshot of the volumes to create a backup of critical workloads.
-
Set the DeletionPolicy to Retain in the CloudFormation stack of the instance with the volume that you want to update. Example:
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: false
-
Update the stack.
Remove the resource from its CloudFormation stack and implement your changes
Complete the following steps:
- Remove the instance from the template to remove the instance from the CloudFormation stack but not delete the underlying resource. Then, manually modify the EC2 instance.
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 import the EC2 instance back into the template.
- Modify the EBS volume attributes.
Import the resource back into your CloudFormation stack
Complete the following steps:
-
Open the CloudFormation console.
-
In the navigation pane, choose Stack.
-
Choose Stack actions, and then choose Import resources into stack.
-
Enter the updated CloudFormation template. Example:
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: false
Note: To import a resource, the CloudFormation template must describe the resource with its existing configurations.
-
For Identifier, enter the instance ID.
-
Choose Import resource.
After CloudFormation updates the status to IMPORT_COMPLETE, the instance is part of the stack.
You might receive the "There was an error creating this change set. As part of the import operation, you cannot modify or add [Outputs]" error message. To resolve this issue, verify that the Outputs section of the latest CloudFormation template matches the template that your stack uses. If they're not the same, then update the latest CloudFormation template to match the values in the Outputs section of the template that your stack uses. Then, update the stack again.