- Newest
- Most votes
- Most comments
When you change the resource's logical ID within the template, it initiates a replacement update. Please refer the document that explains LogicalId stability:
https://docs.aws.amazon.com/cdk/v2/guide/identifiers.html#identifiers-logical-ids
Logical ID stability
-
Avoid changing the logical ID of a resource after it has been created. AWS CloudFormation identifies resources by their logical ID. Therefore, if you change the logical ID of a resource, AWS CloudFormation creates a new resource with the new logical ID, then deletes the existing one. Depending on the type of resource, this might cause service interruption, data loss, or both.
-
In your case, when you changed the Logical ID of the S3 bucket from "MyBucket" to "MyBucket1", CloudFormation treated this as a instruction to delete the original bucket and create a new one with the updated Logical ID. This is why you didn't see an UPDATE_COMPLETE action for the S3 bucket in the CloudFormation event history, but rather a deletion of the old bucket and creation of a new one.
Example:
1). Creation of S3 bucket using below template.
Resources:
S3Bucket: #Logical ID of the Resource
Type: AWS::S3::Bucket
2). Now you are updating the stack S3 bucket logical Id as mentioned below:
Resources:
S3NewBucket: #The changed Logical ID of the Resource
Type: AWS::S3::Bucket
Since you changed the LogicalId, CloudFormation treats this as a instruction to delete the original bucket and create a new one with the updated Logical ID. This is why you do not observe an UPDATE_COMPLETE action for the S3 bucket in the CloudFormation event as the UniqueId itself has lost its identity in the template.
Also you do not have DeletionPolicy or UpdateReplacePolicy set to Retain, the old bucket will be deleted and the new S3 bucketwith logical Id with"S3NewBucket" will be created.
In order to save old resources, please use DeletionPolicy attribute set to "Retain" or UpdateReplacePolicy to "Retain on resources you want to keep even if their Logical ID changes. Please refer the document: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatereplacepolicy.html#aws-attribute-updatereplacepolicy-example.yaml
Reference:
[1]. https://repost.aws/knowledge-center/delete-cf-stack-retain-resources/n
answered a year ago
When you change or remove the Logical ID of a resource in a CloudFormation template, CloudFormation interprets this as a request to delete the existing resource and create a new one. This behavior occurs because the Logical ID is how CloudFormation uniquely identifies resources within a stack.
In your case, when you changed the Logical ID of the S3 bucket from "MyBucket" to "MyBucket1", CloudFormation treated this as a instruction to delete the original bucket and create a new one with the updated Logical ID. This is why you didn't see an UPDATE_COMPLETE action for the S3 bucket in the CloudFormation event history, but rather a deletion of the old bucket and creation of a new one.
For the EC2 instance, changing the AMI triggers a replacement of the instance, which is why you saw it being deleted and recreated. This is expected behavior for properties that require replacement.
To prevent unintended deletions when changing Logical IDs, you have a few options:
- Avoid changing Logical IDs unless absolutely necessary.
- If you need to rename a resource, use a two-step process: First, create the new resource with the desired name alongside the old one. Then, in a separate update, remove the old resource.
- Use the DeletionPolicy attribute set to "Retain" on resources you want to keep even if their Logical ID changes.
Remember, CloudFormation uses Logical IDs to track resources across stack updates. Changing a Logical ID essentially tells CloudFormation to treat it as a completely new resource, leading to the behavior you observed.
Sources
Recreate a resource deleted outside CloudFormation | AWS re:Post
Update your stack template - AWS CloudFormation
Understand update behaviors of stack resources - AWS CloudFormation
answered a year ago
Relevant content
asked 4 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 3 years ago
