aws / aws/aws-cdk

Automatic Policy Deduplication for AwsCustomResource

Open
#31,975 4 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-iam effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

When working on a CDK app where deploying around 200 IAM policies related to Organization Tags, along with 200 custom resources attached to a single IAM role. Customer is using the AwsCustomResourcePolicy construct to define the required permissions for these custom resources.

However, when passing this policy to the AwsCustomResource construct using props, each custom resource is creating a new policy and attaching it to the same role. Due to the character limit for policies, they encountering the following error:

```
Resource handler returned message: "Maximum policy size of 10240 bytes exceeded for role
```

### Use Case

As a workaround, we can create the role and relevant policies outside of the loop and then pass the role to customResources. This works, However, the expectation is that CDK custom resources should ideally handle this automatically by combining similar policies to avoid creating redundant ones

### Proposed Solution

_No response_

### Other Information

CDK code for replication:

```
import * as cdk from 'aws-cdk-lib';
import * as cr from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
import * as iam from 'aws-cdk-lib/aws-iam'

export class ReproduceIssueStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const customResourceRole = new iam.Role(this, 'CustomResourceRole', {
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
});

// Generate 200 dummy account IDs
const dummyAccountIds = Array.from({ length: 200 }, (_, i) => `${10000 + i}`);

const customResources = dummyAccountIds.map(accountId => {
return new cr.AwsCustomResource(this, `TagResource${accountId}`, {
onCreate: {
service: 'Organizations',
action: 'tagResource',
parameters: {
ResourceId: accountId,
Tags: [
{
Key: 'TestKey',
Value: 'TestValue'
}
]
},
physicalResourceId: cr.PhysicalResourceId.of(`TagResource${accountId}`)
},
role: customResourceRole
});
});

customResourceRole.addToPrincipalPolicy(
new iam.PolicyStatement({
actions: ['Organizations:tagResource'],
resources: ['*'],
})
);
}
}
```

### Acknowledgements

- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

2.124.0

### Environment details (OS name and version, etc.)

MacOS 14.7

Contributor guide

Open the contributing guide

Research direction

Start with the AwsCustomResourcePolicy and AwsCustomResource entry points and reproduce the 200-resource example from the issue. Determine how repeated policies are currently created and attached to the shared role, then define completion as avoiding redundant policy growth while preserving the required Organizations permissions and resolving the policy-size failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.