Skip to content

Why does the resource gets deleted when the cloudformation template is updated especially when changing the resource properties ? How can we avoid this ?

1

I've launched the ec2 instance by using the below template. When i changed the AMIID the ec2 instance got deleted and it created a new one. I'm not sure what happened can anyone explain about this and How to avoid this ?

First template:

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ImageID1 # Replace with a valid AMI ID
      InstanceType: t2.micro
      Tags:
        - Key: Name
          Value: MyEC2Instance

Updated Template:

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ImageID2 # Replace with a valid AMI ID
      InstanceType: t2.micro
      Tags:
        - Key: Name
          Value: MyEC2Instance
2 Answers
0
Accepted Answer

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

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:

[1]. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatereplacepolicy.html#aws-attribute-updatereplacepolicy-example.yaml

AWS
answered a year ago
EXPERT
reviewed a year ago
0

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:

  1. 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.

  2. Use CloudFormation change sets before applying updates. This allows you to preview the changes that will occur to your stack before actually executing them.

  3. 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.

  4. 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.

  5. 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

answered a year ago
EXPERT
reviewed 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.

Relevant content