Events Notify AWS Construct for AWS CDK
cdk-events-notify is an AWS CDK Construct Library (currently v2.2.767) that provides a mechanism to trigger Lambda functions for push notifications to Line Notify or Slack. It primarily focuses on detecting AWS Console Login or switch role events via CloudTrail. The library is actively maintained with frequent updates, as indicated by its versioning and recent activity on PyPI and GitHub.
Common errors
-
ModuleNotFoundError: No module named 'cdk_events_notify'
cause The `cdk-events-notify` package is not installed in your Python environment or the virtual environment activated for your CDK project.fixRun `pip install cdk-events-notify` in your project's Python virtual environment. If using a `requirements.txt`, ensure it's listed and run `pip install -r requirements.txt`. -
ValueError: LINE_NOTIFY_TOKEN environment variable is not set.
cause The provided quickstart code explicitly checks for the `LINE_NOTIFY_TOKEN` environment variable and raises an error if it's missing.fixSet the `LINE_NOTIFY_TOKEN` environment variable before running `cdk deploy` or `cdk synth`. For example: `export LINE_NOTIFY_TOKEN='your-token-here'`. -
Error: 'MyStack' depends on 'AnotherStack' (Depends on Lambda resources., Depends on resources.). Adding this dependency (AnotherStack -> MyStack/MyConstruct/Resource.Arn) would create a cyclic reference.
cause While not directly related to `cdk-events-notify` specifically, this is a common AWS CDK error when creating event notification patterns (like those implicitly handled by `cdk-events-notify` or explicitly with `aws-events`) between different stacks in a way that creates a circular dependency in CloudFormation.fixRefactor your CDK application to avoid circular dependencies. This might involve combining resources into a single stack, using existing resources via `from_lookup` methods, or leveraging custom resources for advanced scenarios where native CDK constructs create circular references.
Warnings
- breaking This library is designed for AWS CDK v2. While previous versions might have supported CDK v1, AWS CDK v1 has reached End-of-Support. Using `cdk-events-notify` with CDK v1 is not recommended and may lead to compatibility issues or missing features.
- gotcha AWS EventBridge, which `cdk-events-notify` relies on for event detection, is a regional service. If a console sign-in or role switch event occurs in a different AWS region than where your `cdk-events-notify` stack is deployed, the event will not be captured and notifications will not be sent.
- gotcha A `LINE_NOTIFY_TOKEN` or Slack webhook URL is required for notifications to function. The quickstart example demonstrates passing this as an environment variable, which is a common pattern for sensitive information.
Install
-
pip install cdk-events-notify
Imports
- EventNotify
import cdk_events_notify
from cdk_events_notify import EventNotify
- Stack
from aws_cdk.core import Stack
from aws_cdk import Stack, Environment
Quickstart
import os
import aws_cdk as cdk
from constructs import Construct
from cdk_events_notify import EventNotify
class MyEventNotifyStack(cdk.Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Retrieve Line Notify Token from environment variable for security
line_notify_token = os.environ.get('LINE_NOTIFY_TOKEN', '')
if not line_notify_token:
raise ValueError("LINE_NOTIFY_TOKEN environment variable is not set.")
# Create an EventNotify construct for Line notifications
EventNotify(self, 'LineEventNotify',
line_notify_token=line_notify_token)
app = cdk.App()
MyEventNotifyStack(app, 'MyEventNotifyStack',
env=cdk.Environment(account=os.environ.get('CDK_DEFAULT_ACCOUNT'),
region=os.environ.get('CDK_DEFAULT_REGION'))
)
app.synth()