How do I recreate a resource that was deleted outside of CloudFormation?

2 minute read
1

I want to recreate a resource that was deleted outside of the AWS CloudFormation stack.

Short description

When a resource's logical ID is removed from the template, CloudFormation interprets the resource as deleted. When you change the resource's logical ID  within the template, it initiates a replacement update. The update replaces the deleted CloudFormation resource. 
Note: Make sure that you update all the references to the old logical ID.

Resolution

Follow these steps to recover a resource that was deleted outside the CloudFormation stack.

  1. Identify the logical ID of the resource that was deleted out of band within your stacks template.
    Note: The logical ID is different from the physical ID, which is the Resource Name.

    In the following example, the Amazon Simple Storage Service (Amazon S3) bucket has its logical ID referenced within an export operation. The S3Bucket resource within the example template was deleted outside of CloudFormation's stack operations.

    Resources:  
      S3Bucket: #Logical ID of the Resource  
        Type: AWS::S3::Bucket  
    Outputs:  
      BucketName:  
        Value: !Ref S3Bucket  
        Export:  
          Name: PrimaryBucket
  2. Assign a new logical ID for the deleted resource (for example, S3NewBucket).

    Resources:  
      S3NewBucket: #The changed Logical ID of the Resource  
        Type: AWS::S3::Bucket  
    Outputs:  
      BucketName:  
        Value: !Ref S3Bucket  
        Export:  
          Name: PrimaryBucket
  3. Find and replace all references of the old logical ID with the new logical ID.

    Resources:  
      S3NewBucket: #The changed logical ID of the resource  
        Type: AWS::S3::Bucket  
    Outputs:  
      BucketName:  
        Value: !Ref S3NewBucket #Reference has been updated to point towards the new logical ID  
        Export:  
          Name: PrimaryBucket
  4. Update the CloudFormation stack with the edited template. The update recreates the deleted resource.

Related information

Update behaviors of stack resources

AWS OFFICIAL
AWS OFFICIALUpdated 2 months ago