|
| 1 | +import { IScheduleTarget, ISchedule, ScheduleTargetInput, ScheduleTargetConfig } from '@aws-cdk/aws-scheduler-alpha'; |
| 2 | +import { Names } from 'aws-cdk-lib'; |
| 3 | +import * as events from 'aws-cdk-lib/aws-events'; |
| 4 | +import { IRole } from 'aws-cdk-lib/aws-iam'; |
| 5 | +import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target'; |
| 6 | +import { sameEnvDimension } from './util'; |
| 7 | + |
| 8 | +/** |
| 9 | + * An entry to be sent to EventBridge |
| 10 | + * |
| 11 | + * @see https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEventsRequestEntry.html |
| 12 | + */ |
| 13 | +export interface EventBridgePutEventsEntry { |
| 14 | + /** |
| 15 | + * The event body |
| 16 | + * |
| 17 | + * Can either be provided as an object or as a JSON-serialized string |
| 18 | + * @example |
| 19 | + * |
| 20 | + * ScheduleTargetInput.fromText('{"instance-id": "i-1234567890abcdef0", "state": "terminated"}'); |
| 21 | + * ScheduleTargetInput.fromObject({ Message: 'Hello from a friendly event :)' }); |
| 22 | + */ |
| 23 | + readonly detail: ScheduleTargetInput; |
| 24 | + |
| 25 | + /** |
| 26 | + * Used along with the source field to help identify the fields and values expected in the detail field |
| 27 | + * |
| 28 | + * For example, events by CloudTrail have detail type "AWS API Call via CloudTrail" |
| 29 | + * @see https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html |
| 30 | + */ |
| 31 | + readonly detailType: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * The event bus the entry will be sent to. |
| 35 | + * |
| 36 | + */ |
| 37 | + readonly eventBus: events.IEventBus; |
| 38 | + |
| 39 | + /** |
| 40 | + * The service or application that caused this event to be generated |
| 41 | + * |
| 42 | + * Example value: `com.example.service` |
| 43 | + * |
| 44 | + * @see https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html |
| 45 | + */ |
| 46 | + readonly source: string; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Send an event to an AWS EventBridge by AWS EventBridge Scheduler. |
| 51 | + */ |
| 52 | +export class EventBridgePutEvents extends ScheduleTargetBase implements IScheduleTarget { |
| 53 | + constructor( |
| 54 | + private readonly entry: EventBridgePutEventsEntry, |
| 55 | + private readonly props: ScheduleTargetBaseProps, |
| 56 | + ) { |
| 57 | + super(props, entry.eventBus.eventBusArn); |
| 58 | + if (this.props.input) { |
| 59 | + throw new Error('ScheduleTargetBaseProps.input is not supported for EventBridgePutEvents. Please use entry.detail instead.'); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + protected addTargetActionToRole(schedule: ISchedule, role: IRole): void { |
| 64 | + const eventBus = this.entry.eventBus; |
| 65 | + const eventBusEnv = eventBus.env; |
| 66 | + if (!sameEnvDimension(eventBusEnv.region, schedule.env.region)) { |
| 67 | + throw new Error(`Cannot assign eventBus in region ${eventBusEnv.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the eventBus must be in the same region.`); |
| 68 | + } |
| 69 | + |
| 70 | + if (!sameEnvDimension(eventBusEnv.account, schedule.env.account)) { |
| 71 | + throw new Error(`Cannot assign eventBus in account ${eventBusEnv.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the eventBus must be in the same account.`); |
| 72 | + } |
| 73 | + |
| 74 | + if (this.props.role && !sameEnvDimension(this.props.role.env.account, eventBusEnv.account)) { |
| 75 | + throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(eventBus.node)} in account ${eventBusEnv.account}. Both the target and the execution role must be in the same account.`); |
| 76 | + } |
| 77 | + |
| 78 | + eventBus.grantPutEventsTo(role); |
| 79 | + } |
| 80 | + |
| 81 | + protected bindBaseTargetConfig(_schedule: ISchedule): ScheduleTargetConfig { |
| 82 | + return { |
| 83 | + ...super.bindBaseTargetConfig(_schedule), |
| 84 | + input: this.entry.detail, |
| 85 | + eventBridgeParameters: { |
| 86 | + detailType: this.entry.detailType, |
| 87 | + source: this.entry.source, |
| 88 | + }, |
| 89 | + }; |
| 90 | + } |
| 91 | +} |
0 commit comments