I kind of stuck with configuring cdk a common use case: to have an Alarm and Metric for Lambda Function
import lambda = require("@aws-cdk/aws-lambda")
import { Duration } from "@aws-cdk/core"
import lambda = require("@aws-cdk/aws-lambda")
import { Duration } from "@aws-cdk/core";
import { LogGroup } from '@aws-cdk/aws-logs'
import { MetricFilter, FilterPattern } from '@aws-cdk/aws-logs'
import { Alarm, ComparisonOperator, TreatMissingData } from "@aws-cdk/aws-cloudwatch"
const lambdaFn = new lambda.Function(this, "VouchersPayoutQueueConsumer", {
functionName: 'lambda-custom-name',
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.fromAsset("dir_name"),
timeout: Duration.seconds(15),
memorySize: 1024,
handler: "handler.handlerMethod",
})
const METRIC_NAME = 'metric-name'
const METRIC_NAMESPACE = 'metric-namespace'
new MetricFilter(this, `MetricFilterId`, {
metricName: METRIC_NAME,
metricNamespace: METRIC_NAMESPACE,
logGroup: LogGroup.fromLogGroupName(this, "LambdaLogGroupId", `/aws/lambda/${lambdaFn.functionName}`),
filterPattern: FilterPattern.stringValue("$.params.name", "==", "ReportErrorsToS3"),
metricValue: "$.params.value"
})
new Alarm(this, "AlarmId", {
metric,
evaluationPeriods: 1,
actionsEnabled: true,
alarmName: `alarms-s3errors`,
alarmDescription: "Alarm ReportErrorsToS3",
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
treatMissingData: TreatMissingData.IGNORE,
period: Duration.hours(1),
threshold: 1,
statistic: "max"
})
1/4 | 11:02:05 AM | CREATE_FAILED | AWS::Logs::MetricFilter | MetricFilter (MetricFilter8E0FEF2B) The specified log group does not exist. (Service: AWSLogs; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: a1395551-5768-40c3-baaa-cbb133effe7b)
AWS CDK AWS 1.22.0
In order to create Alarms in CDK we have to first define a metric and then create our cloudwatch alarm. We only created metrics by using the metric* methods exposed on the Function construct, i.e.: Copied! const functionErrors = myFunction.
Which Metrics Does Lambda Track by Default? The Lambda service comes with seven metrics for your functions out of the box. Invocations, duration, error count, throttles, async delivery failures, iterator age, and concurrent executions.
Result:
import { Metric } from "@aws-cdk/aws-cloudwatch";
import lambda = require("@aws-cdk/aws-lambda")
import { Duration } from "@aws-cdk/core";
import { LogGroup } from '@aws-cdk/aws-logs'
import { MetricFilter, FilterPattern } from '@aws-cdk/aws-logs'
import { Alarm, ComparisonOperator, TreatMissingData } from "@aws-cdk/aws-cloudwatch"
const LAMBDA_CUSTOM_NAME = `lambda-custom-name`
let logGroup = new LogGroup(this, 'LogGroup', {
logGroupName: `/aws/lambda/${LAMBDA_CUSTOM_NAME}`
})
const lambdaFn = new lambda.Function(this, "VouchersPayoutQueueConsumer", {
functionName: LAMBDA_CUSTOM_NAME,
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.fromAsset("dir_name"),
timeout: Duration.seconds(15),
memorySize: 1024,
handler: "handler.handlerMethod",
});
const METRIC_NAME = 'metric-name'
const METRIC_NAMESPACE = 'metric-namespace'
const metric = new Metric({
namespace: METRIC_NAMESPACE,
metricName: METRIC_NAME
})
new MetricFilter(this, `MetricFilterId`, {
metricName: METRIC_NAME,
metricNamespace: METRIC_NAMESPACE,
logGroup: logGroup,
filterPattern: FilterPattern.stringValue("$.params.name", "==", "ReportErrorsToS3"),
metricValue: "$.params.value"
})
new Alarm(this, "AlarmId", {
metric,
evaluationPeriods: 1,
actionsEnabled: true,
alarmName: `alarms-s3errors`,
alarmDescription: "Alarm ReportErrorsToS3",
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
treatMissingData: TreatMissingData.IGNORE,
period: Duration.hours(1),
threshold: 1,
statistic: "max"
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With