Monitoring CodePipeline deployments.

Monitoring CodePipeline deployments.

This is the third article of a series of articles about monitoring.

-> First article: Monitoring CloudWatch key metrics using Slack and manage them using Terraform

-> Second article: APM using Datadog

-> Fourth article: Monitoring external services status using RSS and Slack

AWS CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines. CodePipeline automates the build, test, and deploy phases of your release process every time there is a code change, based on the release model you define.

In this article, we are going to focus on how to monitor CodePipeline deployments and push status reports to Slack. This way, we will have visibility when a deployment starts/succeeds/fails.

If you're looking for an article describing CI/CD and how to setup AWS CodePipeline, check this article written by OBytes' member Bouchaara Adil: Enable CI/CD with AWS developer tools

Alerting to Slack

We are going to use a CloudWatch Event Rule to capture every AWS CodePipeline state change for the Pipeline we choose. Besides this, we will need a Lambda function and an SNS topic to send messages to Slack. This is the same schema we used on the first article, changing the CloudWatch Alarm for a CloudWatch event rule.

CloudWatch Event Rule

Every change on a Pipeline emits a new CloudWatch Event, so, we will create a CloudWatch Event Rule to capture those events and notify the SNS topic that will invoke our Lambda function.

The CloudWatch Event Rule needs an Event Pattern which has the following format. As you see, you could capture the states you desire, for example notifying only for failed builds. In this case, we are going to notify when a build starts, fails or succeeds.

{
  "detail": {
    "state": [
      "STARTED",
      "FAILED",
      "SUCCEEDED"
    ]
  },
  "detail-type": [
    "CodePipeline Pipeline Execution State Change"
  ],
  "resources": [
    "arn:aws:codepipeline:<REGION>:<ACCOUNT_ID>:<PIPELINE_NAME>"
  ],
  "source": [
    "aws.codepipeline"
  ]
}

Then, you need to configure a target for the CloudWatch Event Rule. We will use an SNS topic. As input for the SNS topic, we will use the Pipeline name and the state, so we can create a custom message.

  input_transformer {
    input_paths {
      pipeline = "$.detail.pipeline"
      state    = "$.detail.state"
    }

    input_template = "\"The pipeline *<pipeline>* has *<state>*.\""
  }

SNS and Lambda function

We will use the same SNS topic and Lambda function than in the first article. You can find them in the source code of this article, along with the Terraform code for the CloudWatch Event Rule.

You’ll also need to create a Slack webhook and set it as an ENV variable for the Lambda function.

Slack

Once all the parts are ready, you'll notifications like the following in the Slack channel you configured.

Thanks for reading!

In this article, we created notifications for your CI/CD to Slack. This will help you gain visibility over builds and be aware of the status of your deployments. In the final article of this series, we will set up notifications for external services (AWS, GitHub, DockerHub…) used on our stack. Stay tuned!

Jose López
Jose López
2019-12-12 | 3 min read
Share article

More articles