【问题标题】:AWS CDK Creating an aws_codestarnotifications.CfnNotificationRule and setting the targetsAWS CDK 创建 aws_codestarnotifications.CfnNotificationRule 并设置目标
【发布时间】:2020-07-24 14:18:09
【问题描述】:

我正在尝试使用 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 似乎不存在该类型。

【问题讨论】:

    标签: python aws-cdk


    【解决方案1】:

    Ok 终于弄明白了,TargetProperty 是 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= [aws_codestarnotifications.CfnNotificationRule.TargetProperty(
                          target_type="SNS",
                          target_address=notification_topic.topic_arn),
                         ]
                )
    

    【讨论】:

    • 感谢这个答案,我能够使用聊天机器人并使用 Slack 集成!谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2022-06-30
    • 2020-12-05
    • 2020-09-17
    相关资源
    最近更新 更多