---
AWSTemplateFormatVersion: 2010-09-09

Description: Reference Architecture to host WordPress on AWS - Creates CloudWatch Dashboard

Metadata:

  Authors:
    Description: Darryl Osborne (darrylo@amazon.com)
  License:
    Description: 'Copyright 2018 Amazon.com, Inc. and its affiliates. All Rights Reserved.
      Licensed under the Amazon Software License (the "License"). You may not use this file
      except in compliance with the License. A copy of the License is located at
      http://aws.amazon.com/asl/
      or in the "license" file accompanying this file. This file is distributed on an "AS IS"
      BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
      License for the specific language governing permissions and limitations under the License.'

  AWS::CloudFormation::Interface:
    ParameterGroups:
    - Label:
        default: AWS Resource Parameters
      Parameters:
        - ElasticFileSystem
        - DatabaseCluster
        - PublicAlbFullName
        - EfsCreateAlarms
        - BurstCreditBalanceDecreaseAlarmArn
        - BurstCreditBalanceIncreaseAlarmArn
        - CriticalAlarmArn
        - WarningAlarmArn
    ParameterLabels:
      BurstCreditBalanceDecreaseAlarmArn:
        default: Decrease Alarm Arn
      BurstCreditBalanceIncreaseAlarmArn:
        default: Increase Alarm Arn
      CriticalAlarmArn:
        default: Critical Alarm Arn
      DatabaseCluster:
        default: Amazon RDS Database Cluster
      ElasticFileSystem:
        default: Amazon EFS File System
      EfsCreateAlarms:
        default: EFS Alarms Created
      PublicAlbFullName:
        default: Amazon ALB Full Name
      WarningAlarmArn:
        default: Warning Alarm Arn

Parameters:

  BurstCreditBalanceDecreaseAlarmArn:
    Default: ''
    Description: Amazon EFS Burst Credit Balance Decrease Alarm Arn
    Type: String
  BurstCreditBalanceIncreaseAlarmArn:
    Default: ''
    Description: Amazon EFS Burst Credit Balance Increase Alarm Arn
    Type: String
  CriticalAlarmArn:
    Default: ''
    Description: Amazon EFS Burst Credit Balance Critical Alarm Arn
    Type: String
  DatabaseCluster:
    Description: Amazon RDS database cluster
    Type: String
  EfsCreateAlarms:
    AllowedValues:
      - true
      - false
    Description: Were Amazon EFS alarms created?
    Type: String
  ElasticFileSystem:
    Description: Amazon EFS file system id.
    Type: String
  PublicAlbFullName:
    Description: Amazon ALB Full Name
    Type: String
  WarningAlarmArn:
    Default: ''
    Description: Amazon EFS Burst Credit Balance Warning Alarm Arn
    Type: String

Conditions:

  Alarms:
    !Equals [ !Ref EfsCreateAlarms, true ]
  NoAlarms:
    !Equals [ !Ref EfsCreateAlarms, false ]

