Skip to content

Commit

Permalink
feat(aws-eventbridge-lambda): Support for custom EventBus (#354)
Browse files Browse the repository at this point in the history
* enhanced construct to support custom EventBus

* fix linting issues

* fix PR review comments

* refactoring and use IEventBus instead of EventBus

* update the props naming convention to align with existing constructs

* updated the props documentation

* upated exisitingEventBusInterface & eventBusProps description

Co-authored-by: santhosh <>
  • Loading branch information
surukonda authored Sep 2, 2021
1 parent 876073b commit fd750a5
Show file tree
Hide file tree
Showing 14 changed files with 1,809 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ _Parameters_
|:-------------|:----------------|-----------------|
|existingLambdaObj?|[`lambda.Function`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html)|Existing instance of Lambda Function object, providing both this and `lambdaFunctionProps` will cause an error.|
|lambdaFunctionProps?|[`lambda.FunctionProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.FunctionProps.html)|User provided props to override the default props for the Lambda function.|
|existingEventBusInterface?|[`events.IEventBus`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-events.IEventBus.html)| Optional user-provided custom EventBus for construct to use. Providing both this and `eventBusProps` results an error.|
|eventBusProps?|[`events.EventBusProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-events.EventBusProps.html)|Optional user-provided properties to override the default properties when creating a custom EventBus. Setting this value to `{}` will create a custom EventBus using all default properties. If neither this nor `existingEventBusInterface` is provided the construct will use the `default` EventBus. Providing both this and `existingEventBusInterface` results an error.|
|eventRuleProps|[`events.RuleProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-events.RuleProps.html)|User provided eventRuleProps to override the defaults|

## Pattern Properties

| **Name** | **Type** | **Description** |
|:-------------|:----------------|-----------------|
|eventBus|[`events.IEventBus`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-events.IEventBus.html)|Returns the instance of events.IEventBus used by the construct|
|eventsRule|[`events.Rule`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-events.Rule.html)|Returns an instance of events.Rule created by the construct|
|lambdaFunction|[`lambda.Function`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html)|Returns an instance of lambda.Function created by the construct|

Expand All @@ -82,7 +85,7 @@ Out of the box implementation of the Construct without any override will set the
* Enable reusing connections with Keep-Alive for NodeJs Lambda function
* Enable X-Ray Tracing
* Set Environment Variables
* AWS_NODEJS_CONNECTION_REUSE_ENABLED (for Node 10.x and higher functions)
* AWS_NODEJS_CONNECTION_REUSE_ENABLED (for Node 10.x and higher functions)

## Architecture
![Architecture Diagram](architecture.png)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ export interface EventbridgeToLambdaProps {
* @default - Default props are used
*/
readonly lambdaFunctionProps?: lambda.FunctionProps,
/**
* Existing instance of a custom EventBus.
*
* @default - None
*/
readonly existingEventBusInterface?: events.IEventBus,
/**
* A new custom EventBus is created with provided props.
*
* @default - None
*/
readonly eventBusProps?: events.EventBusProps,
/**
* User provided eventRuleProps to override the defaults
*
Expand All @@ -45,6 +57,7 @@ export interface EventbridgeToLambdaProps {

export class EventbridgeToLambda extends Construct {
public readonly lambdaFunction: lambda.Function;
public readonly eventBus?: events.IEventBus;
public readonly eventsRule: events.Rule;

/**
Expand All @@ -70,7 +83,13 @@ export class EventbridgeToLambda extends Construct {
})
};

const defaultEventsRuleProps = defaults.DefaultEventsRuleProps([lambdaFunc]);
// build an event bus if existingEventBus is provided or eventBusProps are provided
this.eventBus = defaults.buildEventBus(this, {
existingEventBusInterface: props.existingEventBusInterface,
eventBusProps: props.eventBusProps
});

const defaultEventsRuleProps = defaults.DefaultEventsRuleProps([lambdaFunc], this.eventBus);
const eventsRuleProps = overrideProps(defaultEventsRuleProps, props.eventRuleProps, true);

this.eventsRule = new events.Rule(this, 'EventsRule', eventsRuleProps);
Expand Down
Loading

0 comments on commit fd750a5

Please sign in to comment.