Skip to content

Instantly share code, notes, and snippets.

@dmeehan1968
Created November 9, 2023 15:52
Show Gist options
  • Save dmeehan1968/7f033d7189a90020783dcc20dea61f55 to your computer and use it in GitHub Desktop.
Save dmeehan1968/7f033d7189a90020783dcc20dea61f55 to your computer and use it in GitHub Desktop.
Scheduler Target for EventBridge to PutEvents
import { ScheduleTargetBase, ScheduleTargetBaseProps } from "@aws-cdk/aws-scheduler-targets-alpha"
import { IRole, Role } from "aws-cdk-lib/aws-iam"
import { EventBus } from "aws-cdk-lib/aws-events"
import { ISchedule, ScheduleTargetConfig, ScheduleTargetInput } from "@aws-cdk/aws-scheduler-alpha"
/**
* This is a simple implementation of a EventBridge Scheduler Target that will put events to an EventBus.
* It is built upon the base provided by @aws-cdk/aws-scheduler-targets-alpha.
*/
export class EventBridgePutEvents<T extends Record<string, any>> extends ScheduleTargetBase {
source: string
detailType: string
constructor(
public readonly eventBus: EventBus,
props: EventBridgePutEventsProps<T>,
) {
const { source, detailType, detail, ...baseProps } = props
super({
input: ScheduleTargetInput.fromObject(detail),
...baseProps,
}, eventBus.eventBusArn)
this.source = source
this.detailType = detailType
}
protected bindBaseTargetConfig(_schedule: ISchedule): ScheduleTargetConfig {
return {
...super.bindBaseTargetConfig(_schedule),
eventBridgeParameters: {
source: this.source,
detailType: this.detailType,
},
}
}
protected addTargetActionToRole(schedule: ISchedule, role: IRole) {
this.eventBus.grantPutEventsTo(role)
}
}
export interface EventBridgePutEventsProps<T extends Record<string, any>> extends Omit<ScheduleTargetBaseProps, 'input'> {
source: string
detailType: string
detail: T
role?: Role
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment