Skip to content

fix(events): cannot specify a custom id on rule targets#37994

Open
tskawada wants to merge 1 commit into
aws:mainfrom
tskawada:fix/events-rule-target-id
Open

fix(events): cannot specify a custom id on rule targets#37994
tskawada wants to merge 1 commit into
aws:mainfrom
tskawada:fix/events-rule-target-id

Conversation

@tskawada
Copy link
Copy Markdown

Issue # (if applicable)

Closes #37025.

Reason for this change

The Id of an EventBridge Rule target is always auto-generated as Target0, Target1, ...
There is no way for users to specify a custom target id through the L2 Rule.addTarget API, even though the underlying PutTargets API allows it.
As a result, infrastructure imported into CDK that was originally created with a custom target id is reported as drifted by CloudFormation, since CDK overwrites the id at synth time.

Description of changes

Added an optional second argument AddTargetOptions to Rule.addTarget(target, options?) with an optional id field.
When provided, it is used as the CloudFormation target Id and takes precedence over both the auto-generated id and the deprecated RuleTargetConfig.id returned from IRuleTarget.bind(). The id is validated against the EventBridge constraints (1–64 chars, [A-Za-z0-9._-]); tokens are passed through.

The id passed to IRuleTarget.bind(rule, id) is intentionally left as the auto-generated id so derived resources (IAM roles, log groups, etc.) keep their existing logical ids. This keeps the change strictly opt-in.

The @deprecated note on RuleTargetConfig.id was updated to point to the new option, and a short Custom Target Id section was added to the aws-events README.

Alternatives considered: enriching RuleProps.targets with a { target, id } shape (rejected: public type change), and passing
the user id to bind() (rejected: would change derived resource logical ids for existing stacks).

Describe any new or updated permissions being added

N/A. Synth-time only; no runtime IAM permissions are added.

Description of how you validated changes

Added unit tests in aws-events/test/rule.test.ts covering: custom id is rendered, mixed custom and auto-generated ids, user id beats RuleTargetConfig.id, all allowed characters, validation failures (empty / too long / invalid chars), and Token ids deferred to synth.
All 345 existing aws-events tests continue to pass. Full aws-cdk-lib build (eslint, pkglint, awslint) passes.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions Bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels May 25, 2026
Copy link
Copy Markdown
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

✅ A exemption request has been requested. Please wait for a maintainer's review.

@tskawada
Copy link
Copy Markdown
Author

Exemption Request

This change adds an optional opt-in property whose effect is limited to a single string field of the synthesized loudFormation template (AWS::Events::Rule.Targets[].Id). The output is fully verified by unit tests using Template.fromStack().hasResourceProperties(). No runtime behavior, IAM permissions, or new dependencies are introduced, so an integration test would only re-verify what CloudFormation already guarantees for the field's character set. Requesting an exemption from the integration test requirement.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/medium Medium work item – several days of effort p2 pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aws-events: no way to set Id on Rule Target

2 participants