aws-iam: imported Group's default policy name is not stack-safe, so grants from one stack silently replace another's
Describe the bug
An IAM Group imported with Group.fromGroupArn() / Group.fromGroupName() creates its default inline policy with a hardcoded construct id ('DefaultPolicy'), so the generated policy name depends only on the path inside the stack. Importing the same group into two stacks with the same construct id therefore produces the same inline policy name on the same physical group.
An IAM inline policy is identified by (principal, policy name), so the stack that deploys second issues PutGroupPolicy with a name the first stack already used, and its document replaces the first stack's document. Nothing fails: both stacks report CREATE_COMPLETE / UPDATE_COMPLETE, and the permissions granted by the first stack are silently gone.
The identical bug for imported roles was fixed in #23080 by deriving the default policy name from the construct's path including the stack, behind the @aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName feature flag (packages/aws-cdk-lib/aws-iam/lib/private/imported-role.ts). Imported groups (and users, #37682) were never given the same treatment.
This has been reported before for groups and closed for staleness rather than fixed: #7483 (2020, "policy names attached to imported resources conflict with each other") and #29762 (2024, "Default policy replaced when group imported with from_group_arn"). In #7483 @rix0rrr noted "maybe we can make it slightly less likely to collide by adding in the stack name or something" — which is exactly what the role fix ended up doing.
Regression Issue
- Select this option if this issue appears to be a regression.
Last Known Working CDK Library Version
No response
Expected Behavior
Two stacks that import the same group and grant it different permissions should produce inline policies with different names, so each stack's grants survive independently — the behaviour imported roles already have.
Current Behavior
Both stacks synthesize the same policy name, so the second deployment overwrites the first stack's grants on the physical group.
Synthesizing the repro below:
### StackA
{
"LogicalId": "ImportedGroupDefaultPolicy5FF6A7A2",
"PolicyName": "ImportedGroupDefaultPolicy5FF6A7A2",
"Groups": [ "my-group" ]
}
### StackB
{
"LogicalId": "ImportedGroupDefaultPolicy5FF6A7A2",
"PolicyName": "ImportedGroupDefaultPolicy5FF6A7A2",
"Groups": [ "my-group" ]
}For comparison, an imported role in the same two stacks (with @aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName enabled, which is the default for new apps) correctly produces distinct names:
StackA: PolicyName = "PolicyStackAImportedRoleD6D6C9E0"
StackB: PolicyName = "PolicyStackBImportedRole2D16B130"Because the name is shared, deleting one of the stacks also deletes the inline policy the other stack believes it owns.
Reproduction Steps
import { App, Stack } from 'aws-cdk-lib';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
class GrantingStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);
const group = iam.Group.fromGroupArn(this, 'ImportedGroup', 'arn:aws:iam::123456789012:group/my-group');
const bucket = new s3.Bucket(this, 'Bucket');
bucket.grantRead(group);
}
}
const app = new App();
new GrantingStack(app, 'StackA');
new GrantingStack(app, 'StackB');cdk synth both stacks and compare the AWS::IAM::Policy resources: both carry PolicyName: "ImportedGroupDefaultPolicy5FF6A7A2" and Groups: ["my-group"].
Possible Solution
Mirror the imported-role fix in Group.fromGroupArn's imported class (packages/aws-cdk-lib/aws-iam/lib/group.ts), behind a new FlagType.BugFix feature flag so existing stacks keep their current policy names:
// packages/aws-cdk-lib/aws-iam/lib/private/imported-role.ts (existing, for reference)
const useUniqueName = FeatureFlags.of(this).isEnabled(IAM_IMPORTED_ROLE_STACK_SAFE_DEFAULT_POLICY_NAME);
let defaultDefaultPolicyName = useUniqueName ? `${prefix}${Names.uniqueId(this)}` : prefix;
if (defaultDefaultPolicyName.length > MAX_POLICY_NAME_LEN) {
defaultDefaultPolicyName = `${prefix}${Names.uniqueResourceName(this, { maxLength: MAX_POLICY_NAME_LEN - prefix.length })}`;
}
const policyName = this.defaultPolicyName ?? defaultDefaultPolicyName;
this.defaultPolicy = new Policy(this, policyName, useUniqueName ? { policyName } : undefined);The same change applies to imported users (#37682), which is the identical bug in User.fromUserAttributes. PR #37683 took this approach for both principal types; it was auto-closed for merge conflicts and a missing integ snapshot rather than on design grounds.
Workaround until then: give the imported construct a stack-unique id, or attach an explicitly named policy with group.attachInlinePolicy(new iam.Policy(this, 'P', { policyName: ${Stack.of(this).stackName}Grants, statements: [...] })).
Additional Information/Context
I would like to work on this, covering imported groups and imported users (#37682) in one change.
AWS CDK Library version (aws-cdk-lib)
2.270.0
AWS CDK CLI version
2.1142.0
Node.js Version
v24.1.0
OS
macOS
Language
TypeScript
Language Version
TypeScript 5.x
Other information
No response
Source: aws/aws-cdk