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

问题描述

我正在尝试将通知规则设置为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,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