{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Observability Solution from CloudWatch: NGINX on EC2. Version: 1.0.0.",
  "Transform": "AWS::LanguageExtensions",
  "Parameters": {
    "DashboardNameParameter": {
      "Type": "String",
      "Description": "Enter name of the dashboard, the maximum length is 255, and valid characters are A-Z, a-z, 0-9, \"-\", and \"_\". The dashboard will display metrics from the region where the stack is being created. To easily differentiate this dashboard from similar ones in other regions, we recommend including the region name in the dashboard name.",
      "MinLength": "1",
      "MaxLength": "255",
      "AllowedPattern": "^[a-zA-Z0-9-_]*$",
      "Default": "NginxDashboard"
    }
  },
  "Resources": {
    "CloudWatchDashboard": {
      "Type": "AWS::CloudWatch::Dashboard",
      "Properties": {
        "DashboardName": {
          "Ref": "DashboardNameParameter"
        },
        "DashboardBody": {
          "Fn::ToJsonString": {
            "widgets": [
              {
                "height": 1,
                "width": 24,
                "y": 0,
                "x": 0,
                "type": "text",
                "properties": {
                  "markdown": "## NGINX Connections",
                  "background": "transparent"
                }
              },
              {
                "height": 6,
                "width": 8,
                "y": 1,
                "x": 0,
                "type": "metric",
                "properties": {
                  "metrics": [
                    [
                      {
                        "expression": "SORT(REMOVE_EMPTY(SEARCH('{CWAgent,InstanceId} MetricName=nginx_connections_active', 'Average')), MAX, DESC, 10)",
                        "id": "e1",
                        "period": 60,
                        "label": "Active [avg: ${AVG}, max: ${MAX}] - ${PROP('Dim.InstanceId')}"
                      }
                    ]
                  ],
                  "yAxis": {
                    "left": {
                      "label": "Connections",
                      "showUnits": false,
                      "min": 0
                    }
                  },
                  "view": "timeSeries",
                  "stacked": false,
                  "region": {
                    "Ref": "AWS::Region"
                  },
                  "stat": "Average",
                  "period": 60,
                  "title": "Top 10 Connections Active"
                }
              },
              {
                "height": 6,
                "width": 8,
                "y": 1,
                "x": 8,
                "type": "metric",
                "properties": {
                  "metrics": [
                    [
                      {
                        "expression": "SORT(REMOVE_EMPTY(SEARCH('{CWAgent,InstanceId} MetricName=nginx_connections_reading', 'Average')), MAX, DESC, 10)",
                        "id": "e1",
                        "period": 60,
                        "label": "Reading [avg: ${AVG}, max: ${MAX}] - ${PROP('Dim.InstanceId')}"
                      }
                    ]
                  ],
                  "yAxis": {
                    "left": {
                      "label": "Connections",
                      "showUnits": false,
                      "min": 0
                    }
                  },
                  "view": "timeSeries",
                  "stacked": false,
                  "region": {
                    "Ref": "AWS::Region"
                  },
                  "stat": "Average",
                  "period": 60,
                  "title": "Top 10 Connections Reading"
                }
              },
              {
                "height": 6,
                "width": 8,
                "y": 1,
                "x": 16,
                "type": "metric",
                "properties": {
                  "metrics": [
                    [
                      {
                        "expression": "SORT(REMOVE_EMPTY(SEARCH('{CWAgent,InstanceId} MetricName=nginx_connections_writing', 'Average')), MAX, DESC, 10)",
                        "id": "e1",
                        "period": 60,
                        "label": "Writing [avg: ${AVG}, max: ${MAX}] - ${PROP('Dim.InstanceId')}"
                      }
                    ]
                  ],
                  "yAxis": {
                    "left": {
                      "label": "Connections",
                      "showUnits": false,
                      "min": 0
                    }
                  },
                  "view": "timeSeries",
                  "stacked": false,
                  "region": {
                    "Ref": "AWS::Region"
                  },
                  "stat": "Average",
                  "period": 60,
                  "title": "Top 10 Connections Writing"
                }
              },
              {
                "height": 6,
                "width": 8,
                "y": 7,
                "x": 0,
                "type": "metric",
                "properties": {
                  "metrics": [
                    [
                      {
                        "expression": "SORT(REMOVE_EMPTY(SEARCH('{CWAgent,InstanceId} MetricName=nginx_connections_waiting', 'Average')), MAX, DESC, 10)",
                        "id": "e1",
                        "period": 60,
                        "label": "Waiting [avg: ${AVG}, max: ${MAX}] - ${PROP('Dim.InstanceId')}"
                      }
                    ]
                  ],
                  "yAxis": {
                    "left": {
                      "label": "Connections",
                      "showUnits": false,
                      "min": 0
                    }
                  },
                  "view": "timeSeries",
                  "stacked": false,
                  "region": {
                    "Ref": "AWS::Region"
                  },
                  "stat": "Average",
                  "period": 60,
                  "title": "Top 10 Connections Waiting"
                }
              },
              {
                "height": 6,
                "width": 8,
                "y": 7,
                "x": 8,
                "type": "metric",
                "properties": {
                  "metrics": [
                    [
                      {
                        "expression": "SORT(REMOVE_EMPTY(SEARCH('{CWAgent,InstanceId} MetricName=nginx_connections_accepted', 'Sum')), MAX, DESC, 10)",
                        "id": "e1",
                        "period": 60,
                        "label": "Accepted [avg: ${AVG}, max: ${MAX}] - ${PROP('Dim.InstanceId')}"
                      }
                    ]
                  ],
                  "yAxis": {
                    "left": {
                      "label": "Connections",
                      "showUnits": false,
                      "min": 0
                    }
                  },
                  "view": "timeSeries",
                  "stacked": false,
                  "region": {
                    "Ref": "AWS::Region"
                  },
                  "stat": "Average",
                  "period": 60,
                  "title": "Top 10 Connections Accepted"
                }
              },
              {
                "height": 6,
                "width": 8,
                "y": 7,
                "x": 16,
                "type": "metric",
                "properties": {
                  "metrics": [
                    [
                      {
                        "expression": "SORT(REMOVE_EMPTY(SEARCH('{CWAgent,InstanceId} MetricName=nginx_connections_handled', 'Sum')), MAX, DESC, 10)",
                        "id": "e1",
                        "period": 60,
                        "label": "Handled [avg: ${AVG}, max: ${MAX}] - ${PROP('Dim.InstanceId')}"
                      }
                    ]
                  ],
                  "yAxis": {
                    "left": {
                      "label": "Connections",
                      "showUnits": false,
                      "min": 0
                    }
                  },
                  "view": "timeSeries",
                  "stacked": false,
                  "region": {
                    "Ref": "AWS::Region"
                  },
                  "stat": "Average",
                  "period": 60,
                  "title": "Top 10 Connections Handled"
                }
              },
              {
                "height": 1,
                "width": 24,
                "y": 13,
                "x": 0,
                "type": "text",
                "properties": {
                  "markdown": "## NGINX Requests",
                  "background": "transparent"
                }
              },
              {
                "height": 6,
                "width": 8,
                "y": 14,
                "x": 0,
                "type": "metric",
                "properties": {
                  "metrics": [
                    [
                      {
                        "expression": "SORT(REMOVE_EMPTY(SEARCH('{CWAgent,InstanceId} MetricName=nginx_http_requests_total', 'Sum')), MAX, DESC, 10)",
                        "id": "e1",
                        "period": 60,
                        "label": "Requests [avg: ${AVG}, max: ${MAX}] - ${PROP('Dim.InstanceId')}"
                      }
                    ]
                  ],
                  "yAxis": {
                    "left": {
                      "label": "Requests",
                      "showUnits": false,
                      "min": 0
                    }
                  },
                  "view": "timeSeries",
                  "stacked": false,
                  "region": {
                    "Ref": "AWS::Region"
                  },
                  "stat": "Average",
                  "period": 60,
                  "title": "Top 10 Total Requests"
                }
              },
              {
                "height": 1,
                "width": 24,
                "y": 20,
                "x": 0,
                "type": "text",
                "properties": {
                  "markdown": "## NGINX Metric Exporter",
                  "background": "transparent"
                }
              },
              {
                "height": 6,
                "width": 8,
                "y": 21,
                "x": 0,
                "type": "metric",
                "properties": {
                  "metrics": [
                    [
                      {
                        "expression": "SORT(REMOVE_EMPTY(SEARCH('{CWAgent,InstanceId} MetricName=nginx_up', 'Minimum')), MAX, ASC, 10)",
                        "id": "e1",
                        "period": 60,
                        "label": "Up [avg: ${AVG}, max: ${MAX}] - ${PROP('Dim.InstanceId')}"
                      }
                    ]
                  ],
                  "yAxis": {
                    "left": {
                      "label": "Value",
                      "showUnits": false,
                      "min": 0
                    }
                  },
                  "view": "timeSeries",
                  "stacked": false,
                  "region": {
                    "Ref": "AWS::Region"
                  },
                  "stat": "Average",
                  "period": 60,
                  "title": "Metric Scrape Success"
                }
              }
            ]
          }
        }
      }
    }
  }
}