# OnEventOptions

Standard set of options for `onXxx` event handlers on construct.

Extends: [EventCommonOptions](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_events.EventCommonOptions.html)
Used as argument in: [IAgent.onEvent](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.IAgent.html#onevent), [IAgentAlias.onCloudTrailEvent](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.IAgentAlias.html#oncloudtrailevent), IDatabaseInstance.onEvent, IImagePipeline.onCVEDetected, IImagePipeline.onEvent, IImagePipeline.onImageBuildCompleted, IImagePipeline.onImageBuildFailed, IImagePipeline.onImageBuildStateChange, IImagePipeline.onImageBuildSucceeded, IImagePipeline.onImagePipelineAutoDisabled, IImagePipeline.onWaitForAction, IJob.onEvent, IJob.onFailure, IJob.onSuccess, IJob.onTimeout, IPipeline.onEvent, IPipeline.onStateChange, IProject.onBuildFailed, IProject.onBuildStarted, IProject.onBuildSucceeded, IProject.onEvent, IProject.onPhaseChange, IProject.onStateChange, IRepository.onCommentOnCommit (aws_codecommit), IRepository.onCommentOnPullRequest (aws_codecommit), IRepository.onEvent (aws_codecommit), IRepository.onPullRequestStateChange (aws_codecommit), IRepository.onReferenceCreated (aws_codecommit), IRepository.onReferenceDeleted (aws_codecommit), IRepository.onReferenceUpdated (aws_codecommit), IRepository.onStateChange (aws_codecommit), IRepository.onCloudTrailEvent (aws_ecr), IRepository.onEvent (aws_ecr), IRule.onComplianceChange, IRule.onEvent, IRule.onReEvaluationStatus, Trail.onEvent

- Source: [`aws-events/lib/on-event-options.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-events/lib/on-event-options.ts#L50)
- Type last changed: v2.38.0 · 17 Aug 2022

## Example

TypeScript:

```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { LambdaFunction } from 'aws-cdk-lib/aws-events-targets';

const repo = new ecr.Repository(this, 'Repo');
const lambdaHandler = new lambda.Function(this, 'LambdaFunction', {
  runtime: lambda.Runtime.PYTHON_3_12,
  code: lambda.Code.fromInline('# dummy func'),
  handler: 'index.handler',
});

repo.onEvent('OnEventTargetLambda', {
  target: new LambdaFunction(lambdaHandler),
});
```

Python:

```python
import aws_cdk.aws_lambda as lambda_
from aws_cdk.aws_events_targets import LambdaFunction

repo = ecr.Repository(self, "Repo")
lambda_handler = lambda_.Function(self, "LambdaFunction",
    runtime=lambda_.Runtime.PYTHON_3_12,
    code=lambda_.Code.from_inline("# dummy func"),
    handler="index.handler"
)

repo.on_event("OnEventTargetLambda",
    target=LambdaFunction(lambda_handler)
)
```

Java:

```java
import software.amazon.awscdk.services.lambda.*;
import software.amazon.awscdk.services.events.targets.LambdaFunction;

Repository repo = new Repository(this, "Repo");
Function lambdaHandler = Function.Builder.create(this, "LambdaFunction")
        .runtime(Runtime.PYTHON_3_12)
        .code(Code.fromInline("# dummy func"))
        .handler("index.handler")
        .build();

repo.onEvent("OnEventTargetLambda", OnEventOptions.builder()
        .target(new LambdaFunction(lambdaHandler))
        .build());
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.Lambda;
using Amazon.CDK.AWS.Events.Targets;

var repo = new Repository(this, "Repo");
var lambdaHandler = new Function(this, "LambdaFunction", new FunctionProps {
    Runtime = Runtime.PYTHON_3_12,
    Code = Code.FromInline("# dummy func"),
    Handler = "index.handler"
});

repo.OnEvent("OnEventTargetLambda", new OnEventOptions {
    Target = new LambdaFunction(lambdaHandler)
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

repo := ecr.NewRepository(this, jsii.String("Repo"))
lambdaHandler := lambda.NewFunction(this, jsii.String("LambdaFunction"), &FunctionProps{
	Runtime: lambda.Runtime_PYTHON_3_12(),
	Code: lambda.Code_FromInline(jsii.String("# dummy func")),
	Handler: jsii.String("index.handler"),
})

repo.OnEvent(jsii.String("OnEventTargetLambda"), &OnEventOptions{
	Target: awscdk.NewLambdaFunction(lambdaHandler),
})
```

## Properties

- `crossStackScope`: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) — The scope to use if the source of the rule and its target are in different Stacks (but in the same account & region).

  **Default:** none (the main scope will be used, even for cross-stack Events)

  This helps dealing with cycles that often arise in these situations.

- `description`: string — A description of the rule's purpose.

  **Default:** No description

- `eventPattern`: [EventPattern](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_events.EventPattern.html) — Additional restrictions for the event to route to the specified target.

  **Default:** No additional filtering based on an event pattern.

  The method that generates the rule probably imposes some type of event
  filtering. The filtering implied by what you pass here is added
  on top of that filtering.

- `ruleName`: string — A name for the rule.

  **Default:** AWS CloudFormation generates a unique physical ID.

- `target`: [IRuleTarget](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_events.IRuleTarget.html) — The target to register for the event.

  **Default:** No target is added to the rule. Use `addTarget()` to add a target.
