title: AWS Create Cloudformation log alert for Lambda
body: <p>I want to create an alert if something goes wrong with Lambda function especially when lambda throws an exception. I am planning to configure SNS topic to send a message if that alert is triggered. </p>
  
  <p>All lambdas are created using CloudFormation scripts, so I am searching for a CloudFormation template to configure alarms on CloudWatch logs. I was not able to find a good/working sample. Sample code below .</p>
  
  <pre><code>{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Description" : "AWS CloudTrail API Activity Alarm Template for CloudWatch Logs",
    "Parameters" : {
        "LogGroupName" : {
            "Type" : "String",
            "Default" : "CloudTrail/DefaultLogGroup",
            "Description" : "Enter CloudWatch Logs log group name. Default is CloudTrail/DefaultLogGroup"
        },
        "Email" : {
            "Type" : "String",
            "Description" : "Email address to notify when an API activity has triggered an alarm"
        }
    },
    "Resources" : {
      "SecurityGroupChangesAlarm": {
        "Type": "AWS::CloudWatch::Alarm",
        "Properties": {
            "AlarmName" : "CloudTrailSecurityGroupChanges",
            "AlarmDescription" : "Alarms when an API call is made to create, update or delete a Security Group.",
            "AlarmActions" : [{ "Ref" : "AlarmNotificationTopic" }],
            "MetricName" : "SecurityGroupEventCount",
            "Namespace" : "CloudTrailMetrics",
            "ComparisonOperator" : "GreaterThanOrEqualToThreshold",
            "EvaluationPeriods" : "1",
            "Period" : "300",
            "Statistic" : "Sum",
            "Threshold" : "1"
        }
      },
  
      "AlarmNotificationTopic": {
        "Type": "AWS::SNS::Topic",
        "Properties": {
            "Subscription": [
                {
                    "Endpoint": { "Ref": "Email" },
                    "Protocol": "email"
                }
            ]
        }
      }
    }
  }
  </code></pre>