amazon-web-servicesaws-cdk

CDK Unable to Add CodeStarNotification to CodePipeline


I use CDK to deploy a codepipeline. It works fine until I try to add notification for codepipeline success/fail events. It gives CREATE_FAILED error with message Resource handler returned message: "Invalid request provided: AWS::CodeStarNotifications::NotificationRule" (RequestToken: bb566fd0-1ac9-5d61-03fe-f9c27b4196fa, HandlerErrorCode: InvalidRequest). What could be the reason? Thanks.

import * as codepipeline from "@aws-cdk/aws-codepipeline";
import * as codepipeline_actions from "@aws-cdk/aws-codepipeline-actions";
import * as codestar_noti from "@aws-cdk/aws-codestarnotifications";
import * as sns from "@aws-cdk/aws-sns";

    const pipeline = new codepipeline.Pipeline(...);
    const topicArn = props.sns_arn_for_developer;
    const targetTopic = sns.Topic.fromTopicArn(
      this,
      "sns-notification-topic",
      topicArn
    );
    new codestar_noti.NotificationRule(this, "Notification", {
      detailType: codestar_noti.DetailType.BASIC,
      events: [
        "codepipeline-pipeline-pipeline-execution-started",
        "codepipeline-pipeline-pipeline-execution-failed",
        "codepipeline-pipeline-pipeline-execution-succeeded",
        "codepipeline-pipeline-pipeline-execution-canceled",
      ],
      source: pipeline,
      targets: [targetTopic],
    });

Here is the snippet of generated cloudformation tempalte.

    "Notification2267453E": {
      "Type": "AWS::CodeStarNotifications::NotificationRule",
      "Properties": {
        "DetailType": "BASIC",
        "EventTypeIds": [
          "codepipeline-pipeline-pipeline-execution-started",
          "codepipeline-pipeline-pipeline-execution-failed",
          "codepipeline-pipeline-pipeline-execution-succeeded",
          "codepipeline-pipeline-pipeline-execution-canceled"
        ],
        "Name": "sagemakerbringyourownNotification36194CEC",
        "Resource": {
          "Fn::Join": [
            "",
            [
              "arn:",
              {
                "Ref": "AWS::Partition"
              },
              ":codepipeline:ap-southeast-1:305326993135:",
              {
                "Ref": "sagemakerbringyourownpipeline0A8C43B1"
              }
            ]
          ]
        },
        "Targets": [
          {
            "TargetAddress": "arn:aws:sns:ap-southeast-1:305326993135:whitespace_alerts",
            "TargetType": "SNS"
          }
        ]
      },
      "Metadata": {
        "aws:cdk:path": "sagemaker-bring-your-own/Notification/Resource"
      }
    },

Solution

  • This is because imported resources cannot be modified. As you pointed out in the comments, setting up the notification involves modifying the Topic resource, specifically its access policy.

    Reference: https://docs.aws.amazon.com/cdk/v2/guide/resources.html#resources_importing