GreenDroid
GreenDroid

Reputation: 347

Is it possible to re-create AWS resources using CloudFormation?

Lets say an AWS stack was created using CloudFormation. Now one of those resources was modified outside CloudFormation.

1) Is it possible to have CloudFormation specifically create those resources? Based on my understanding, we can't do that because CloudFormation does not identify a difference, and so does not create the modified resources. Is my observation correct?

2) Also, what options do I have to revert a stack to its original state, if modified outside CloudFormation?

Upvotes: 8

Views: 9809

Answers (5)

Unfortunately, the answer is NO

  1. if you made changes in the stack after the creation, Cloudformation can't track those changes.

  2. if you need to revert those changes, you must delete the stack and rebuild.

Upvotes: 0

Sergio Barbosa
Sergio Barbosa

Reputation: 459

To force the EC2 re-creating, I do use a simple trick, when I'm deploying, I jump between AMI's IDs (I took two similar AMI's ID), that had helped me when I'm testing user data or things that I want to test during the EC2 bootstrap. Again, it just works for EC2.

Upvotes: 0

Aman Gupta
Aman Gupta

Reputation: 3797

This is one possible hack you could use without deleting the entire stack.

  1. From the template remove the specific resource which got deleted accidentally.
  2. Now update the stack which makes your stack and resources in your account in sync.
  3. Revert the template to its state before step1 and update again which will create the resource which got deleted accidentally.

Upvotes: 8

wjordan
wjordan

Reputation: 20380

First, beware that modifying CloudFormation-created resources outside of CloudFormation is explicitly discouraged, according to AWS CloudFormation Best Practices:

Manage All Stack Resources Through AWS CloudFormation

After you launch a stack, use the AWS CloudFormation console, API, or AWS CLI to update resources in your stack. Do not make changes to stack resources outside of AWS CloudFormation. Doing so can create a mismatch between your stack's template and the current state of your stack resources, which can cause errors if you update or delete the stack.

However, if you've modified a CloudFormation-managed resource accidentally and need to recover, you may have some limited options beyond simply deleting and re-creating the stack altogether (which may not be an acceptable option):

  1. It is not possible for CloudFormation to automatically update its internal state based on the current state of an externally-modified resource.

    However, depending on the exact resource type, in some cases you can manually update CloudFormation afterwards by applying a stack update that matches the current state of the resource.

  2. Similarly, it is not possible for CloudFormation to automatically revert an externally-modified resource back to its original unmodified CloudFormation state.

    However, depending on the exact resource type, in some cases you can either:

    • Revert a resource by manually updating the resource back to its original state;
    • Update the resource by applying a stack update, bringing both the CloudFormation stack and the managed resource to an altogether new state that will once again be in sync.

Upvotes: 1

helloV
helloV

Reputation: 52375

Unfortunately the answer for both your questions is NO.

  1. If you modify the resources in the stack after stack creation status is COMPLETE, there is nothing CF can do since it doesn't keep track of modification to resources
  2. You have no option other than deleting the current stack and create a new one

Upvotes: 4

Related Questions