Skip to content

Commit 3d7023d

Browse files
authored
fix: onEvent function to pass all the options to rule resource (#35829)
### Issue # (if applicable) N/A ### Reason for this change `onEvent()` has a lot of options other than description e.g. `ruleName` `detail` etc.. The old code didn't pass those values to rule, so it will never be propagated properly, so the consumer of this method will think that we're respecting their options, but in real we're discarding it. ### Description of changes Creating the rule first with all the options so all the options from the consumer get propagated properly to the rule, then add the specific things that you want. ### Describe any new or updated permissions being added N/A ### Description of how you validated changes Add unit tests ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent dad112e commit 3d7023d

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

packages/@aws-cdk/aws-bedrock-alpha/bedrock/agents/agent.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,15 @@ export abstract class AgentBase extends Resource implements IAgent {
133133
* @returns An EventBridge Rule configured for agent events
134134
*/
135135
public onEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
136-
// Create rule with minimal props and event pattern
137-
const rule = new events.Rule(this, id, {
138-
description: options.description,
139-
eventPattern: {
140-
source: ['aws.bedrock'],
141-
detail: {
142-
'agent-id': [this.agentId],
143-
},
136+
const rule = new events.Rule(this, id, options);
137+
rule.addEventPattern({
138+
source: ['aws.bedrock'],
139+
detail: {
140+
'agent-id': [this.agentId],
144141
},
145142
});
146143

147-
// Add target if provided
148-
if (options.target) {
149-
rule.addTarget(options.target);
150-
}
144+
rule.addTarget(options.target);
151145
return rule;
152146
}
153147

packages/@aws-cdk/aws-bedrock-alpha/test/bedrock/agents/agent.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,27 @@ describe('Agent', () => {
293293

294294
const rule = agent.onEvent('TestRule', {
295295
description: 'Custom rule description',
296+
ruleName: 'MyCustomEventRuleName',
297+
eventPattern: {
298+
account: ['123456789012'],
299+
region: ['us-east-1'],
300+
detail: {
301+
test: 'value',
302+
},
303+
},
296304
});
297305

298306
expect(rule).toBeDefined();
299307
Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', {
300308
Description: 'Custom rule description',
309+
Name: 'MyCustomEventRuleName',
301310
EventPattern: {
302311
source: ['aws.bedrock'],
303312
detail: {
304313
'agent-id': [{ 'Fn::GetAtt': [Match.stringLikeRegexp('TestAgent[A-Z0-9]+'), 'AgentId'] }],
305314
},
315+
account: ['123456789012'],
316+
region: ['us-east-1'],
306317
},
307318
});
308319
});

packages/@aws-cdk/example-construct-library/lib/example-resource.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,7 @@ abstract class ExampleResourceBase extends Resource implements IExampleResource
201201
* as it simplifies the implementation code (less branching).
202202
*/
203203
public onEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
204-
const rule = new events.Rule(this, id, {
205-
description: options.description,
206-
ruleName: options.ruleName,
207-
crossStackScope: options.crossStackScope,
208-
});
204+
const rule = new events.Rule(this, id, options);
209205
rule.addTarget(options.target);
210206
rule.addEventPattern({
211207
// obviously, you would put your resource-specific values here

packages/@aws-cdk/example-construct-library/test/example-resource.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,28 @@ describe('Example Resource', () => {
121121
});
122122

123123
test('onEvent adds an Event Rule', () => {
124-
exampleResource.onEvent('MyEvent');
124+
exampleResource.onEvent('MyEvent', {
125+
description: 'Custom rule description',
126+
ruleName: 'MyCustomEventRuleName',
127+
eventPattern: {
128+
account: ['123456789012'],
129+
region: ['us-east-1'],
130+
detail: {
131+
test: 'value',
132+
},
133+
},
134+
});
125135

126136
Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', {
137+
Description: 'Custom rule description',
138+
Name: 'MyCustomEventRuleName',
127139
EventPattern: {
128140
detail: {
129141
'example-resource-name': [EXAMPLE_RESOURCE_NAME],
142+
'test': 'value',
130143
},
144+
account: ['123456789012'],
145+
region: ['us-east-1'],
131146
},
132147
});
133148
});

0 commit comments

Comments
 (0)