EventBridge to SNS Topic

Hello,

I want to create the following resources:

  1. EventBridge Bus
  2. SNS Topic
  3. Lambda Function

I want to see a event pattern matching rule to the Bus, and add the SNS Topic as the target.
And I want to add the Lambda Function as the subscriber to the SNS Topic.

So the workflow is basically as follows:

EventBridge Bus => SNS Topic => Lambda


I’m trying to use EventBus and Topic constructs from @serverless-stack/resources, but it seems like the EventBus construct allows only Queue and Functilon constructs to be added as targets.
This is what my stack looks like right now, and I couldn’t manage to achieve what I want:

import { App, Stack, StackProps, Topic, EventBus } from "@serverless-stack/resources";


export class MainStack extends Stack {

      constructor(scope: App, id: string, props?: StackProps) {
            super(scope, id, props);

            // The SNS Topic that EventBridge Bus targets
            const snsTopic = new Topic(this, "Topic", {
                  subscribers: ["src/sns/fallback/index.main"],
                  snsTopic: {
                        topicName: 'FallbackTopic',
                        displayName: 'Fallback'
                  }
            });

            // The EventBus that targets the SNS Topic
            const eventBus = new EventBus(this, id, {
                  eventBridgeEventBus: {
                        eventBusName: 'FallbackBus'
                  },
                  rules: {
                        fallbackEventRule: {
                              description: 'The rule to match with events that comes from fallback handler',
                              ruleName: 'fallbackEventRule',
                              eventPattern: {
                                    account: [scope.account],

                              },
                        }
                  }
            })


            this.addOutputs({
                  EventBusName: eventBus.eventBusName,
                  SNSTopicName: snsTopic.topicName
            });
      }
}

How can I add the SNS Topic as the target?

For those looking for solution:

Install @aws-cdk/aws-events and @aws-cdk/aws-events-targets:

npx sst add-cdk @aws-cdk/aws-events @aws-cdk/aws-events-targets

Then in one of the file inside stacks/ folder:

import * as sst from '@serverless-stack/resources'
import * as events from '@aws-cdk/aws-events'
import * as targets from '@aws-cdk/aws-events-targets'

export class MainStack extends sst.Stack {
      constructor(scope: sst.App, id: string, props?: sst.StackProps) {
            super(scope, id, props)

            ///////////////////////////////////////////////////////////
            // Create EventBus
            ///////////////////////////////////////////////////////////
            const eventBus = new events.EventBus(this, id, {
                  eventBusName: 'TheNameOfTheBus'
            })

            ///////////////////////////////////////////////////////////
            // Create SNS Topic to be used as EventBridge Target
            // and Lambda Event Source
            ///////////////////////////////////////////////////////////
            const snsTopic = new sst.Topic(this, 'SNSTopic', {
                  snsTopic: {
                        topicName: 'TheNameOfTheTopic'
                  }
            })

            ///////////////////////////////////////////////////////////
            // Create SNS Target from the topic for EventBridge
            ///////////////////////////////////////////////////////////
            const snsTarget = new targets.SnsTopic(snsTopic.snsTopic)

            ///////////////////////////////////////////////////////////
            // Create EventBridge Rule to match the events to targets
            ///////////////////////////////////////////////////////////

            new events.Rule(this, 'MyRule', {
                  description: 'Put events to SNS Topic',
                  eventBus,
                  ruleName: 'MyRuleName',
                  targets: [snsTarget]
            })

            ///////////////////////////////////////////////////////////
            // Outputs
            ///////////////////////////////////////////////////////////
            this.addOutputs({
                  EventBusName: eventBus.eventBusName,
                  EventBusArn: eventBus.eventBusArn,
                  SNSTopicName: snsTopic.topicName,
                  SNSTopicArn: snsTopic.topicArn
            })
      }
}