Amazon EventBridge
EventBridge is a serverless event bus service that makes it easy to connect applications using events. Build event-driven architectures with decoupled microservices.
EventBridge Event-Driven Architecture
Quick Start
#r "../src/bin/Release/net8.0/publish/Amazon.JSII.Runtime.dll"
#r "../src/bin/Release/net8.0/publish/Constructs.dll"
#r "../src/bin/Release/net8.0/publish/Amazon.CDK.Lib.dll"
#r "../src/bin/Release/net8.0/publish/FsCDK.dll"
open FsCDK
open Amazon.CDK
open Amazon.CDK.AWS.Events
open Amazon.CDK.AWS.Events.Targets
open Amazon.CDK.AWS.Lambda
Scheduled Events
Run Lambda functions on a schedule using EventBridge rules.
stack "ScheduledEvents" {
// Lambda function
let processFunction =
lambda "ProcessDaily" {
runtime Runtime.DOTNET_8
handler "App::Handler"
code "./lambda"
}
// Run daily at midnight UTC
eventBridgeRule "DailyProcessing" {
description "Process data daily at midnight"
schedule (Schedule.Cron(CronOptions(Hour = "0", Minute = "0")))
target (LambdaFunction(processFunction.Function.Value))
}
}
Rate-Based Scheduling
Execute tasks at regular intervals.
stack "RateBasedEvents" {
let monitorFunction =
lambda "Monitor" {
runtime Runtime.DOTNET_8
handler "App::Monitor"
code "./lambda"
}
// Run every 5 minutes
eventBridgeRule "HealthCheck" {
description "Health check every 5 minutes"
schedule (Schedule.Rate(Duration.Minutes(5.0)))
target (LambdaFunction(monitorFunction.Function.Value))
enabled true
}
}
Event Pattern Matching
React to specific AWS events using event patterns.
stack "EventPatterns" {
let alertFunction =
lambda "AlertHandler" {
runtime Runtime.DOTNET_8
handler "App::HandleAlert"
code "./lambda"
}
// React to EC2 instance state changes
let ec2StateChangePattern =
let details = System.Collections.Generic.Dictionary<string, obj>()
details.Add("state", [| "terminated" |] :> obj)
EventPattern(
Source = [| "aws.ec2" |],
DetailType = [| "EC2 Instance State-change Notification" |],
Detail = details
)
eventBridgeRule "EC2Termination" {
description "Alert on EC2 instance termination"
eventPattern ec2StateChangePattern
target (LambdaFunction(alertFunction.Function.Value))
}
}
Custom Event Bus
Create a custom event bus for your application events. Note: Custom event buses are created using the CDK EventBus class directly, then referenced in rules.
Multi-Target Rules
Send events to multiple targets.
stack "MultiTarget" {
let logFunction =
lambda "Logger" {
runtime Runtime.DOTNET_8
handler "App::Log"
code "./lambda"
}
let notifyFunction =
lambda "Notifier" {
runtime Runtime.DOTNET_8
handler "App::Notify"
code "./lambda"
}
eventBridgeRule "CriticalEvents" {
description "Handle critical system events"
schedule (Schedule.Rate(Duration.Hours(1.0)))
target (LambdaFunction(logFunction.Function.Value))
target (LambdaFunction(notifyFunction.Function.Value))
}
}
Cron Expressions
Use cron expressions for complex scheduling.
stack "CronSchedule" {
let reportFunction =
lambda "WeeklyReport" {
runtime Runtime.DOTNET_8
handler "App::GenerateReport"
code "./lambda"
}
// Run every Monday at 9 AM UTC
eventBridgeRule "WeeklyReport" {
description "Generate weekly report"
schedule (Schedule.Cron(CronOptions(Minute = "0", Hour = "9", WeekDay = "MON")))
target (LambdaFunction(reportFunction.Function.Value))
}
}
Disabled Rules
Create rules that are initially disabled.
stack "DisabledRule" {
let maintenanceFunction =
lambda "Maintenance" {
runtime Runtime.DOTNET_8
handler "App::RunMaintenance"
code "./lambda"
}
eventBridgeRule "MaintenanceTask" {
description "Maintenance task (manually enabled)"
schedule (Schedule.Rate(Duration.Days(1.0)))
enabled false // Start disabled
target (LambdaFunction(maintenanceFunction.Function.Value))
}
}
Best Practices
Performance
- ✅ Use event patterns to filter events efficiently
- ✅ Batch events when possible
- ✅ Monitor rule metrics (Invocations, FailedInvocations)
Reliability
- ✅ Configure dead-letter queues for failed events
- ✅ Use retries with exponential backoff
- ✅ Set appropriate target timeout values
Security
- ✅ Use IAM policies to control who can put events
- ✅ Encrypt custom event buses at rest
- ✅ Use resource-based policies for cross-account access
Cost Optimization
- ✅ Use event filtering to reduce unnecessary invocations
- ✅ Consolidate rules where possible
- ✅ Monitor CloudWatch metrics for usage patterns
Operational Excellence
- ✅ Use descriptive rule names and descriptions
- ✅ Tag rules with project and environment
- ✅ Document event schemas
- ✅ Version your event patterns
namespace FsCDK
namespace Amazon
namespace Amazon.CDK
namespace Amazon.CDK.AWS
namespace Amazon.CDK.AWS.Events
namespace Amazon.CDK.AWS.Events.Targets
namespace Amazon.CDK.AWS.Lambda
val stack: name: string -> StackBuilder
<summary>Creates an AWS CDK Stack construct.</summary>
<param name="name">The name of the stack.</param>
<code lang="fsharp"> stack "MyStack" { lambda myFunction bucket myBucket } </code>
<summary>Creates an AWS CDK Stack construct.</summary>
<param name="name">The name of the stack.</param>
<code lang="fsharp"> stack "MyStack" { lambda myFunction bucket myBucket } </code>
val processFunction: FunctionSpec
val lambda: name: string -> FunctionBuilder
<summary>Creates a Lambda function configuration.</summary>
<param name="name">The function name.</param>
<code lang="fsharp"> lambda "MyFunction" { handler "index.handler" runtime Runtime.NODEJS_18_X code "./lambda" timeout 30.0 } </code>
<summary>Creates a Lambda function configuration.</summary>
<param name="name">The function name.</param>
<code lang="fsharp"> lambda "MyFunction" { handler "index.handler" runtime Runtime.NODEJS_18_X code "./lambda" timeout 30.0 } </code>
custom operation: runtime (Runtime)
Calls FunctionBuilder.Runtime
<summary>Sets the runtime for the Lambda function.</summary>
<param name="config">The function configuration.</param>
<param name="runtime">The Lambda runtime.</param>
<code lang="fsharp"> lambda "MyFunction" { runtime Runtime.NODEJS_18_X } </code>
<summary>Sets the runtime for the Lambda function.</summary>
<param name="config">The function configuration.</param>
<param name="runtime">The Lambda runtime.</param>
<code lang="fsharp"> lambda "MyFunction" { runtime Runtime.NODEJS_18_X } </code>
Multiple items
type Runtime = inherit DeputyBase new: name: string * ?family: Nullable<RuntimeFamily> * ?props: ILambdaRuntimeProps -> unit member RuntimeEquals: other: Runtime -> bool member ToString: unit -> string member BundlingImage: DockerImage member Family: Nullable<RuntimeFamily> member IsVariable: bool member Name: string member SupportsCodeGuruProfiling: bool member SupportsInlineCode: bool ...
--------------------
Runtime(name: string, ?family: System.Nullable<RuntimeFamily>, ?props: ILambdaRuntimeProps) : Runtime
type Runtime = inherit DeputyBase new: name: string * ?family: Nullable<RuntimeFamily> * ?props: ILambdaRuntimeProps -> unit member RuntimeEquals: other: Runtime -> bool member ToString: unit -> string member BundlingImage: DockerImage member Family: Nullable<RuntimeFamily> member IsVariable: bool member Name: string member SupportsCodeGuruProfiling: bool member SupportsInlineCode: bool ...
--------------------
Runtime(name: string, ?family: System.Nullable<RuntimeFamily>, ?props: ILambdaRuntimeProps) : Runtime
property Runtime.DOTNET_8: Runtime with get
custom operation: handler (string)
Calls FunctionBuilder.Handler
<summary>Sets the handler for the Lambda function.</summary>
<param name="config">The function configuration.</param>
<param name="handler">The handler name (e.g., "index.handler").</param>
<code lang="fsharp"> lambda "MyFunction" { handler "index.handler" } </code>
<summary>Sets the handler for the Lambda function.</summary>
<param name="config">The function configuration.</param>
<param name="handler">The handler name (e.g., "index.handler").</param>
<code lang="fsharp"> lambda "MyFunction" { handler "index.handler" } </code>
custom operation: code (Code)
Calls FunctionBuilder.Code
<summary>Sets the code source from a Code object.</summary>
<param name="config">The function configuration.</param>
<param name="path">The Code object.</param>
<code lang="fsharp"> lambda "MyFunction" { code (Code.FromBucket myBucket "lambda.zip") } </code>
<summary>Sets the code source from a Code object.</summary>
<param name="config">The function configuration.</param>
<param name="path">The Code object.</param>
<code lang="fsharp"> lambda "MyFunction" { code (Code.FromBucket myBucket "lambda.zip") } </code>
val eventBridgeRule: name: string -> EventBridgeRuleBuilder
<summary>Creates an EventBridge rule with AWS best practices.</summary>
<param name="name">The rule name.</param>
<code lang="fsharp"> eventBridgeRule "MyRule" { description "Process daily events" schedule (Schedule.Rate(Duration.Hours(24.0))) target (LambdaFunction(myFunction)) } </code>
<summary>Creates an EventBridge rule with AWS best practices.</summary>
<param name="name">The rule name.</param>
<code lang="fsharp"> eventBridgeRule "MyRule" { description "Process daily events" schedule (Schedule.Rate(Duration.Hours(24.0))) target (LambdaFunction(myFunction)) } </code>
custom operation: description (string)
Calls EventBridgeRuleBuilder.Description
<summary>Sets the rule description.</summary>
<summary>Sets the rule description.</summary>
custom operation: schedule (Schedule)
Calls EventBridgeRuleBuilder.Schedule
<summary>Sets the schedule for the rule.</summary>
<summary>Sets the schedule for the rule.</summary>
type Schedule =
inherit DeputyBase
static member Cron: options: ICronOptions -> Schedule
static member Expression: expression: string -> Schedule
static member Rate: duration: Duration -> Schedule
member ExpressionString: string
Schedule.Cron(options: ICronOptions) : Schedule
Multiple items
type CronOptions = interface ICronOptions new: unit -> unit member Day: string member Hour: string member Minute: string member Month: string member WeekDay: string member Year: string
--------------------
CronOptions() : CronOptions
type CronOptions = interface ICronOptions new: unit -> unit member Day: string member Hour: string member Minute: string member Month: string member WeekDay: string member Year: string
--------------------
CronOptions() : CronOptions
custom operation: target (IRuleTarget)
Calls EventBridgeRuleBuilder.Target
<summary>Adds a target to the rule.</summary>
<summary>Adds a target to the rule.</summary>
Multiple items
type LambdaFunction = inherit DeputyBase interface IRuleTarget new: handler: IFunction * ?props: ILambdaFunctionProps -> unit member Bind: rule: IRule * ?id: string -> IRuleTargetConfig
--------------------
LambdaFunction(handler: IFunction, ?props: ILambdaFunctionProps) : LambdaFunction
type LambdaFunction = inherit DeputyBase interface IRuleTarget new: handler: IFunction * ?props: ILambdaFunctionProps -> unit member Bind: rule: IRule * ?id: string -> IRuleTargetConfig
--------------------
LambdaFunction(handler: IFunction, ?props: ILambdaFunctionProps) : LambdaFunction
FunctionSpec.Function: IFunction option
property Option.Value: IFunction with get
val monitorFunction: FunctionSpec
Schedule.Rate(duration: Duration) : Schedule
type Duration =
inherit DeputyBase
member FormatTokenToNumber: unit -> string
member IsUnresolved: unit -> bool
member Minus: rhs: Duration -> Duration
member Plus: rhs: Duration -> Duration
member ToDays: ?opts: ITimeConversionOptions -> float
member ToHours: ?opts: ITimeConversionOptions -> float
member ToHumanString: unit -> string
member ToIsoString: unit -> string
member ToMilliseconds: ?opts: ITimeConversionOptions -> float
...
Duration.Minutes(amount: float) : Duration
custom operation: enabled (bool)
Calls EventBridgeRuleBuilder.Enabled
<summary>Enables or disables the rule.</summary>
<summary>Enables or disables the rule.</summary>
val alertFunction: FunctionSpec
val ec2StateChangePattern: EventPattern
val details: System.Collections.Generic.Dictionary<string,obj>
namespace System
namespace System.Collections
namespace System.Collections.Generic
Multiple items
type Dictionary<'TKey,'TValue> = interface ICollection<KeyValuePair<'TKey,'TValue>> interface IEnumerable<KeyValuePair<'TKey,'TValue>> interface IEnumerable interface IDictionary<'TKey,'TValue> interface IReadOnlyCollection<KeyValuePair<'TKey,'TValue>> interface IReadOnlyDictionary<'TKey,'TValue> interface ICollection interface IDictionary interface IDeserializationCallback interface ISerializable ...
<summary>Represents a collection of keys and values.</summary>
<typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
<typeparam name="TValue">The type of the values in the dictionary.</typeparam>
--------------------
System.Collections.Generic.Dictionary() : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(dictionary: System.Collections.Generic.IDictionary<'TKey,'TValue>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(capacity: int) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(dictionary: System.Collections.Generic.IDictionary<'TKey,'TValue>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(capacity: int, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
type Dictionary<'TKey,'TValue> = interface ICollection<KeyValuePair<'TKey,'TValue>> interface IEnumerable<KeyValuePair<'TKey,'TValue>> interface IEnumerable interface IDictionary<'TKey,'TValue> interface IReadOnlyCollection<KeyValuePair<'TKey,'TValue>> interface IReadOnlyDictionary<'TKey,'TValue> interface ICollection interface IDictionary interface IDeserializationCallback interface ISerializable ...
<summary>Represents a collection of keys and values.</summary>
<typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
<typeparam name="TValue">The type of the values in the dictionary.</typeparam>
--------------------
System.Collections.Generic.Dictionary() : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(dictionary: System.Collections.Generic.IDictionary<'TKey,'TValue>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(capacity: int) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(dictionary: System.Collections.Generic.IDictionary<'TKey,'TValue>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
System.Collections.Generic.Dictionary(capacity: int, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : System.Collections.Generic.Dictionary<'TKey,'TValue>
Multiple items
val string: value: 'T -> string
--------------------
type string = System.String
val string: value: 'T -> string
--------------------
type string = System.String
type obj = System.Object
System.Collections.Generic.Dictionary.Add(key: string, value: obj) : unit
Multiple items
type EventPattern = interface IEventPattern new: unit -> unit member Account: string array member Detail: IDictionary<string,obj> member DetailType: string array member Id: string array member Region: string array member Resources: string array member Source: string array member Time: string array ...
--------------------
EventPattern() : EventPattern
type EventPattern = interface IEventPattern new: unit -> unit member Account: string array member Detail: IDictionary<string,obj> member DetailType: string array member Id: string array member Region: string array member Resources: string array member Source: string array member Time: string array ...
--------------------
EventPattern() : EventPattern
custom operation: eventPattern (IEventPattern)
Calls EventBridgeRuleBuilder.EventPattern
<summary>Sets the event pattern for the rule.</summary>
<summary>Sets the event pattern for the rule.</summary>
val logFunction: FunctionSpec
val notifyFunction: FunctionSpec
Duration.Hours(amount: float) : Duration
val reportFunction: FunctionSpec
val maintenanceFunction: FunctionSpec
Duration.Days(amount: float) : Duration
FsCDK