如何通过 aws-cdk 将自定义指标和警报添加到 AWS Lambda 函数
How to add custom Metric and Alarm to AWS Lambda function via aws-cdk
我有点坚持配置 cdk 一个常见的用例:有一个 Alarm 和 Metric 对于 Lambda 函数
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"
})
- 从哪里获取 metric 以及如何使用 MetricFilter 映射它?
- 如何为 MetricFilter 正确设置 logGroup 以解决问题:
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
- metric 是单独声明的,但是 metricName 和 namespace 必须相同 用于 MetricFilter
- LogGroup 也必须单独实例化,但与 logGroupName 相同,lambda 将创建。
结果:
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"
})
以上述答案为基础(我没有足够的评论要点)- 如果您想遵循此模式,请确保指定 const 指标的类型 'metric'。它应该看起来像:
new Alarm(this, "AlarmId", {
metric: 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"
})
我有点坚持配置 cdk 一个常见的用例:有一个 Alarm 和 Metric 对于 Lambda 函数
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"
})
- 从哪里获取 metric 以及如何使用 MetricFilter 映射它?
- 如何为 MetricFilter 正确设置 logGroup 以解决问题:
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
- metric 是单独声明的,但是 metricName 和 namespace 必须相同 用于 MetricFilter
- LogGroup 也必须单独实例化,但与 logGroupName 相同,lambda 将创建。
结果:
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"
})
以上述答案为基础(我没有足够的评论要点)- 如果您想遵循此模式,请确保指定 const 指标的类型 'metric'。它应该看起来像:
new Alarm(this, "AlarmId", {
metric: 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"
})