Skip to main content

AWS CloudWatch Events Construct Library

Project description

Amazon CloudWatch Events Construct Library

---

Stability: Stable


Amazon CloudWatch Events delivers a near real-time stream of system events that describe changes in AWS resources. For example, an AWS CodePipeline emits the State Change event when the pipeline changes it's state.

  • Events: An event indicates a change in your AWS environment. AWS resources can generate events when their state changes. For example, Amazon EC2 generates an event when the state of an EC2 instance changes from pending to running, and Amazon EC2 Auto Scaling generates events when it launches or terminates instances. AWS CloudTrail publishes events when you make API calls. You can generate custom application-level events and publish them to CloudWatch Events. You can also set up scheduled events that are generated on a periodic basis. For a list of services that generate events, and sample events from each service, see CloudWatch Events Event Examples From Each Supported Service.
  • Targets: A target processes events. Targets can include Amazon EC2 instances, AWS Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, Amazon SNS topics, Amazon SQS queues, and built-in targets. A target receives events in JSON format.
  • Rules: A rule matches incoming events and routes them to targets for processing. A single rule can route to multiple targets, all of which are processed in parallel. Rules are not processed in a particular order. This enables different parts of an organization to look for and process the events that are of interest to them. A rule can customize the JSON sent to the target, by passing only certain parts or by overwriting it with a constant.
  • EventBuses: An event bus can receive events from your own custom applications or it can receive events from applications and services created by AWS SaaS partners. See Creating an Event Bus.

Rule

The Rule construct defines a CloudWatch events rule which monitors an event based on an event pattern and invoke event targets when the pattern is matched against a triggered event. Event targets are objects that implement the IRuleTarget interface.

Normally, you will use one of the source.onXxx(name[, target[, options]]) -> Rule methods on the event source to define an event rule associated with the specific activity. You can targets either via props, or add targets using rule.addTarget.

For example, to define an rule that triggers a CodeBuild project build when a commit is pushed to the "master" branch of a CodeCommit repository:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
on_commit_rule = repo.on_commit("OnCommit",
    target=targets.CodeBuildProject(project),
    branches=["master"]
)

You can add additional targets, with optional input transformer using eventRule.addTarget(target[, input]). For example, we can add a SNS topic target which formats a human-readable message for the commit.

For example, this adds an SNS topic as a target:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
on_commit_rule.add_target(targets.SnsTopic(topic,
    message=events.RuleTargetInput.from_text(f"A commit was pushed to the repository {codecommit.ReferenceEvent.repositoryName} on branch {codecommit.ReferenceEvent.referenceName}")
))

Scheduling

You can configure a Rule to run on a schedule (cron or rate).

The following example runs a task every day at 4am:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from aws_cdk.aws_events import Rule, Schedule
from aws_cdk.aws_events_targets import EcsTask

ecs_task_target = EcsTask(cluster=cluster, task_definition=task_definition)

Rule(self, "ScheduleRule",
    schedule=Schedule.cron(minute="0", hour="4"),
    targets=[ecs_task_target]
)

More details in ScheduledEvents documentation page.

Event Targets

The @aws-cdk/aws-events-targets module includes classes that implement the IRuleTarget interface for various AWS services.

The following targets are supported:

  • targets.CodeBuildProject: Start an AWS CodeBuild build
  • targets.CodePipeline: Start an AWS CodePipeline pipeline execution
  • targets.EcsTask: Start a task on an Amazon ECS cluster
  • targets.LambdaFunction: Invoke an AWS Lambda function
  • targets.SnsTopic: Publish into an SNS topic
  • targets.SqsQueue: Send a message to an Amazon SQS Queue
  • targets.SfnStateMachine: Trigger an AWS Step Functions state machine
  • targets.AwsApi: Make an AWS API call

Cross-account targets

It's possible to have the source of the event and a target in separate AWS accounts:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from aws_cdk.core import App, Stack
import aws_cdk.aws_codebuild as codebuild
import aws_cdk.aws_codecommit as codecommit
import aws_cdk.aws_events_targets as targets

app = App()

stack1 = Stack(app, "Stack1", env=Environment(account=account1, region="us-east-1"))
repo = codecommit.Repository(stack1, "Repository")

stack2 = Stack(app, "Stack2", env=Environment(account=account2, region="us-east-1"))
project = codebuild.Project(stack2, "Project")

repo.on_commit("OnCommit",
    target=targets.CodeBuildProject(project)
)

In this situation, the CDK will wire the 2 accounts together:

  • It will generate a rule in the source stack with the event bus of the target account as the target
  • It will generate a rule in the target stack, with the provided target
  • It will generate a separate stack that gives the source account permissions to publish events to the event bus of the target account in the given region, and make sure its deployed before the source stack

Note: while events can span multiple accounts, they cannot span different regions (that is a CloudWatch, not CDK, limitation).

For more information, see the AWS documentation on cross-account events.

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-1.24.0.tar.gz (136.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_cdk.aws_events-1.24.0-py3-none-any.whl (134.0 kB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.aws-events-1.24.0.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-events-1.24.0.tar.gz
  • Upload date:
  • Size: 136.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-events-1.24.0.tar.gz
Algorithm Hash digest
SHA256 81ca4d2cf869d62e79f995c29e28dfc453ebc9d7eb2f758908f32d372c5d4bb3
MD5 2a5238ce2f87cfeb41dd49be040f868f
BLAKE2b-256 9edfca911e164f0510d89dc157de3c71f4596521fdf281cb688f25ff19e83ba5

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_events-1.24.0-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_events-1.24.0-py3-none-any.whl
  • Upload date:
  • Size: 134.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_events-1.24.0-py3-none-any.whl
Algorithm Hash digest
SHA256 77611cbf79f0645e99e2fc727f8ece02be510797dba4ab5f8cbf27822b27c43b
MD5 9080bde12b1f4e306ac549b540e209af
BLAKE2b-256 3352a6c1b4a4c0c70b8394cc6d5dc5de29c9138fd4da1b436cf31e27e7dc32ee

See more details on using hashes here.

Supported by

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