Skip to content

Commit 9a76fdc

Browse files
authored
fix(core): implicit Aspect applications do not override custom Aspect applications (#34132)
Some CDK methods apply mutating Aspects on behalf of users. Since #32333, these Aspects have a priority of `MUTATING` to classify their behavior. If a user-applied Aspect (priority `DEFAULT`) now configures the same property as an implicitly added Aspect: * Before that change, the relative execution order depended on the location of the Aspects in the construct tree. * After that change, the user Aspect always "wins" (executes last) because its priority is higher. In this change, we roll back to the behavior from pre-2.172.0, and introduce a feature flag which gives the Aspects a priority only if the feature flag is enabled. This introduces the feature flag: ```json { "context": { "@aws-cdk/core:aspectPrioritiesMutating": true } } ``` Which sets the priority of Aspects added on your behalf a priority of `MUTATING` (200) (instead of the default `DEFAULT`, 500). * If you have given your own Aspect a priority of `MUTATING` already to make sure it can get overridden by another Aspect of priority `MUTATING`, this current change will not affect you (either with or without feature flag). * If you have come to rely on the new default priority being low already, you can set the above feature flag to re-enable the new behavior. ----------- Did not touch the following Aspects: - In `integ-tests-alpha`: overriding logical IDs in assertions stacks does not affect production infrastructure. - Tags: tags are exclusively manipulated through the official APIs, so there no conflict between custom and implicit Aspects. - CDK Pipelines: there cannot be a conflict because the customer can't create a default pipeline before the implicit Aspect. This PR also introduces some slight rendering and documentation changes to the feature flags to improve clarity of the purpose of certain fields and the produced report.
1 parent c5365a0 commit 9a76fdc

File tree

27 files changed

+654
-272
lines changed

27 files changed

+654
-272
lines changed

packages/@aws-cdk/aws-servicecatalogappregistry-alpha/lib/application-associator.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as cdk from 'aws-cdk-lib/core';
2+
import * as cxapi from 'aws-cdk-lib/cx-api';
23
import { Construct } from 'constructs';
34
import { IApplication } from './application';
45
import { CheckedStageStackAssociator } from './aspects/stack-associator';
@@ -50,7 +51,9 @@ export class ApplicationAssociator extends Construct {
5051
this.associateCrossAccountStacks = targetBindResult.associateCrossAccountStacks;
5152
cdk.Aspects.of(scope).add(new CheckedStageStackAssociator(this, {
5253
associateCrossAccountStacks: this.associateCrossAccountStacks,
53-
}), { priority: cdk.AspectPriority.MUTATING });
54+
}), {
55+
priority: cdk.FeatureFlags.of(this).isEnabled(cxapi.ASPECT_PRIORITIES_MUTATING) ? cdk.AspectPriority.MUTATING : undefined,
56+
});
5457
}
5558

