
service: 'tcf-test-cw-alarms'

provider:
  name: aws
  runtime: python3.7
  profile: 'tcf-test'
  stackName: 'tcf-test-cw-alarms'
  region: 'af-south-1'
  stage: ${self:provider.profile}
  deploymentBucket:
    name: ${self:custom.ServerlessBucketName}
    serverSideEncryption: AES256
    blockPublicAccess: true

plugins:
  - serverless-deployment-bucket
  - serverless-parameters

custom:
  CustomerName: tcf
  StageName: test
  # Combination of profile and function name used as bucket name:
  ResourceName: serverless-framework # Only lowercase letters and numbers allowed. Used to populate bucket name.
  ServerlessBucketName: '${self:custom.ResourceName}-${self:provider.stage}-${self:provider.region}'
  FlowLogBucketName: '${self:custom.ResourceName}-${self:provider.stage}-${self:provider.region}-flowlog'

resources:

  # CloudFormation resource templates

  Resources:

    # Create cpu alarm for Ec2

    CPUAlarm:
      Type: AWS::CloudWatch::Alarm
      Properties:
        AlarmDescription: CPU alarm for my instance
        OKActions:
          - Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-SNSTechnicalNotification'
        AlarmActions:
          - Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-SNSTechnicalNotification'
        MetricName: CPUUtilization
        Namespace: AWS/EC2
        Statistic: Average
        Period: '300'
        EvaluationPeriods: '3'
        Threshold: '90'
        ComparisonOperator: GreaterThanThreshold
        Dimensions:
          - Name: InstanceId
            Value:
              Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-Ec2InstanceId'
        TreatMissingData: breaching

    StatusCheckInstanceAlarm:
      Type: AWS::CloudWatch::Alarm
      Properties:
        AlarmDescription: CPU alarm for my instance
        OKActions:
          - Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-SNSTechnicalNotification'
        AlarmActions:
          - Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-SNSTechnicalNotification'
        MetricName: StatusCheckFailed_Instance
        Namespace: AWS/EC2
        Statistic: Average
        Period: '300'
        EvaluationPeriods: '3'
        Threshold: '1'
        ComparisonOperator: GreaterThanThreshold
        Dimensions:
          - Name: InstanceId
            Value:
              Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-Ec2InstanceId'
        TreatMissingData: breaching

    StatusCheckSystemAlarm:
      Type: AWS::CloudWatch::Alarm
      Properties:
        AlarmDescription: CPU alarm for my instance
        OKActions:
          - Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-SNSTechnicalNotification'
        AlarmActions:
          - Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-SNSTechnicalNotification'
        MetricName: StatusCheckFailed_System
        Namespace: AWS/EC2
        Statistic: Average
        Period: '300'
        EvaluationPeriods: '3'
        Threshold: '1'
        ComparisonOperator: GreaterThanThreshold
        Dimensions:
          - Name: InstanceId
            Value:
              Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-Ec2InstanceId'
        TreatMissingData: breaching
