【问题标题】:How to get the ARN of a codepipeline inside of the same CDK stack如何在同一 CDK 堆栈中获取代码管道的 ARN
【发布时间】:2020-11-11 06:56:36
【问题描述】:

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

        })
    }
}

【问题讨论】:

    标签: amazon-cloudformation amazon-sns aws-codepipeline aws-cdk


    【解决方案1】:

    将管道初始化为局部变量,然后您可以在之后使用其内部方法,例如,您的新代码如下所示(我注意到您在 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

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 2021-11-27
      • 2021-09-06
      • 1970-01-01
      • 2022-11-04
      • 2021-08-21
      • 2021-10-18
      • 2012-12-24
      • 2019-12-22
      相关资源
      最近更新 更多