Skip to content

Commit

Permalink
fix(events): imported event rule ignores environment from arn (#27907)
Browse files Browse the repository at this point in the history
This PR fixes the bug that `fromEventRuleArn` does not extract region and account from the arn.

- Behavior until now

```ts
    const stack = new Stack(); // <- region: us-east-1, account: 123456789012
    const imported = Rule.fromEventRuleArn(stack, 'Imported', 'arn:aws:events:us-west-2:999999999999:rule/example');

    // imported.env.region : us-east-1("Ref": "AWS::Region"). But it should be us-west-2.
    // imported.env.account : 123456789012("Ref": "AWS::AccountId"). But it should be 999999999999.
```

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
go-to-k committed Nov 10, 2023
1 parent 31a449e commit bfbe756
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/aws-cdk-lib/aws-events/lib/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export class Rule extends Resource implements IRule {
public ruleArn = eventRuleArn;
public ruleName = parts.resourceName || '';
}
return new Import(scope, id);
return new Import(scope, id, {
environmentFromArn: eventRuleArn,
});
}

public readonly ruleArn: string;
Expand Down
14 changes: 14 additions & 0 deletions packages/aws-cdk-lib/aws-events/test/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ describe('rule', () => {
expect(importedRule.ruleName).toEqual('example');
});

test('sets account for imported rule env by fromEventRuleArn', () => {
const stack = new cdk.Stack();
const importedRule = Rule.fromEventRuleArn(stack, 'Imported', 'arn:aws:events:us-west-2:999999999999:rule/example');

expect(importedRule.env.account).toEqual('999999999999');
});

test('sets region for imported rule env by fromEventRuleArn', () => {
const stack = new cdk.Stack();
const importedRule = Rule.fromEventRuleArn(stack, 'Imported', 'arn:aws:events:us-west-2:999999999999:rule/example');

expect(importedRule.env.region).toEqual('us-west-2');
});

test('rule can be disabled', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit bfbe756

Please sign in to comment.