我已经使用CDK创建了AWS Logs SubscriptionFilter。我现在正在尝试为此资源的一些指标创建指标/警报。
ForwardedLogEvents
文档中的DeliveryErrors
,DeliveryThrottling
,Monitoring AWS Logs with CloudWatch Metrics)要求指定以下尺寸:LogGroupName
DestinationType
FilterName
前两个很容易指定,因为在创建构造时也需要LogGroupName
,在我的情况下,DestinationType
只是Lambda
。但是,我看不到使用CDK来获取FilterName
的方法。
FilterName
类似于MyStackName-MyLogicalID29669D87-GCMA0Q4KKALH
。因此,我无法使用Fn.ref
直接指定它(因为我不知道逻辑ID)。使用CloudFormation,我可以直接完成Ref: LogicalId
。metric*
对象上也没有SubscriptionFilter
方法(不同于其他标准构造,如Lambda函数,S3存储桶等),因此我必须手动指定Metric
对象。参见例如:CDK metric objects docs。FilterName
-因此,我也不能使用变量来指定它,并且名称是动态生成的。非常接近我需要的示例代码:
const metric = new Metric({
namespace: 'AWS/Logs',
metricName: 'ForwardedLogEvents',
dimensions: {
DestinationType: 'Lambda',
// I know this value since I specified it while creating the SubscriptionFilter
LogGroupName: 'MyLogGroupName',
FilterName: Fn.ref('logical-id-wont-work-since-it-is-dynamic-in-CDK')
}
})
FilterName
属性以构造Metric
对象?我能够使用Stack#getLogicalId方法解决此问题。
[在Kotlin中,作为任何extension function的Construct):
fun Construct.getLogicalId() = Stack.of(this).getLogicalId(this.node.defaultChild as CfnElement)
...,然后将其与任何构造函数一起使用:
val metric = Metric.Builder.create()
.namespace("AWS/Logs")
.metricName("ForwardedLogEvents")
.dimensions(mapOf(
"DestinationType" to "Lambda",
"LogGroupName" to myLogGroup.logGroupName,
"FilterName" to mySubscriptionFilter.getLogicalId()
))
.statistic("sum")
.build()