aws-cloudformationamazon-cloudwatchamazon-cloudwatch-metricstroposphere

Associating CloudWatch Alarm with MetricFilter using Troposphere


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?


Solution

  • 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.