Antarr Byrd
Antarr Byrd

Reputation: 26061

Adding Custom Metric to CloudFormation template

I need to add an alarm the triggers a rollback in CloudFormation. I've settled on adding a custom metric that can be assigned manually. I'm looking at the documentation here. This looks like it would work but I need it to be added to the template. Is this possible? The way I'm thinking initially the value would be 0 failures like below

aws cloudwatch put-metric-data --metric-name Failures --namespace MyNamespace --value 0

when I want to trigger a rollback I would set it to 1

aws cloudwatch put-metric-data --metric-name Failures --namespace MyNamespace --value 1

But I need this to be in the template so that I can have access to the correct/dynamic value of the namespace when defining the alarm.

Upvotes: 0

Views: 5260

Answers (2)

Tarek El-Mallah
Tarek El-Mallah

Reputation: 4115

You can use cloudformation simple template to add metrics

resource "AWS::Logs::MetricFilter" specifies a metric filter that describes how CloudWatch Logs extracts information from logs and transforms it into Amazon CloudWatch metrics. If you have multiple metric filters that are associated with a log group, all the filters are applied to the log streams in that group.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html

sample

404MetricFilter: 
  Type: AWS::Logs::MetricFilter
  Properties: 
    LogGroupName: 
      Ref: "myLogGroup"
    FilterPattern: "[ip, identity, user_id, timestamp, request, status_code = 404, size]"
    MetricTransformations: 
      - 
        MetricValue: "1"
        MetricNamespace: "WebServer/404s"
        MetricName: "404Count"

Upvotes: 4

samtoddler
samtoddler

Reputation: 9605

unless you want to do this via a cloudformation custom resource

you can use this as well Running bash commands in AWS CloudFormation templates

With the above you can run your command like

Resources:
  CommandRunner:
    Type: AWSUtility::CloudFormation::CommandRunner
    Properties:
      Command: 'aws cloudwatch put-metric-data --metric-name Failures --namespace MyNamespace --value 1'

Upvotes: 1

Related Questions