Delete a resource from CloudFormation stack

0

Is it possible or is it on a roadmap to delete a specific resource from CloudFormation stack? This would be very useful if for whatever reason it is necessary to recreate a resource, without modifying it by hand and causing drift.

2 Answers
1

Hello,

If you update your current stack, omitting whatever resource you want to delete, you should be able to delete a specific resource you want to delete.

For example, let's say you already uploaded a CloudFormation template like the below.

So, when you update your stack, after deleting MyInstance2 lines, CloudFormation is smart enough to delete that resource only while updating.

Resources:

MyInstance: Type: AWS::EC2::Instance

Properties:
  InstanceType: t2.micro
  ImageId: ami-0af2f764c580cc1f9

MyInstance2: Type: AWS::EC2::Instance

Properties:
  InstanceType: t2.micro
  ImageId: ami-0af2f764c580cc1f9
answered a year ago
  • Yes, that's the current way to approach it. However, in stacks with hundreds of resources, this takes quite a lot of time. It would be great to have API for removing specific resource from a specific stack.

0

Resources belonging to a particular stack would be deleted either as a part of stack update or delete (UpdateStack / DeleteStack APIs). However there is no explicit API to remove a particular set of resources or resource types from a stack. It does makes sense for not having such a feature because such deletions could effect other resource(s) which are dependent on the resource(s) being deleted, or referenced in Outputs, etc.

Delete API calls can be made from corresponding service specific APIs, but if those resources are being managed by CloudFormation, they would be considered as out of band change (would cause drift on stack) and are not recommended.

Lastly, if there are a hundreds of resources (as per your latest comment) that are to be cleaned up you find it difficult to identify and delete them (during update/delete), you can also consider (if its feasible) to have them in a nested stack (AWS::CloudFormation::Stack resource), which would make it easier during deletions.

In case, the resources are NOT being managed by CloudFormation, CloudControl API (a centralised API proxy service). Ref : https://docs.aws.amazon.com/cloudcontrolapi/latest/userguide/resource-operations-delete.html

We strongly advise against using Cloud Control API to delete resources that are under active management by other services. Doing so can lead to unexpected results. For example, don't use Cloud Control API to delete resources that are currently part of an AWS CloudFormation stack.
AWS
SUPPORT ENGINEER
answered a year ago
  • Thanks for the inputs. The scenario is as mentioned of having hundred of resources, and knowing that exactly 1 needs to be removed. The suggestion of splitting the resources into nested stacks is interesting, but I am not sure if it will be decreasing the deployment time, as with my experience CloudFormation still needs to go through all the nested stacks to check if updates are needed.

  • And I understand the point about not being able to delete the resource. But then I would assume that the API call would fail. But having an API to delete a resource from the stack would be a great improvement for operations.

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.

Guidelines for Answering Questions