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

2024-06-01 05:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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?
            )
        

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

我认为这应该是一个基于 https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-codestarnotifications.CfnNotificationRule.TargetProperty.html 但是Python似乎不存在这种类型


Tags: thecoreselfawsidpipelineinitnotification
1条回答
网友
1楼 · 发布于 2024-06-01 05:20:53

好的,最后我们弄明白了,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),
                     ]
            )

相关问题 更多 >