Certopolis

AWS Certified CloudOps Engineer – Associate

CloudFormation DeletionPolicy vs stack policy vs protection

The CloudFormation documentation gives three separate controls. DeletionPolicy decides what happens to one resource when it leaves the stack: Delete, Retain or Snapshot. A stack policy blocks update actions on chosen resources during stack updates. Termination protection makes any attempt to delete the whole stack fail. None of them controls calls made outside CloudFormation.

What does DeletionPolicy do?

With no DeletionPolicy, CloudFormation deletes a resource when its stack is deleted. The exception is AWS::RDS::DBCluster, and AWS::RDS::DBInstance resources without a DBClusterIdentifier, where the default is Snapshot.

Retain keeps the resource and its contents and can be used on any resource type. Snapshot takes a snapshot before deletion on resources that support it, such as AWS::EC2::Volume, AWS::RDS::DBInstance and AWS::Redshift::Cluster. RetainExceptOnCreate behaves like Retain except when the operation that created the resource is rolled back.

The attribute also applies when a stack update removes the resource from the template. With Retain, the physical resource stays and only leaves CloudFormation's scope. It does not apply to replacement: when an update replaces a resource, the old physical resource is deleted.

Retained resources and snapshots keep incurring charges until you delete them. For a Secrets Manager secret, CloudFormation's default deletion uses the ForceDeleteWithoutRecovery flag.

What does a stack policy do?

A stack policy is a JSON document that says which update actions are allowed on which resources. A new stack has none, so every update is allowed. Once a policy is set, every resource is protected by default and any update not explicitly allowed is denied.

The actions are Update:Modify, Update:Replace, Update:Delete and Update:*. A Deny always overrides an Allow. Principal must be *, so one policy applies to every user; a stack has one policy only.

A stack policy applies only during stack updates. AWS describes it as a fail-safe against accidental updates, not an access control. To control who can act on a resource, use IAM.

What does termination protection do?

With termination protection enabled, a request to delete the stack fails and the stack, including its status, stays unchanged. It is disabled by default and can be turned on at creation or later.

The setting passes to nested stacks, and you change it on the root stack only. A stack update that removes a nested stack still deletes that nested stack.

Where the exam catches people

  • Removing a resource from the template to hand it to another team without Retain: the update deletes the resource.
  • Believing DeletionPolicy: Retain survives replacement: when an update replaces a resource, the old physical resource is deleted.
  • Believing a stack policy stops a console or API delete of the resource itself: it applies only during stack updates. Use an IAM deny.
  • Believing termination protection protects individual resources: it only makes attempts to delete the stack fail.
  • Assuming an RDS DB instance is deleted outright with its stack: its default policy is Snapshot unless it belongs to a cluster.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. An Amazon DynamoDB table belongs to a CloudFormation stack, and its resource carries no policy attributes. Another team will take the table over, so a SysOps administrator must remove it from the stack's template while the table and its data stay in place. Each weekly release of the stack also changes several other resources. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Deployment, Provisioning, and Automation building.

    Not that one: D is right. In the game your Deployment, Provisioning, and Automation building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Setting DeletionPolicy: Retain in one release, then removing the table in the next, makes CloudFormation drop the table from the stack's scope and leave the table and its data where they are for the other team.

    Written from docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html

  2. A CloudFormation stack manages a production Amazon RDS DB instance and has no stack policy or service role. Operators use their own IAM role, and the release pipeline updates the stack with a separate role. Last month an operator deleted the DB instance in the Amazon RDS console by mistake. Operators must be unable to delete the instance by any means, while pipeline updates continue. Which action should the SysOps administrator take to meet these requirements?

    Right. In the game that’s Renown for your Deployment, Provisioning, and Automation building.

    Not that one: B is right. In the game your Deployment, Provisioning, and Automation building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The pipeline's role is unaffected.

    Written from docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/protect-stack-resources.html

  3. A company keeps its shared networking resources in one CloudFormation stack. Last week, in a test account, an operator deleted a similar stack after selecting the wrong stack in the console. A SysOps administrator must make sure that any attempt to delete the production networking stack fails and leaves the stack unchanged. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Deployment, Provisioning, and Automation building.

    Not that one: B is right. In the game your Deployment, Provisioning, and Automation building would lose a little Renown, and this question would come back later.

Ask for something