fix(events): cannot specify a custom id on rule targets#37994
Conversation
There was a problem hiding this comment.
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.
|
Exemption Request This change adds an optional opt-in property whose effect is limited to a single string field of the synthesized loudFormation template ( |
Issue # (if applicable)
Closes #37025.
Reason for this change
The
Idof an EventBridge Rule target is always auto-generated asTarget0,Target1, ...There is no way for users to specify a custom target id through the L2
Rule.addTargetAPI, even though the underlyingPutTargetsAPI 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
AddTargetOptionstoRule.addTarget(target, options?)with an optionalidfield.When provided, it is used as the CloudFormation target
Idand takes precedence over both the auto-generated id and the deprecatedRuleTargetConfig.idreturned fromIRuleTarget.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
@deprecatednote onRuleTargetConfig.idwas updated to point to the new option, and a shortCustom Target Idsection was added to theaws-eventsREADME.Alternatives considered: enriching
RuleProps.targetswith a{ target, id }shape (rejected: public type change), and passingthe 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.tscovering: custom id is rendered, mixed custom and auto-generated ids, user id beatsRuleTargetConfig.id, all allowed characters, validation failures (empty / too long / invalid chars), and Token ids deferred to synth.All 345 existing
aws-eventstests continue to pass. Fullaws-cdk-libbuild (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