constructor()

in lib/chime-notifier/chime-notifier.ts [46:84]


  constructor(scope: Construct, id: string, props: ChimeNotifierProps) {
    super(scope, id);

    const message = props.message ?? '/md @All Pipeline **$PIPELINE** failed in action **$ACTION**. Latest change:\n```\n$REVISION\n```\n([Failure details]($URL))';

    if (props.webhookUrls.length > 0) {
      // Reuse the same Lambda code for all pipelines, we will move the Lambda parameterizations into
      // the CloudWatch Event Input.
      const notifierLambda = new lambda.SingletonFunction(this, 'Default', {
        handler: 'index.handler',
        uuid: '0f4a3ee0-692e-4249-932f-a46a833886d8',
        code: lambda.Code.fromAsset(path.join(__dirname, 'handler')),
        runtime: lambda.Runtime.NODEJS_12_X,
        timeout: Duration.minutes(5),
      });

      notifierLambda.addToRolePolicy(new iam.PolicyStatement({
        actions: ['codepipeline:GetPipelineExecution', 'codepipeline:ListActionExecutions'],
        resources: [props.pipeline.pipelineArn],
      }));

      props.pipeline.onStateChange(`${this.node.path}-ChimeNotifier`, {
        target: new events_targets.LambdaFunction(notifierLambda, {
          event: events.RuleTargetInput.fromObject({
            // Add parameters
            message,
            webhookUrls: props.webhookUrls,
            // Copy over "detail" field
            detail: events.EventField.fromPath('$.detail'),
          }),
        }),
        eventPattern: {
          detail: {
            state: ['FAILED'],
          },
        },
      });
    }
  }