-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise an alarm when any stages are in a Failed state. (#6)
Configure a lambda function to watch the pipeline and emit a metric for the number of failed stages, enabling consistent alarming on a 'failed pipeline'.
- Loading branch information
1 parent
7dff479
commit ce744c0
Showing
7 changed files
with
412 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './watcher'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import AWS = require('aws-sdk'); | ||
|
||
// export for tests | ||
export const codePipeline = new AWS.CodePipeline(); | ||
export const logger = { | ||
log: (line: string) => process.stdout.write(line) | ||
}; | ||
|
||
/** | ||
* Lambda function for checking the stages of a CodePipeline and emitting log | ||
* entries with { failedCount = <no. of failed stages> } for async metric | ||
* aggregation via metric filters. | ||
* | ||
* It requires the pipeline's name be set as the 'PIPELINE_NAME' environment variable. | ||
*/ | ||
export async function handler() { | ||
const pipelineName = process.env.PIPELINE_NAME; | ||
if (!pipelineName) { | ||
throw new Error("Pipeline name expects environment variable: 'PIPELINE_NAME'"); | ||
} | ||
const state = await codePipeline.getPipelineState({ | ||
name: pipelineName | ||
}).promise(); | ||
|
||
let failedCount = 0; | ||
if (state.stageStates) { | ||
failedCount = state.stageStates | ||
.filter(stage => stage.latestExecution !== undefined && stage.latestExecution.status === 'Failed') | ||
.length; | ||
} | ||
logger.log(JSON.stringify({ | ||
failedCount | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import cloudwatch = require('@aws-cdk/aws-cloudwatch'); | ||
import cpipeline = require('@aws-cdk/aws-codepipeline'); | ||
import events = require('@aws-cdk/aws-events'); | ||
import iam = require('@aws-cdk/aws-iam'); | ||
import lambda = require('@aws-cdk/aws-lambda'); | ||
import logs = require('@aws-cdk/aws-logs'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
import fs = require('fs'); | ||
import path = require('path'); | ||
|
||
export interface PipelineWatcherProps { | ||
/** | ||
* Code Pipeline to monitor for failed stages | ||
*/ | ||
pipeline: cpipeline.Pipeline; | ||
|
||
/** | ||
* Set the pipelineName of the alarm description. | ||
* | ||
* Description is set to 'Pipeline <title> has failed stages' | ||
* | ||
* @default pipeline's name | ||
*/ | ||
title?: string; | ||
} | ||
|
||
/** | ||
* Construct which watches a Code Pipeline for failed stages and raises an alarm | ||
* if there are any failed stages. | ||
* | ||
* A function runs every minute and calls GetPipelineState for the provided pipeline's | ||
* name, counts the number of failed stages and emits a JSON log { failedCount: <number> }. | ||
* A metric filter is then configured to track this value as a CloudWatch metric, and | ||
* a corresponding alarm is set to fire when the maximim value of a single 5-minute interval | ||
* is >= 1. | ||
*/ | ||
export class PipelineWatcher extends cdk.Construct { | ||
public readonly alarm: cloudwatch.Alarm; | ||
|
||
constructor(parent: cdk.Construct, name: string, props: PipelineWatcherProps) { | ||
super(parent, name); | ||
|
||
const pipelineWatcher = new lambda.Function(this, 'Poller', { | ||
handler: 'index.handler', | ||
runtime: lambda.Runtime.NodeJS810, | ||
code: lambda.Code.inline(fs.readFileSync(path.join(__dirname, 'watcher-handler.js')).toString('utf8')), | ||
environment: { | ||
PIPELINE_NAME: props.pipeline.pipelineName | ||
} | ||
}); | ||
|
||
// See https://github.com/awslabs/aws-cdk/issues/1340 for exposing grants on the pipeline. | ||
pipelineWatcher.addToRolePolicy(new iam.PolicyStatement() | ||
.addResource(props.pipeline.pipelineArn) | ||
.addAction('codepipeline:GetPipelineState')); | ||
|
||
// ex: arn:aws:logs:us-east-1:123456789012:log-group:my-log-group | ||
const logGroup = new logs.LogGroup(this, 'Logs', { | ||
logGroupName: `/aws/lambda/${pipelineWatcher.functionName}`, | ||
retentionDays: 731 | ||
}); | ||
|
||
const trigger = new events.EventRule(this, 'Trigger', { | ||
scheduleExpression: 'rate(1 minute)', | ||
targets: [pipelineWatcher] | ||
}); | ||
|
||
const logGroupResource = logGroup.findChild('Resource') as cdk.Resource; | ||
const triggerResource = trigger.findChild('Resource') as cdk.Resource; | ||
triggerResource.addDependency(logGroupResource); | ||
|
||
const metricNamespace = `CDK/Delivlib`; | ||
const metricName = `${props.pipeline.pipelineName}_FailedStages`; | ||
|
||
new logs.MetricFilter(this, 'MetricFilter', { | ||
filterPattern: logs.FilterPattern.exists('$.failedCount'), | ||
metricNamespace, | ||
metricName, | ||
metricValue: '$.failedCount', | ||
logGroup | ||
}); | ||
|
||
this.alarm = new cloudwatch.Alarm(this, 'Alarm', { | ||
alarmDescription: `Pipeline ${props.title || props.pipeline.pipelineName} has failed stages`, | ||
metric: new cloudwatch.Metric({ | ||
metricName, | ||
namespace: metricNamespace, | ||
statistic: cloudwatch.Statistic.Maximum | ||
}), | ||
threshold: 1, | ||
comparisonOperator: cloudwatch.ComparisonOperator.GreaterThanOrEqualToThreshold, | ||
evaluationPeriods: 1, | ||
treatMissingData: cloudwatch.TreatMissingData.Ignore, // We expect a steady stream of data points | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.