{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Metadata": {
        "AWS::CloudFormation::Interface": {
            "ParameterGroups": [
                {
                    "Label": {
                        "default": "Customer Configuration"
                    },
                    "Parameters": [
                        "AlarmNameConfig",
                        "AlarmDescriptionConfig",
                        "ThresholdConfig",
                        "LoadBalancerID",
                        "TargetGroupID"
                    ]
                },
                {
                    "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": "HTTP Successful Response Rate from ALB Target: ApplicationName",
            "Type": "String",
            "MinLength": "1",
            "MaxLength": "255"
        },
        "AlarmDescriptionConfig": {
            "Description": "Please revise the alarm description to be relevant to your workload",
            "Default": "Monitor HTTP Sucessful Response Rate from Target Group. Built from AWS Incident Detection and Response CloudFormation template.",
            "Type": "String",
            "MinLength": "0",
            "MaxLength": "1024"
        },
        "LoadBalancerID": {
            "Description": "Please input the Load Balancer information: app/LoadBalancerName/ID",
            "Type": "String",
            "AllowedPattern": "^app/[a-zA-Z0-9-]*/[a-zA-Z0-9]*$"
        },
        "TargetGroupID": {
            "Description": "Please input the TargetGroup ID information: targetgroup/TargetGroupName/ID",
            "Type": "String",
            "AllowedPattern": "^targetgroup/[a-zA-Z0-9-]*/[a-zA-Z0-9]*$"
        },
        "ThresholdConfig": {
            "Description": "Alarm threshold in percentage, please adjust based on application monitoring requirement",
            "Default": "95",
            "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": {
        "HTTPTargetErrorRate": {
            "Type": "AWS::CloudWatch::Alarm",
            "Properties": {
                "AlarmName": {
                    "Ref": "AlarmNameConfig"
                },
                "AlarmDescription": {
                    "Ref": "AlarmDescriptionConfig"
                },
                "ActionsEnabled": true,
                "OKActions": [],
                "AlarmActions": [],
                "InsufficientDataActions": [],
                "EvaluationPeriods": {
                    "Ref": "EvaluationPeriodsConfig"
                },
                "DatapointsToAlarm": {
                    "Ref": "DatapointsToAlarmConfig"
                },
                "Threshold": {
                    "Ref": "ThresholdConfig"
                },
                "ComparisonOperator": "LessThanThreshold",
                "TreatMissingData": "missing",
                "Metrics": [
                    {
                        "Id": "e1",
                        "Label": "HTTP Success Rate From ALB-Target Group",
                        "ReturnData": true,
                        "Expression": "(m1+m2)/(m1+m2+m3+m4)*100"
                    },
                    {
                        "Id": "m1",
                        "ReturnData": false,
                        "MetricStat": {
                            "Metric": {
                                "Namespace": "AWS/ApplicationELB",
                                "MetricName": "HTTPCode_Target_2XX_Count",
                                "Dimensions": [
                                    {
                                        "Name": "TargetGroup",
                                        "Value": {
                                            "Ref": "TargetGroupID"
                                        }
                                    },
                                    {
                                        "Name": "LoadBalancer",
                                        "Value": {
                                            "Ref": "LoadBalancerID"
                                        }
                                    }
                                ]
                            },
                            "Period": {
                                "Ref": "PeriodConfig"
                            },
                            "Stat": "Sum"
                        }
                    },
                    {
                        "Id": "m2",
                        "ReturnData": false,
                        "MetricStat": {
                            "Metric": {
                                "Namespace": "AWS/ApplicationELB",
                                "MetricName": "HTTPCode_Target_3XX_Count",
                                "Dimensions": [
                                    {
                                        "Name": "TargetGroup",
                                        "Value": {
                                            "Ref": "TargetGroupID"
                                        }
                                    },
                                    {
                                        "Name": "LoadBalancer",
                                        "Value": {
                                            "Ref": "LoadBalancerID"
                                        }
                                    }
                                ]
                            },
                            "Period": {
                                "Ref": "PeriodConfig"
                            },
                            "Stat": "Sum"
                        }
                    },
                    {
                        "Id": "m3",
                        "ReturnData": false,
                        "MetricStat": {
                            "Metric": {
                                "Namespace": "AWS/ApplicationELB",
                                "MetricName": "HTTPCode_Target_4XX_Count",
                                "Dimensions": [
                                    {
                                        "Name": "TargetGroup",
                                        "Value": {
                                            "Ref": "TargetGroupID"
                                        }
                                    },
                                    {
                                        "Name": "LoadBalancer",
                                        "Value": {
                                            "Ref": "LoadBalancerID"
                                        }
                                    }
                                ]
                            },
                            "Period": {
                                "Ref": "PeriodConfig"
                            },
                            "Stat": "Sum"
                        }
                    },
                    {
                        "Id": "m4",
                        "ReturnData": false,
                        "MetricStat": {
                            "Metric": {
                                "Namespace": "AWS/ApplicationELB",
                                "MetricName": "HTTPCode_Target_5XX_Count",
                                "Dimensions": [
                                    {
                                        "Name": "TargetGroup",
                                        "Value": {
                                            "Ref": "TargetGroupID"
                                        }
                                    },
                                    {
                                        "Name": "LoadBalancer",
                                        "Value": {
                                            "Ref": "LoadBalancerID"
                                        }
                                    }
                                ]
                            },
                            "Period": {
                                "Ref": "PeriodConfig"
                            },
                            "Stat": "Sum"
                        }
                    }
                ]
            }
        }
    }
}