From dec21e2e6e12df733f5e2ef70dd98b871bab2722 Mon Sep 17 00:00:00 2001 From: watany <76135106+watany-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 02:26:28 +0000 Subject: [PATCH] AspectPriority --- .../aws-cdk-lib/core/lib/removal-policies.ts | 18 ++++++++++++------ .../core/test/removal-policies.test.ts | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/aws-cdk-lib/core/lib/removal-policies.ts b/packages/aws-cdk-lib/core/lib/removal-policies.ts index 4cc802113a132..f31434f3bf7fd 100644 --- a/packages/aws-cdk-lib/core/lib/removal-policies.ts +++ b/packages/aws-cdk-lib/core/lib/removal-policies.ts @@ -1,5 +1,5 @@ import { IConstruct } from 'constructs'; -import { Aspects, IAspect } from './aspect'; +import { Aspects, IAspect, AspectPriority } from './aspect'; import { CfnResource } from './cfn-resource'; import { RemovalPolicy } from './removal-policy'; @@ -28,11 +28,16 @@ export interface RemovalPolicyProps { * @default false - do not overwrite user-specified policies */ readonly overwrite?: boolean; + + /** + * The priority to use when applying this aspect. + * If multiple aspects apply conflicting settings, the one with the higher priority wins. + * + * @default - AspectPriority.MUTATING + */ + readonly priority?: number; } -/** - * The RemovalPolicyAspect handles applying a removal policy to resources - */ class RemovalPolicyAspect implements IAspect { constructor( private readonly policy: RemovalPolicy, @@ -46,7 +51,6 @@ class RemovalPolicyAspect implements IAspect { if (!patterns || patterns.length === 0) { return false; } - return patterns.includes(resourceType); } @@ -103,7 +107,9 @@ export class RemovalPolicies { * @param props Configuration options */ public apply(policy: RemovalPolicy, props: RemovalPolicyProps = {}) { - Aspects.of(this.scope).add(new RemovalPolicyAspect(policy, props)); + Aspects.of(this.scope).add(new RemovalPolicyAspect(policy, props), { + priority: props.priority ?? AspectPriority.MUTATING, + }); } /** diff --git a/packages/aws-cdk-lib/core/test/removal-policies.test.ts b/packages/aws-cdk-lib/core/test/removal-policies.test.ts index eb096c99119da..09449905123ec 100644 --- a/packages/aws-cdk-lib/core/test/removal-policies.test.ts +++ b/packages/aws-cdk-lib/core/test/removal-policies.test.ts @@ -220,4 +220,18 @@ describe('removal-policys', () => { expect(bucket.cfnOptions.deletionPolicy).toBe('Retain'); expect(table.cfnOptions.deletionPolicy).toBe('RetainExceptOnCreate'); }); + + test('higher priority removal policy overrides lower priority removal policy', () => { + // GIVEN + const stack = new Stack(); + const resource = new TestResource(stack, 'PriorityResource'); + + // WHEN + RemovalPolicies.of(stack).retainOnUpdateOrDelete({ priority: 250 }); + RemovalPolicies.of(stack).destroy({ priority: 10 }); + + // THEN + synthesize(stack); + expect(resource.cfnOptions.deletionPolicy).toBe('Delete'); + }); });