1
votes

I'm using a Troposphere script to provision a CloudWatch metric filter and an alarm. In CloudWatch, it's possible to manually create an alarm that goes off based data that is aggregated from logs by the metric filter, but I would like to link the filter and alarm within the Troposphere script to save the manual labor if possible.

Here's what I have for the script (NOTE: there are some other resources defined and referenced below that are omitted for brevity):

t.add_resource(logs.MetricFilter(
    "PlanReconciliationPlansStepMetricFilter",
    FilterPattern="INFO generatePlanReconciliationStepKnownToMorningstarInPlans",
    LogGroupName=Ref("logGroupName"),
    MetricTransformations=[logs.MetricTransformation(
        "planReconciliationPlansStepMetricTransformation",
        MetricNamespace=Ref("metricNamespace"),
        MetricName=Join("", [Ref("springProfile"), "-", "plan-reconciliation-plans-step"]),
        MetricValue="1")]
))

alarmPlans = t.add_resource(
    Alarm(
        "PlanReconciliationPlansAlarm",
        AlarmDescription="Alarm if plan reconciliation metric filter is exceeded",
        Namespace="AWS/Logs",
        MetricName=Join("", [Ref("springProfile"), "-", "plan-reconciliation-plans-step"]),
        Statistic="Sum",
        Period="60",
        EvaluationPeriods="1",
        Threshold="0",
        ComparisonOperator="GreaterThanThreshold",
        AlarmActions=[Ref(alarmTopic), ]   
    )
)

This produces a well-defined CloudFormation template, however, when I execute the change set and observe the created metric filter, I see that the alarm I wanted to link to the filter is not automatically set and I would need to manually create it:

enter image description here

My thinking was that if the MetricTransformation and Alarm shared the same MetricName property, hopefully the alarm would already be linked to the metric filter, but it appears that this is not the case. Looking at the documentation for metric filters, it appears that there are only three properties to work with (LogGroupName, FilterPattern, and list of MetricTransformations). Is there no way to link the alarm with the metric filter in Troposphere?

1
is Ref("metricNamespace") same as "AWS/Logs"`?Marcin
It's a string parameter set to "BATCH-ERRORS". I'm going to set the alarm namespace to "BATCH-ERRORS" to see if this helps my cause...Adam Freymiller
How did it go with the change?Marcin
Looks like that did the trick. If you want, you can post that as the answer and I'll approve it so you get the pointsAdam Freymiller
Thanks. I will do that.Marcin

1 Answers

0
votes

Based on the comments.

The issue was that Ref("metricNamespace") was different then "AWS/Logs"`. Subsequently, the MetricFilter and the Alarm were using different namespaces.

Setting the namespaces to the same value of BATCH-ERRORS fixed the problem.