Skip to content

Why does a resource gets deleted when we change or remove the LogicalID when updating the cloudformation Template ?

1

I've created the cloud formation template which launches the ec2 instance and the s3 bucket. I've changed the resource properties for the ec2 instance where I've used the different AMI. I didn't use the deletion policy or update policy and i knew it will delete the ec2 instance which is mentioned here: https://repost.aws/knowledge-center/delete-cf-stack-retain-resources/n

But when I change the logicalResourceID for the s3 bucket the old bucket got deleted and there was no UPDATE_COMPLETE for this action in the cloud formation event history. May I know why ? Can anyone please let me know.

1st template:

AWSTemplateFormatVersion: '2010-09-09' Resources: MyEC2Instance: Type: AWS::EC2::Instance Properties: ImageId: imageID1 InstanceType: t2.micro Tags: - Key: Name Value: MyEC2Instance MyBucket: Type: AWS::S3::Bucket

UpdatedTemplate

AWSTemplateFormatVersion: '2010-09-09' Resources: MyEC2Instance: Type: AWS::EC2::Instance Properties: ImageId: imageID2 InstanceType: t2.micro Tags: - Key: Name Value: MyEC2Instance MyBucket1: Type: AWS::S3::Bucket

2 Answers
0
Accepted Answer

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

AWS

answered a year ago

0

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:

  1. Avoid changing Logical IDs unless absolutely necessary.
  2. 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.
  3. 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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.