- Newest
- Most votes
- Most comments
AWS CloudFormation uses one of the following update behaviors for the resource properties:
-
Update with No Interruption
-
Updates with Some Interruption
-
Replacement
If the resource property is Replacement it will follow the below behavior: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html
Replacement
AWS CloudFormation recreates the resource during an update, which also generates a new physical ID. AWS CloudFormation usually creates the replacement resource first, changes references from other dependent resources to point to the replacement resource, and then deletes the old resource. For example, if you update the AvailabilityZone property of an AWS::EC2::Instance resource type, AWS CloudFormation creates a new resource and replaces the current EC2 Instance resource with the new one.
Now let's look into your template:
1). You are updating ImageId in the template.
ImageId: ImageID1 # Replace with a valid AMI ID
- So if you look into the ImageId Property https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-instance.html#cfn-ec2-instance-imageid , the behavior is Replacement behavior:
ImageId
The ID of the AMI. An AMI ID is required to launch an instance and must be specified here or in a launch template.
Required: Conditional
Type: String
Update requires: Replacement
- Now when the stack is updated there is an Update Complete of new resource and Delete Complete of old resource in stack events.
In order to save old resources, please use UpdateReplacePolicy to "Retain on resources you want to keep even if their PysicalId changes.
Example:
AWSTemplateFormatVersion: 2010-09-09
Resources:
myDB:
Type: 'AWS::RDS::DBInstance'
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties: {}
Reference:
When you change certain properties of a resource in a CloudFormation template, such as the AMI ID of an EC2 instance, CloudFormation may perform a replacement update rather than an in-place update. This is because some properties are fundamental to the resource and cannot be changed without creating a new resource.
In the case of an EC2 instance, changing the AMI ID requires a new instance to be created, as the AMI is the base image from which the instance is launched. This is why your original instance was deleted and a new one was created.
This behavior is part of CloudFormation's update process, where it determines the most appropriate way to apply changes to resources. For some properties, CloudFormation can update the resource without interruption or with some interruption. However, for properties that require a complete replacement, CloudFormation will create a new resource with the updated properties, switch any dependencies to the new resource, and then delete the old resource.
To avoid unexpected deletions and recreations of resources, you can:
-
Be aware of which properties trigger replacements for each resource type. For EC2 instances, properties like AMI ID, instance type, and subnet ID typically require replacement.
-
Use CloudFormation change sets before applying updates. This allows you to preview the changes that will occur to your stack before actually executing them.
-
If you need to preserve the existing resource, consider creating a new resource with the desired changes instead of modifying the existing one. You can then manually migrate data or configurations as needed.
-
For EC2 instances specifically, if you need to update the AMI while preserving data, you might consider using Amazon EBS volumes and attaching them to the new instance after the replacement.
-
In some cases, you might be able to use AWS Systems Manager to update the instance in-place without changing the CloudFormation template, depending on what exactly needs to be updated.
Remember, while these strategies can help minimize unexpected changes, some updates will inherently require resource replacement. It's important to thoroughly understand the implications of template changes and plan accordingly to ensure minimal disruption to your applications and services.
Sources
Understand update behaviors of stack resources - AWS CloudFormation
Recreate a resource deleted outside CloudFormation | AWS re:Post
