Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event bus component #327

Closed

Conversation

thelegendtubaguy
Copy link

@thelegendtubaguy thelegendtubaguy commented Apr 29, 2024

Hey there! I thought I might try my hand at this, seems like an easy component. Basically just needs a name.

Closes #326

Hey there!  I thought I might try my hand at this, seems like an easy component.  Basically just needs a name. Not sure I have everything, especially doc wise.
@san4d
Copy link
Contributor

san4d commented Apr 30, 2024

I have something similar I've been working with. Publishing is straight-forward, but I found subscribing to have a bit of boilerplate. My implementation is limited to SQS based on my usage, but I'll share here:

export interface EventBusRule {
    name: string
    /**
     * Represents an [EventBridge Pattern](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)
     */
    pattern?: {
        source?: string[]
        detail?: { [key: string]: any }
        detailType?: string[]
    }
    /**
     *
     */
    target: {
        arn: Input<string>
    }
}
export class EventBus extends Component implements Link.Linkable, Link.AWS.Linkable {
        private bus: aws.cloudwatch.EventBus
        private busName: string

        ....
        
        public addSubscriber(rule: EventBusRule) {
        const name = `${this.busName}${rule.name}`
        const ruleName = prefixName(128, name)
        const targetName = prefixName(64, name)

        const eventPattern: Record<string, unknown> = { ...rule.pattern }
        if (!!rule.pattern?.detailType) {
            eventPattern['detail-type'] = rule.pattern.detailType
            delete eventPattern['detailType']
        }
        eventPattern

        const cwRule = new aws.cloudwatch.EventRule(`${name}Rule`, {
            eventPattern: JSON.stringify(eventPattern),
            eventBusName: this.bus.name,
            name: ruleName,
        })

        new aws.cloudwatch.EventTarget(`${name}Target`, {
            arn: rule.target.arn,
            rule: cwRule.name,
            targetId: targetName,
            eventBusName: this.bus.name,
        })
    }
}

Feel free to use as much or as little as makes sense.

@thelegendtubaguy
Copy link
Author

Looks like they're adding it... without commenting here. :)

@jayair
Copy link
Contributor

jayair commented May 20, 2024

Yeah still working on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: SST EventBridge Bus component and linking capability
3 participants