Resources:

  DashboardWithAlarms:
    Condition: Alarms
    Type: AWS::CloudWatch::Dashboard
    Properties:
      DashboardName: !Join [ '_', [ !Ref 'AWS::Region', !Ref 'AWS::StackName' ] ]
      DashboardBody:
        {"Fn::Join":
          [ "",
          [ 
            '{"widgets": [
                  {
                      "type": "metric",
                      "x": 0,
                      "y": 0,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/EFS", "TotalIOBytes", "FileSystemId", "',!Ref 'ElasticFileSystem','", { "stat": "Sum", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS Throughput"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 18,
                      "y": 0,
                      "width": 6,
                      "height": 3,
                      "properties": {
                          "view": "singleValue",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/EFS", "PermittedThroughput", "FileSystemId", "',!Ref 'ElasticFileSystem','", { "stat": "Maximum" } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS PermittedThroughput"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 6,
                      "y": 0,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/EFS", "TotalIOBytes", "FileSystemId", "',!Ref 'ElasticFileSystem','", { "stat": "SampleCount", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS IOPS"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 12,
                      "y": 0,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/EFS", "BurstCreditBalance", "FileSystemId", "',!Ref 'ElasticFileSystem','", { "stat": "Maximum", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS BurstCreditBalance"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 18,
                      "y": 3,
                      "width": 6,
                      "height": 3,
                      "properties": {
                          "view": "singleValue",
                          "metrics": [
                              [ "Custom/EFS", "SizeInBytes", "FileSystemId", "',!Ref 'ElasticFileSystem','" ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS SizeInBytes"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 0,
                      "y": 12,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/ApplicationELB", "RequestCount", "LoadBalancer", "',!Ref 'PublicAlbFullName','", { "stat": "Sum", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "ALB RequestCount"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 6,
                      "y": 12,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/ApplicationELB", "ActiveConnectionCount", "LoadBalancer", "',!Ref 'PublicAlbFullName','", { "stat": "Sum", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "ALB ActiveConnectionCount"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 12,
                      "y": 12,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/RDS", "FreeableMemory", "Role", "READER", "DBClusterIdentifier", "',!Ref 'DatabaseCluster','", { "stat": "Sum", "period": 60 } ],
                              [ "...", "WRITER", ".", ".", { "period": 60, "stat": "Sum" } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "RDS FreeableMemory"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 18,
                      "y": 12,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/RDS", "CPUUtilization", "Role", "READER", "DBClusterIdentifier", "',!Ref 'DatabaseCluster','", { "stat": "Sum", "period": 60 } ],
                              [ "...", "WRITER", ".", ".", { "period": 60, "stat": "Sum" } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "RDS CPUUtilization"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 0,
                      "y": 6,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "title": "EFS Burst Credit Balance Increase Threshold",
                          "annotations": {
                              "alarms": [
                                  "',!Ref 'BurstCreditBalanceIncreaseAlarmArn','"
                              ]
                          },
                          "view": "timeSeries",
                          "stacked": false
                      }
                  },
                  {
                      "type": "metric",
                      "x": 6,
                      "y": 6,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "title": "EFS Burst Credit Balance Decrease Threshold",
                          "annotations": {
                              "alarms": [
                                  "',!Ref 'BurstCreditBalanceDecreaseAlarmArn','"
                              ]
                          },
                          "view": "timeSeries",
                          "stacked": false
                      }
                  },
                  {
                      "type": "metric",
                      "x": 12,
                      "y": 6,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "title": "EFS Burst Credit Balance - Warning",
                          "annotations": {
                              "alarms": [
                                  "',!Ref 'WarningAlarmArn','"
                              ]
                          },
                          "view": "timeSeries",
                          "stacked": false
                      }
                  },
                  {
                      "type": "metric",
                      "x": 18,
                      "y": 6,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "title": "EFS Burst Credit Balance - Critical",
                          "annotations": {
                              "alarms": [
                                  "',!Ref 'CriticalAlarmArn','"
                              ]
                          },
                          "view": "timeSeries",
                          "stacked": false
                      }
                  }
              ]
            }'
          ]
          ]
        }
  DashboardWithNoAlarms:
    Condition: NoAlarms
    Type: AWS::CloudWatch::Dashboard
    Properties:
      DashboardName: !Join [ '_', [ !Ref 'AWS::Region', !Ref 'AWS::StackName' ] ]
      DashboardBody:
        {"Fn::Join":
          [ "",
          [ 
            '{"widgets": [
                  {
                      "type": "metric",
                      "x": 0,
                      "y": 0,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/EFS", "TotalIOBytes", "FileSystemId", "',!Ref 'ElasticFileSystem','", { "stat": "Sum", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS Throughput"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 18,
                      "y": 0,
                      "width": 6,
                      "height": 3,
                      "properties": {
                          "view": "singleValue",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/EFS", "PermittedThroughput", "FileSystemId", "',!Ref 'ElasticFileSystem','", { "stat": "Maximum" } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS PermittedThroughput"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 6,
                      "y": 0,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/EFS", "TotalIOBytes", "FileSystemId", "',!Ref 'ElasticFileSystem','", { "stat": "SampleCount", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS IOPS"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 12,
                      "y": 0,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/EFS", "BurstCreditBalance", "FileSystemId", "',!Ref 'ElasticFileSystem','", { "stat": "Maximum", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS BurstCreditBalance"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 18,
                      "y": 3,
                      "width": 6,
                      "height": 3,
                      "properties": {
                          "view": "singleValue",
                          "metrics": [
                              [ "Custom/EFS", "SizeInBytes", "FileSystemId", "',!Ref 'ElasticFileSystem','" ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "EFS SizeInBytes"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 0,
                      "y": 12,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/ApplicationELB", "RequestCount", "LoadBalancer", "',!Ref 'PublicAlbFullName','", { "stat": "Sum", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "ALB RequestCount"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 6,
                      "y": 12,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/ApplicationELB", "ActiveConnectionCount", "LoadBalancer", "',!Ref 'PublicAlbFullName','", { "stat": "Sum", "period": 60 } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "ALB ActiveConnectionCount"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 12,
                      "y": 12,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/RDS", "FreeableMemory", "Role", "READER", "DBClusterIdentifier", "',!Ref 'DatabaseCluster','", { "stat": "Sum", "period": 60 } ],
                              [ "...", "WRITER", ".", ".", { "period": 60, "stat": "Sum" } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "RDS FreeableMemory"
                      }
                  },
                  {
                      "type": "metric",
                      "x": 18,
                      "y": 12,
                      "width": 6,
                      "height": 6,
                      "properties": {
                          "view": "timeSeries",
                          "stacked": false,
                          "metrics": [
                              [ "AWS/RDS", "CPUUtilization", "Role", "READER", "DBClusterIdentifier", "',!Ref 'DatabaseCluster','", { "stat": "Sum", "period": 60 } ],
                              [ "...", "WRITER", ".", ".", { "period": 60, "stat": "Sum" } ]
                          ],
                          "region": "',!Ref 'AWS::Region','",
                          "title": "RDS CPUUtilization"
                      }
                  }
              ]
            }'
          ]
          ]
        }