AWS CDK 创建 aws_codestarnotifications.CfnNotificationRule 并设置目标

AWS CDK Creating an aws_codestarnotifications.CfnNotificationRule and setting the targets

我正在尝试使用 CDK 创建一个简单的堆栈,其中代码流水线触发 lambda。

我在尝试设置 CfnNotificationRule 的目标时遇到了困难:

THE_PIPELINE_ARN = "arn:aws:codepipeline:eu-west-2:121212121212:the-pipeline"

class ExampleStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
    
        notification_topic = aws_sns.Topic(self, "TheNotificationTopic")

        notification_rule = aws_codestarnotifications.CfnNotificationRule(
            self,
            "StackStatusChangeNotificationRule",
            detail_type="FULL",
            event_type_ids=[
                "codepipeline-pipeline-action-execution-succeeded",
                "codepipeline-pipeline-action-execution-failed",
            ],
            name="TheStackCodeStarNotificationsNotificationRule",
            resource=THE_PIPELINE_ARN,
            targets= # what goes here?
            )
        

我希望通知转到 notification_topic 定义的 SNS 主题。

我认为这应该是 aws_cdk.aws_codestarnotifications.TargetProperty 基于 https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-codestarnotifications.CfnNotificationRule.TargetProperty.html 但是 Python.

的类型似乎不存在

好吧,终于明白了,TargetProperty 是 CfnNotificationRule 的嵌套 class,而不是模块中的 class(与文档相反)。所以正确的代码看起来像:

THE_PIPELINE_ARN = "arn:aws:codepipeline:eu-west-2:121212121212:the-pipeline"

class ExampleStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
    
        notification_topic = aws_sns.Topic(self, "TheNotificationTopic")

        notification_rule = aws_codestarnotifications.CfnNotificationRule(
            self,
            "StackStatusChangeNotificationRule",
            detail_type="FULL",
            event_type_ids=[
                "codepipeline-pipeline-action-execution-succeeded",
                "codepipeline-pipeline-action-execution-failed",
            ],
            name="TheStackCodeStarNotificationsNotificationRule",
            resource=THE_PIPELINE_ARN,
            targets= [aws_codestarnotifications.CfnNotificationRule.TargetProperty(
                      target_type="SNS",
                      target_address=notification_topic.topic_arn),
                     ]
            )