{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Metadata": {
        "AWS::CloudFormation::Interface": {
            "ParameterGroups": [
                {
                    "Label": {
                        "default": "Customer Configuration"
                    },
                    "Parameters": [
                        "AlarmNameConfig",
                        "AlarmDescriptionConfig",
                        "ThresholdConfig",
                        "DistributionIdConfig"
                    ]
                },
                {
                    "Label": {
                        "default": "Recommended Parameters by AWS Incident Detection and Response"
                    },
                    "Parameters": [
                        "PeriodConfig",
                        "EvaluationPeriodsConfig",
                        "DatapointsToAlarmConfig"
                    ]
                }
            ]
        }
    },
    "Parameters": {
        "AlarmNameConfig": {
            "Description": "Please revise the alarm name to be relevant to your workload",
            "Default": "TotalErrorRate from CloudFront: ApplicationName",
            "Type": "String",
            "MinLength": "1",
            "MaxLength": "255"
        },
        "AlarmDescriptionConfig": {
            "Description": "Please revise the alarm description to be relevant to your workload",
            "Default": "Monitor CloudFront Distribution TotalErrorRate from Target Group. Built from AWS Incident Detection and Response CloudFormation template.",
            "Type": "String",
            "MinLength": "0",
            "MaxLength": "1024"
        },
        "DistributionIdConfig": {
            "Description": "Please input the CloudFront Distribution ID:",
            "Type": "String",
            "AllowedPattern": "^[a-zA-Z0-9]*$"
        },
        "ThresholdConfig": {
            "Description": "Alarm triggers when TotalErrorRate > threshold, please adjust based on application monitoring requirement",
            "Default": "5",
            "Type": "Number",
            "MinValue": "0",
            "MaxValue": "100"
        },
        "EvaluationPeriodsConfig": {
            "Description": "Evaluation periods, recommend to use default value",
            "Default": "3",
            "Type": "Number",
            "MinValue": "1"
        },
        "DatapointsToAlarmConfig": {
            "Description": "Datapoints to alarm, recommend to use default value",
            "Default": "3",
            "Type": "Number",
            "MinValue": "1"
        },
        "PeriodConfig": {
            "Description": "Period of alarm in seconds(10, 30, 60, and any multiple of 60), recommend to use default value",
            "Default": "60",
            "Type": "Number",
            "MinValue": "10"
        }
    },
    "Resources": {
        "TotalErrorRate": {
            "Type": "AWS::CloudWatch::Alarm",
            "Properties": {
                "AlarmName": {
                    "Ref": "AlarmNameConfig"
                },
                "AlarmDescription": {
                    "Ref": "AlarmDescriptionConfig"
                },
                "ActionsEnabled": true,
                "OKActions": [],
                "AlarmActions": [],
                "InsufficientDataActions": [],
                "MetricName": "TotalErrorRate",
                "Namespace": "AWS/CloudFront",
                "Statistic": "Average",
                "Dimensions": [
                    {
                        "Name": "DistributionId",
                        "Value": {
                            "Ref": "DistributionIdConfig"
                        }
                    },
                    {
                        "Name": "Region",
                        "Value": "Global"
                    }
                ],
                "Period": {
                    "Ref": "PeriodConfig"
                },
                "EvaluationPeriods": {
                    "Ref": "EvaluationPeriodsConfig"
                },
                "DatapointsToAlarm": {
                    "Ref": "DatapointsToAlarmConfig"
                },
                "Threshold": {
                    "Ref": "ThresholdConfig"
                },
                "ComparisonOperator": "GreaterThanThreshold",
                "TreatMissingData": "notBreaching"
            }
        }
    }
}