aws-events-target: support adding CloudWatchLogGroup rule target without creation of new Resource Policy
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the feature
The user should be able to override the creation of a new CloudWatch Log Resource Policy when defining a Log Group as the target of an EventBridge rule.
### Use Case
When a CloudWatch LogGroup is defined as the target of an EventBridge rule, CDK creates a CloudWatch Log Resource Policy in order to allow EventBridge to write events to CloudWatch. Since there is a hard limit of 10 CloudWatch Log Resource Policies per account per region, this is limiting the amount of EventBridge rules that can be logged to CloudWatch, especially considering one Resource Policy should be able to dictate access to EventBridge to write to several Log Groups.
Consider the following policies that were created by CDK:
```
{
"policyName": "mycdkappEventsLogGroupPolicymycdkappRule1D26B095CB8287BF4",
"policyDocument": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"events.amazonaws.com\"},\"Action\":[\"logs:PutLogEvents\",\"logs:CreateLogStream\"],\"Resource\":\"arn:aws:logs:us-east-1:*:log-group:/aws/events/rule-1-messages:*\"}]}",
"lastUpdatedTime": 1725997880957
},
{
"policyName": "mycdkappEventsLogGroupPolicymycdkappRule278393ACD2E613614",
"policyDocument": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"events.amazonaws.com\"},\"Action\":[\"logs:PutLogEvents\",\"logs:CreateLogStream\"],\"Resource\":\"arn:aws:logs:us-east-1:*:log-group:/aws/events/rule-2-messages:*\"}]}",
"lastUpdatedTime": 1725997881143
},
{
"policyName": "mycdkappEventsLogGroupPolicymycdkappRule35B273849B4FB4310",
"policyDocument": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"events.amazonaws.com\"},\"Action\":[\"logs:PutLogEvents\",\"logs:CreateLogStream\"],\"Resource\":\"arn:aws:logs:us-east-1:*:log-group:/aws/events/rule-3-messages:*\"}]}",
"lastUpdatedTime": 1725997880890
}
```
These three CloudWatch Logs Resource Policies can be simplified and replaced by just one policy:
```
{
"policyName": "TrustEventsToStoreLogEvents",
"policyDocument": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"TrustEventsToStoreLogEvent\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"delivery.logs.amazonaws.com\",\"events.amazonaws.com\"]},\"Action\":[\"logs:CreateLogStream\",\"logs:PutLogEvents\"],\"Resource\":\"arn:aws:logs:us-east-1:*:log-group:/aws/events/*:*\"}]}",
"lastUpdatedTime": 1721857860333
}
```
### Proposed Solution
A property could be added to LogGroupProps interface (below) which allows the consumer to opt out of the Resource Policy creation.
https://github.com/aws/aws-cdk/blob/1e203753519e10e19ef0db87e1382377b609bcaa/packages/aws-cdk-lib/aws-events-targets/lib/log-group.ts#L62-L89
Then in the if statement (below) which checks for the existence of the Resource Policy CDK is trying to create, a condition could be added to that check to see whether the new property is true or false.
https://github.com/aws/aws-cdk/blob/1e203753519e10e19ef0db87e1382377b609bcaa/packages/aws-cdk-lib/aws-events-targets/lib/log-group.ts#L118-L128
### Other Information
_No response_
### Acknowledgements
- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
2.157.0
### Environment details (OS name and version, etc.)
macOS Ventura 13.4 (Intel processor)
Contributor guide
Research direction
Start with packages/aws-cdk-lib/aws-events-targets/lib/log-group.ts, especially the LogGroupProps interface and the resource-policy condition at the linked lines. Add the opt-out behavior described in the issue and verify that a CloudWatch LogGroup target can skip creation of a new resource policy while the existing behavior remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100