5659
/**
@@ -61,7 +64,9 @@ export class ApplicationAssociator extends Construct {
6164
this.associatedStages.add(stage);
6265
cdk.Aspects.of(stage).add(new CheckedStageStackAssociator(this, {
6366
associateCrossAccountStacks: this.associateCrossAccountStacks,
64-
}), { priority: cdk.AspectPriority.MUTATING });
67+
}), {
68+
priority: cdk.FeatureFlags.of(this).isEnabled(cxapi.ASPECT_PRIORITIES_MUTATING) ? cdk.AspectPriority.MUTATING : undefined,
69+
});
6570
return stage;
6671
}
6772

packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import * as iam from '../../aws-iam';
1717
import * as sns from '../../aws-sns';
1818
import {
1919
Annotations,
20-
AspectPriority,
2120
Aspects,
2221
Aws,
2322
CfnAutoScalingRollingUpdate, CfnCreationPolicy, CfnUpdatePolicy,
@@ -26,6 +25,7 @@ import {
2625
Tokenization, UnscopedValidationError, ValidationError, withResolved,
2726
} from '../../core';
2827
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
28+
import { mutatingAspectPrio32333 } from '../../core/lib/private/aspect-prio';
2929
import { AUTOSCALING_GENERATE_LAUNCH_TEMPLATE } from '../../cx-api';
3030

3131
/**
@@ -1608,7 +1608,9 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
16081608
this.spotPrice = props.spotPrice;
16091609

16101610
if (props.requireImdsv2) {
1611-
Aspects.of(this).add(new AutoScalingGroupRequireImdsv2Aspect(), { priority: AspectPriority.MUTATING });
1611+
Aspects.of(this).add(new AutoScalingGroupRequireImdsv2Aspect(), {
1612+
priority: mutatingAspectPrio32333(this),
1613+
});
16121614
}
16131615

16141616
this.node.addValidation({ validate: () => this.validateTargetGroup() });

packages/aws-cdk-lib/aws-backup/lib/selection.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { BackupableResourcesCollector } from './backupable-resources-collector';
44
import { IBackupPlan } from './plan';
55
import { BackupResource, TagOperation } from './resource';
66
import * as iam from '../../aws-iam';
7-
import { Lazy, Resource, Aspects, AspectPriority } from '../../core';
7+
import { Lazy, Resource, Aspects } from '../../core';
88
import { addConstructMetadata } from '../../core/lib/metadata-resource';
9+
import { mutatingAspectPrio32333 } from '../../core/lib/private/aspect-prio';
910

1011
/**
1112
* Options for a BackupSelection
@@ -143,7 +144,9 @@ export class BackupSelection extends Resource implements iam.IGrantable {
143144
}
144145

145146
if (resource.construct) {
146-
Aspects.of(resource.construct).add(this.backupableResourcesCollector, { priority: AspectPriority.MUTATING });
147+
Aspects.of(resource.construct).add(this.backupableResourcesCollector, {
148+
priority: mutatingAspectPrio32333(resource.construct),
149+
});
147150
// Cannot push `this.backupableResourcesCollector.resources` to
148151
// `this.resources` here because it has not been evaluated yet.
149152
// Will be concatenated to `this.resources` in a `Lazy.list`

packages/aws-cdk-lib/aws-ec2/lib/instance.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import { UserData } from './user-data';
1515
import { BlockDevice } from './volume';
1616
import { IVpc, Subnet, SubnetSelection } from './vpc';
1717
import * as iam from '../../aws-iam';
18-
import { Annotations, AspectPriority, Aspects, Duration, FeatureFlags, Fn, IResource, Lazy, Resource, Stack, Tags, Token } from '../../core';
18+
import { Annotations, Aspects, Duration, FeatureFlags, Fn, IResource, Lazy, Resource, Stack, Tags, Token } from '../../core';
1919
import { md5hash } from '../../core/lib/helpers-internal';
2020
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
21+
import { mutatingAspectPrio32333 } from '../../core/lib/private/aspect-prio';
2122
import * as cxapi from '../../cx-api';
2223

2324
/**
@@ -671,7 +672,9 @@ export class Instance extends Resource implements IInstance {
671672
}));
672673

673674
if (props.requireImdsv2) {
674-
Aspects.of(this).add(new InstanceRequireImdsv2Aspect(), { priority: AspectPriority.MUTATING });
675+
Aspects.of(this).add(new InstanceRequireImdsv2Aspect(), {
676+
priority: mutatingAspectPrio32333(this),
677+
});
675678
}
676679
}
677680

packages/aws-cdk-lib/aws-ecs/lib/cluster.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import {
2424
IAspect,
2525
Token,
2626
Names,
27-
AspectPriority,
2827
FeatureFlags, Annotations,
2928
} from '../../core';
3029
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
30+
import { mutatingAspectPrio32333 } from '../../core/lib/private/aspect-prio';
3131
import { Disable_ECS_IMDS_Blocking, Enable_IMDS_Blocking_Deprecated_Feature } from '../../cx-api';
3232

3333
const CLUSTER_SYMBOL = Symbol.for('@aws-cdk/aws-ecs/lib/cluster.Cluster');
@@ -331,7 +331,9 @@ export class Cluster extends Resource implements ICluster {
331331
// since it's harmless, but we'd prefer not to add unexpected new
332332
// resources to the stack which could surprise users working with
333333
// brown-field CDK apps and stacks.
334-
Aspects.of(this).add(new MaybeCreateCapacityProviderAssociations(this, id), { priority: AspectPriority.MUTATING });
334+
Aspects.of(this).add(new MaybeCreateCapacityProviderAssociations(this, id), {
335+
priority: mutatingAspectPrio32333(this),
336+
});
335337
}
336338

337339
/**

packages/aws-cdk-lib/aws-iam/lib/permissions-boundary.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { IConstruct } from 'constructs';
22
import { CfnRole, CfnUser } from './iam.generated';
33
import { IManagedPolicy } from './managed-policy';
4-
import { AspectPriority, Aspects, CfnResource } from '../../core';
4+
import { Aspects, CfnResource } from '../../core';
5+
import { mutatingAspectPrio32333 } from '../../core/lib/private/aspect-prio';
56

67
/**
78
* Modify the Permissions Boundaries of Users and Roles in a construct tree
@@ -40,7 +41,9 @@ export class PermissionsBoundary {
4041
node.addPropertyOverride('PermissionsBoundary', boundaryPolicy.managedPolicyArn);
4142
}
4243
},
43-
}, { priority: AspectPriority.MUTATING });
44+
}, {
45+
priority: mutatingAspectPrio32333(this.scope),
46+
});
4447
}
4548

4649
/**
@@ -56,6 +59,8 @@ export class PermissionsBoundary {
5659
node.addPropertyDeletionOverride('PermissionsBoundary');
5760
}
5861
},
59-
}, { priority: AspectPriority.MUTATING });
62+
}, {
63+
priority: mutatingAspectPrio32333(this.scope),
64+
});
6065
}
6166
}

0 commit comments

Comments
 (0)