Stelligent

Get Notified on AWS CodePipeline Errors

With AWS CodePipeline Notifications, developers and others can receive notifications on events that occur in your pipelines?—?including pipeline, stage, and action changes. For example, you can receive email notifications on pipeline failures.

Because CodePipeline notifications leverage Amazon CloudWatch Events, you can also integrate it with AWS resources such as SNS, Lambda, SQS, SSM, EC2, Inspector, Kinesis Stream, ECS, Step Functions, and many more. One simple example is that you can use a Lambda function as a CloudWatch Event Rule Target and every time it gets notified, it runs a Lambda function and this Lambda function triggers a Slack notification. With this configuration, you can get CodePipeline notifications while you’re in Slack. In this post, I will focus on getting notified via Amazon SNS.

Because CodePipeline notifications leverage Amazon CloudWatch Events, you can integrate it with AWS resources such as SNS, Lambda, SQS, SSM, EC2, Inspector, Kinesis Streams, ECS, Step Functions, and many more.

At the conclusion of this post, you can provision all of the AWS resources by clicking a “Launch Stack” button and going through the AWS CloudFormation steps to launch the solution stack. As part of this configuration, it will automatically provision a CloudWatch Event Rule for CodePipeline notifications via SNS using an email endpoint. The example builds on an existing deployment pipeline for EC2 solution. You will also get access to a solution that only establishes the necessary CloudWatch and SNS resources rather than embedding with another solution.

The provisioning of all of the AWS resources is defined in a CloudFormation template. You can find the source examples in GitHub.

Figure 1 shows the failure of a CodeBuild test action in CodePipeline when a failure occurs.

Figure 1?—?CodePipeline Failure when one of the actions fail

In Figure 2, you can see the type of email you will receive when your CodePipeline pipeline fails. In the example, developers can click on the link to directly access the pipeline that just failed.

Figure 2?—?Email Notification when a CodePipeline Failure Occurs

The remainder of this post describes how to configure the solution in your AWS account.

Prerequisites

Here are the prerequisites for this solution:

To see these steps in more detail, go to the Prerequisites.

Architecture and Implementation

The components of this solution are:

CloudFormation Template

In this section, I’ll highlight a few of the most relevant code snippets from the CloudFormation template that automates the provisioning of the AWS resources in this solution.

SNS Topic

The CloudFormation snippet below creates an SNS Topic. Amazon Simple Notification (SNS) is a fully-managed service for sending messages to subscribing endpoints. In this case, I’m creating a subscription with an email endpoint. As part of launching the CloudFormation stack, the user is prompted to enter an email address for receiving these notifications.

  MySNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      Subscription:
      - Endpoint:
          Ref: EmailAddress
        Protocol: email

CloudWatch Event Rule

The CloudWatch Event Rule defines the conditions under which a CloudWatch Event is triggered. In the snippet below, it triggers the SNS Topic as a target when the CodePipeline source is in a FAILED state. In addition, it provides the text that’s displayed in the SNS Topic so that the user knows which action to take.

EventRule:
    Type: "AWS::Events::Rule"
    Properties:
      Description: "EventRule"
      EventPattern:
        source:
        - aws.codepipeline
        detail-type:
        - CodePipeline Pipeline Execution State Change
        detail:
          state:
          - FAILED
      State: "ENABLED"
      Targets:
        -
          Arn:
            Ref: "MySNSTopic"
          Id: "OpsTopic"
          InputTransformer:
            InputTemplate: '"The Pipeline  has failed. Go to https://console.aws.amazon.com/codepipeline/home?region=us-east-1#/view/" '
            InputPathsMap:
              pipeline: "$.detail.pipeline"

The combination of this event rule and the SNS Topic causes an email to be sent to an email address – provided by the user – when CodePipeline fails (as shown in Figure 2).

Costs

Since costs can vary as you use certain AWS services and other tools, you can see a cost breakdown and some sample scenarios to give you an idea of what your monthly spend might look like. Note this will be dependent on your unique environment and deployment, and the AWS Cost Calculator can assist in establishing cost projections.

The bottom line on pricing for this particular example is that you will charged no more than a few pennies if you launch the solution run through a few changes and then terminate the CloudFormation stack and associated AWS resources.

Deployment Steps

This particular solution defines the CloudWatch Event Rule in the context of a full solution that deploys an application onto EC2 using the AWS Developer Tools. The relevant part of this solution is here.

There are three main steps in launching this solution: preparing an AWS account, launching the stack, and testing the deployment. Each is described in more detail in this section. Please note that you are responsible for any charges incurred while creating and launching your solution.

Step 1. Prepare an AWS Account

If you don’t already have an AWS account, create one at http://aws.amazon.com by following the on-screen instructions. Part of the sign-up process involves receiving a phone call and entering a PIN using the phone keypad. Be sure you’ve signed up for the CloudFormation service. Use the region selector in the navigation bar of the console to choose the Northern Virginia (us-east-1) region

Step 2. Launch the Stack

Click on the “Launch Stack” button below to launch the CloudFormation stack. Before you launch the stack, review the architecture, configuration, and other considerations discussed in this post. To download the template, click here.

Time to deploy: Approximately 7 minutes

The template includes default settings that you can customize by following the instructions in this post.

Step 3. Test the Deployment

Here are the steps to test the deployment:

Alternative Solution: Separate Stack

Alternatively, you can launch a stack that only provisions the CloudWatch Event Rules and SNS Target by clicking the “Launch Stack” button below. You might want to enable  Termination Protection in the Advanced section of the Options page when launching the stack as part of the CloudFormation console. This makes it less simple to delete the stack if you want notifications to enabled for all CodePipeline invocations.

Time to deploy: Approximately 1 minute

Troubleshooting

For some AWS accounts, you might receive need to edit the text of the CloudWatch Event. If this happens to you, perform the following steps:

Additional Resources

Here are some of the supporting resources discussed in this post:

Did you find this post interesting? Are you passionate about working with the latest AWS technologies? If so, Stelligent is hiring and we would love to hear from you!

Stelligent Amazon Pollycast