我已经使用CDK创建了AWS Logs SubscriptionFilter。我现在正在尝试为此资源的一些指标创建指标/警报。
ForwardedLogEvents
,DeliveryErrors
,DeliveryThrottling
)都需要指定以下维度:
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
对象?答案 0 :(得分:0)
我可以使用Stack#getLogicalId方法来解决此问题。
在科特林,以extension function的Construct开头):
fun Construct.getLogicalId() = Stack.of(this).getLogicalId(this.node.defaultChild as CfnElement)
...,然后将其与任何Construct一起使用:
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()