如何在同一 CDK 堆栈中获取代码管道的 ARN

How to get the ARN of a codepipeline inside of the same CDK stack

我正在尝试将通知规则设置为 CDK 堆栈中代码管道的一部分。

请注意,这不是 CDK 管道,而是正在设置 AWS CodePipeline 的 CDK 堆栈。

为了创建 CfnNotificationRule,我必须传入 CodePipeline 的 ARN 作为 resource 参数。在下面的示例代码中,我将 ARN 硬编码为 TARGET_ARN

不过,我想动态提供这个。

如何将 CDK 为 my-pipeline 生成的 ARN 提供给 CfnNotificationRule 构造函数?

const codepipeline = require('@aws-cdk/aws-codepipeline');

class PipelineStack extends cdk.Stack {

    constructor(scope, id, props) {
        super(scope, id, props);

        //I want the ARN of this pipeline in TARGET_ARN
        new codepipeline.Pipeline(this, 'Pipeline', {
            crossAccountKeys: false,
            pipelineName: "my-pipeline",
            stages: [{
                    stageName: 'Source',
                },
                {
                    stageName: 'Build',
                },
                {
                    stageName: 'Deploy',
                    ]
                }
            ]
        })



        const AWS_SLACK_CHATBOT_ARN = 'arn:aws:chatbot::111111111111:chat-configuration/slack-channel/my-slack-channel'
        const TARGET_ARN = 'arn:aws:codepipeline:us-east-2:111111111111:my-pipeline'

        const notes = new notifications.CfnNotificationRule(this, 'my-dev-slack', {
            detailType: "FULL",
            name: "my-dev-slack",
            eventTypeIds: [
                "codepipeline-pipeline-action-execution-succeeded",
                "codepipeline-pipeline-action-execution-failed",
                "codepipeline-pipeline-stage-execution-failed"
            ],
            targets: [{
                targetType: "AWSChatbotSlack",
                targetAddress: AWS_SLACK_CHATBOT_ARN
            }],
            resource: TARGET_ARN

        })
    }
}

将管道初始化为局部变量,然后你可以使用它的内部方法,例如你的新代码看起来像这样(我注意到你在 stageName: 'Deploy', 下有一个括号 [导致代码注释编译,所以我在我的示例中删除了它)

        const myPipeline = new codepipeline.Pipeline(this, 'Pipeline', {
            crossAccountKeys: false,
            pipelineName: "my-pipeline",
            stages: [{
                stageName: 'Source',
            },
                {
                    stageName: 'Build',
                },
                {
                    stageName: 'Deploy',
                    
                }]
        })

        myPipeline.pipelineArn

myPipeline.pipelineArn 会给你 ARN