Review CloudFormation resource update protection

Protect critical stack resources from unintended replacement or removal during updates.

Description

A CloudFormation stack policy limits resource changes, replacements and deletions during updates. If an Ansible task omits the policy, check the existing stack’s policy.

Potential impact

An unintended database replacement can cause downtime or data loss. A policy that omits necessary allows can also block routine updates.

Remediation

Use stack_policy_body for inline JSON or stack_policy for a policy file path. Explicitly allow required updates and deny the actions to block on protected logical IDs. Review the change set. Handle stack deletion, direct service API changes and recovery separately with termination protection, IAM and backups.

Examples

Supply the actual template URL. The separate disable_rollback setting controls rollback after a failed creation.

Before

yaml
- name: Create the CloudFormation stack
  amazon.aws.cloudformation:
    stack_name: ansible-cloudformation
    state: present
    region: us-east-1
    disable_rollback: true
    template_url: https://s3.amazonaws.com/my-bucket/cloudformation.template

After

yaml
- name: Create the CloudFormation stack
  amazon.aws.cloudformation:
    stack_name: ansible-cloudformation
    stack_policy_body: |
      {
        "Statement": [
          {
            "Effect": "Allow",
            "Action": "Update:*",
            "Principal": "*",
            "Resource": "*"
          },
          {
            "Effect": "Deny",
            "Action": "Update:*",
            "Principal": "*",
            "Resource": "LogicalResourceId/PrimaryDatabase"
          }
        ]
      }
    state: present
    region: us-east-1
    disable_rollback: true
    template_url: https://s3.amazonaws.com/my-bucket/cloudformation.template

The second policy allows Update:* generally and denies it for PrimaryDatabase. Match that name to the logical ID you intend to protect.

References