# GR-06

stateful resources keep their data. Retain on delete and replace; symmetric KMS keys rotate.

| Property | Value |
| --- | --- |
| Runs on | Every CloudFormation template, in CI on each change and in the pipeline's Validate stage |
| Source | `guard/rules/gr-06-stateful-retain.guard` |
| Exceptions | none |

## Rule text

```
# GR-06: stateful resources keep their data. Retain on delete and replace; symmetric KMS keys rotate.
# Asymmetric keys cannot rotate automatically: record the exception in guard/exceptions.yaml (DNSSEC KSK, D51).
let stateful = Resources.*[ Type in ['AWS::S3::Bucket', 'AWS::KMS::Key', 'AWS::DynamoDB::Table'] ]
let sym_keys = Resources.*[ Type == 'AWS::KMS::Key'
  Properties.KeySpec not exists or Properties.KeySpec == 'SYMMETRIC_DEFAULT' ]

rule GR_06_retain when %stateful !empty {
  %stateful {
    DeletionPolicy == 'Retain'     <<GR-06: DeletionPolicy must be Retain>>
    UpdateReplacePolicy == 'Retain' <<GR-06: UpdateReplacePolicy must be Retain>>
  }
}

rule GR_06_rotation when %sym_keys !empty {
  %sym_keys { Properties.EnableKeyRotation == true <<GR-06: symmetric KMS keys must enable rotation>> }
}
```
