Skip to main content

Event targets for Amazon EventBridge

Project description

Event Targets for Amazon EventBridge

---

cdk-constructs: Stable


This library contains integration classes to send Amazon EventBridge to any number of supported AWS Services. Instances of these classes should be passed to the rule.addTarget() method.

Currently supported are:

  • Start a CodeBuild build
  • Start a CodePipeline pipeline
  • Run an ECS task
  • Invoke a Lambda function
  • Publish a message to an SNS topic
  • Send a message to an SQS queue
  • Start a StepFunctions state machine
  • Queue a Batch job
  • Make an AWS API call
  • Put a record to a Kinesis stream
  • Log an event into a LogGroup
  • Put a record to a Kinesis Data Firehose stream
  • Put an event on an EventBridge bus

See the README of the @aws-cdk/aws-events library for more information on EventBridge.

Invoke a Lambda function

Use the LambdaFunction target to invoke a lambda function.

The code snippet below creates an event rule with a Lambda function as a target triggered for every events from aws.ec2 source. You can optionally attach a dead letter queue.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_lambda as lambda_
import aws_cdk.aws_events as events
import aws_cdk.aws_sqs as sqs
import aws_cdk.aws_events_targets as targets

fn = lambda_.Function(self, "MyFunc",
    runtime=lambda_.Runtime.NODEJS_12_X,
    handler="index.handler",
    code=lambda_.Code.from_inline(f"exports.handler = {handler.toString()}")
)

rule = events.Rule(self, "rule",
    event_pattern=EventPattern(
        source=["aws.ec2"]
    )
)

queue = sqs.Queue(self, "Queue")

rule.add_target(targets.LambdaFunction(fn,
    dead_letter_queue=queue
))

Log an event into a LogGroup

Use the LogGroup target to log your events in a CloudWatch LogGroup.

For example, the following code snippet creates an event rule with a CloudWatch LogGroup as a target. Every events sent from the aws.ec2 source will be sent to the CloudWatch LogGroup.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_logs as logs
import aws_cdk.aws_events as events
import aws_cdk.aws_events_targets as targets

log_group = logs.LogGroup(self, "MyLogGroup",
    log_group_name="MyLogGroup"
)

rule = events.Rule(self, "rule",
    event_pattern=EventPattern(
        source=["aws.ec2"]
    )
)

rule.add_target(targets.CloudWatchLogGroup(log_group))

Trigger a CodeBuild project

Use the CodeBuildProject target to trigger a CodeBuild project.

The code snippet below creates a CodeCommit repository that triggers a CodeBuild project on commit to the master branch. You can optionally attach a dead letter queue.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_sdk.aws_codebuild as codebuild
import aws_sdk.aws_codecommit as codecommit
import aws_sdk.aws_sqs as sqs
import aws_cdk.aws_events_targets as targets

repo = codecommit.Repository(self, "MyRepo",
    repository_name="aws-cdk-codebuild-events"
)

project = codebuild.Project(self, "MyProject",
    source=codebuild.Source.code_commit(repository=repo)
)

dead_letter_queue = sqs.Queue(self, "DeadLetterQueue")

# trigger a build when a commit is pushed to the repo
on_commit_rule = repo.on_commit("OnCommit",
    target=targets.CodeBuildProject(project,
        dead_letter_queue=dead_letter_queue
    ),
    branches=["master"]
)

Trigger a State Machine

Use the SfnStateMachine target to trigger a State Machine.

The code snippet below creates a Simple StateMachine that is triggered every minute with a dummy object as input. You can optionally attach a dead letter queue to the target.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_sdk.aws_iam as iam
import aws_sdk.aws_sqs as sqs
import aws_cdk.aws_stepfunctions as sfn
import aws_cdk.aws_events_targets as targets

rule = events.Rule(stack, "Rule",
    schedule=events.Schedule.rate(cdk.Duration.minutes(1))
)

dlq = sqs.Queue(stack, "DeadLetterQueue")

role = iam.Role(stack, "Role",
    assumed_by=iam.ServicePrincipal("events.amazonaws.com")
)
state_machine = sfn.StateMachine(stack, "SM",
    definition=sfn.Wait(stack, "Hello", time=sfn.WaitTime.duration(cdk.Duration.seconds(10))),
    role=role
)

rule.add_target(targets.SfnStateMachine(state_machine,
    input=events.RuleTargetInput.from_object(SomeParam="SomeValue"),
    dead_letter_queue=dlq
))

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.aws-events-targets-1.94.0.tar.gz (82.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file aws-cdk.aws-events-targets-1.94.0.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-events-targets-1.94.0.tar.gz
  • Upload date:
  • Size: 82.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-events-targets-1.94.0.tar.gz
Algorithm Hash digest
SHA256 b04b3120a325051435b32f4d43c023d6342e7f2979bab861786e26151e1a2af1
MD5 f2a4f705fee39fa41a29ca852ed56900
BLAKE2b-256 16c121d1fae9cd73325a9bf0c3107f42bf5ac18e725436e30fd0d2c661a9fbe0

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_events_targets-1.94.0-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_events_targets-1.94.0-py3-none-any.whl
  • Upload date:
  • Size: 85.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_events_targets-1.94.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0050d645f04ea93b126a211902b259939b338253d578344560088f066e7695e2
MD5 b16705fadd0ee03d33d4e88c36a8e68a
BLAKE2b-256 34203a11cebb5d91c4d10578c6c96552d8a8bdd31829bb4fe6f76ede1421d9f8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page