0
votes

Context

I have created a AWS Logs SubscriptionFilter using CDK. I am now trying to create a metric/alarm for some of the metrics for this resource.

Problem

  • All the metrics I am interested in (see ForwardedLogEvents, DeliveryErrors, DeliveryThrottling in the Monitoring AWS Logs with CloudWatch Metrics docs) requires these dimensions to be specified:
    • LogGroupName
    • DestinationType
    • FilterName

The first two are easy to specify since the LogGroupName is also required while creating the construct and DestinationType in my case is just Lambda. However, I see no way to get FilterName using CDK.

  • Using CloudWatch, I see that the FilterName is like MyStackName-MyLogicalID29669D87-GCMA0Q4KKALH. So I can't directly specify it using a Fn.ref (since I don't know the logical id). Using CloudFormation, I could have directly done Ref: LogicalId.
  • I also don't see any properties on the SubscriptionFilter object that will return this (unlike most other CDK constructs this one seems pretty bare and returns absolutely no information about the resource).
  • There are also no metric* methods on SubscriptionFilter object (unlike other standard constructs like Lambda functions, S3 buckets etc.), so I have to manually specify the Metric object. See for example: CDK metric objects docs.
  • The CDK construct (and the underlying CloudFormation resource: AWS::Logs::SubscriptionFilter) does not let me specify the FilterName - so I can't use a variable to specify it also and the name is dynamically generated.

Example code that is very close to what I need:

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')
  }
})

Question

  1. How can I figure out how to acquire the FilterName property to construct the Metric object?
  2. Or otherwise, is there another way to go about this?
1
I have requested a feature from CDK: github.com/aws/aws-cdk/issues/8141 - arnab

1 Answers

0
votes

I was able to work around this by using Stack#getLogicalId method.

Example code

In Kotlin, as an extension function for any Construct):

fun Construct.getLogicalId() = Stack.of(this).getLogicalId(this.node.defaultChild as CfnElement)

... and then use it with any 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()