Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable termination protection of a stack #1682

Closed
Visorgood opened this issue Feb 5, 2019 · 15 comments · Fixed by #7610
Closed

Enable termination protection of a stack #1682

Visorgood opened this issue Feb 5, 2019 · 15 comments · Fixed by #7610
Assignees
Labels
@aws-cdk/core Related to core CDK functionality effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. in-progress This issue is being actively worked on.

Comments

@Visorgood
Copy link

Hello,

in CloudFormation there is a feature of termination protection:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-protect-stacks.html
https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html see --enable-termination-protection

Would it be possible to implement this feature in CDK so that one can set this flag and stack wouldn't be allow to be destroyed?

@eladb eladb added the feature-request A feature should be added or improved. label Feb 6, 2019
@Visorgood
Copy link
Author

To add some details - if I create a stack with CDK using cdk deploy, then enable termination protection of this stack using AWS UI or API, and then try to do cdk destroy, I will receive an error with a message: Stack [...] cannot be deleted while TerminationProtection is enabled. That is very important feature for stateful stacks. It would be great to have this flag as a part of StackProps.

@mhuebner
Copy link

+1

@IsmaelMartinez
Copy link
Contributor

@eladb , I think I might be able to pick this one up... but will need to have a bit of a view on integration tests.

If this is only for the client (cdk deploy --enable-termination-protection), it should not take me long.
If it is both, meaning having something in the constructor, it will probably grow arms and legs and take a couple of months as it did happen with the tags.

As a workaround you can run the aws cli or sdk:
https://docs.aws.amazon.com/cli/latest/reference/cloudformation/update-termination-protection.html
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudformation.html#CloudFormation.Client.update_termination_protection

@jewelsjacobs
Copy link

jewelsjacobs commented Sep 3, 2019

Hi folks.

This is a feature I'm also interested in. I was under the impression I could tag native CF commands on to the cdk cli but was disappointed to find that:

cdk deploy --enable-termination-protection (and different variations along those lines) did not work

I thought a cool workaround would be implementing some sort of cloudformation:DeleteStack Effect.DENY type Policy Doc. I was inspired by the iam policy document test in the cdk source: https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-iam/test/policy-document.test.ts#L62

Unfortunately my attempts to do anything similar were not successful. I couldn't get the IAM changes to show up in the template, nor could I block any type of stack deletion attempts.

Here are my gists where I POCed it in a bare bones new cdk init app:

My preferable solution in the end was to use an AWSCustomResource. It works very nicely.
https://gist.github.com/jewelsjacobs/69dcad5e398075f60628322a591b105d

Sorry if this seems verbose but I'm including all of this info to:

  1. Let you know either the policy doc or cli option would have worked for my use case
  2. Give googlers something to search with :)

@SomayaB SomayaB added needs-triage This issue or PR still needs to be triaged. gap labels Sep 9, 2019
@SomayaB SomayaB added @aws-cdk/core Related to core CDK functionality and removed gap labels Sep 18, 2019
@NGL321
Copy link
Contributor

NGL321 commented Oct 14, 2019

Hey @Visorgood,

Sorry for so long without a response. This seems like a totally reasonable feature!
If you would like to add it, please feel free to put in a PR. Otherwise somebody will update this issue when there is a change in status.

😸

@NGL321 NGL321 removed the needs-triage This issue or PR still needs to be triaged. label Oct 14, 2019
@NGL321
Copy link
Contributor

NGL321 commented Oct 17, 2019

@eladb Insight on integration tests for @IsmaelMartinez?

@IsmaelMartinez
Copy link
Contributor

Hi @NGL321, I assume I can use the ones in https://github.com/aws/aws-cdk/tree/master/packages/aws-cdk/test/integ/cli as a template... but let me know if there is a better guide.

As far as I can see in here https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-accidental-updates/ the only way to enable termination protection is either by the UI or client, there is no Cloudformation attribute for it (but I am not an expert on cloudformation)

If I don't read it wrong, adding a policy will prevent from elements on your stack been deleted once you delete the stack itself. I don't think we are talking about that case in here.

Let me know if my assumptions are correct and I can help (or work on) this. https://github.com/aws/aws-cdk/blob/master/packages/aws-cdk/bin/cdk.ts

@aperuru
Copy link

aperuru commented Nov 11, 2019

Is there any time-line on this issue, expected release version/month ?

@jayant141191
Copy link

When is this feature expected to release?

@IsmaelMartinez
Copy link
Contributor

IsmaelMartinez commented Nov 28, 2019

I am waiting for a response... and then I might be able to dedicate some time to it. Either of you feel free to make the changes and I am happy to help with the little that I know.

@jogold
Copy link
Contributor

jogold commented Nov 28, 2019

@IsmaelMartinez I think the best approach here would be to add a --termination-protection option to deploy and pass this down to deployStack:

export async function deployStack(options: DeployStackOptions): Promise<DeployStackResult> {

There you could call updateTerminationProtection.

@jogold
Copy link
Contributor

jogold commented Nov 28, 2019

But this would maybe not fit with #3437

@IsmaelMartinez
Copy link
Contributor

@IsmaelMartinez I think the best approach here would be to add a --termination-protection option to deploy and pass this down to deployStack:

export async function deployStack(options: DeployStackOptions): Promise<DeployStackResult> {

There you could call updateTerminationProtection.

Thanks for the reply. Yeah, I thought about going down that route.

The question is more, do we want to also add a policy to stop accidental delete of elements in your stack?

If I the functionality, the enable termination protection stops you from deleting the stack, but you can delete all the elements in a stack if you want.

If we only enable the termination protection, as it is, I don’t think it should have much impact in #3437 (but I might be wrong!).

@eladb eladb added the effort/small Small work item – less than a day of effort label Jan 23, 2020
@alokstage
Copy link

do we know any tentative date when this feature is releasing?

jogold added a commit to jogold/aws-cdk that referenced this issue Apr 26, 2020
Add a `terminationProtection` prop to `StackProps` to enable stack termination
protection.

Closes aws#1682
jogold added a commit to jogold/aws-cdk that referenced this issue Apr 26, 2020
Add a `terminationProtection` prop to `StackProps` to enable stack termination
protection.

Closes aws#1682
@SomayaB SomayaB added the in-progress This issue is being actively worked on. label Apr 28, 2020
@moatazelmasry2
Copy link
Contributor

I just submitted PR #7748 that fulfills this request and kinda followed @jogold suggestion on implementing that. Please let me know what you think.

@mergify mergify bot closed this as completed in #7610 May 4, 2020
mergify bot pushed a commit that referenced this issue May 4, 2020
Add a `terminationProtection` prop to `StackProps` to enable stack termination
protection.

This does not require extra IAM permission for existing CDK stacks
(`cloudformation:UpdateTerminationProtection`).

The logic to evaluate if we can skip deploy is now moved to a separate
function.

Closes #1682
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. in-progress This issue is being actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.