AWSTemplateFormatVersion: 2010-09-09
Description: Emits fake breaking news
Parameters:
  App:
    Description: App name
    Type: String
    Default: fakebreakingnews
    AllowedValues:
      - fakebreakingnews
  Stack:
    Description: Stack name
    Type: String
    Default: mobile-notifications
  Stage:
    Description: Stage name
    Type: String
    AllowedValues:
    - CODE
    - PROD
    Default: CODE
  DeployBucket:
    Description: Bucket where RiffRaff uploads artifacts on deploy
    Type: String
    Default: mobile-notifications-dist
  AlarmTopic:
    Type: String
    Description: The ARN of the SNS topic to send all the cloudwatch alarms to

Resources:
  ExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action: sts:AssumeRole
      Path: /
      Policies:
      - PolicyName: logs
        PolicyDocument:
          Statement:
          - Effect: Allow
            Action: logs:CreateLogGroup
            Resource: !Sub "arn:aws:logs:eu-west-1:${AWS::AccountId}:*"
          - Effect: Allow
            Action:
            - logs:CreateLogStream
            - logs:PutLogEvents
            Resource:
            - !Sub "arn:aws:logs:eu-west-1:${AWS::AccountId}:log-group:/aws/lambda/${Stack}-${App}-${Stage}:*"
      - PolicyName: Conf
        PolicyDocument:
          Statement:
          - Action: ssm:GetParametersByPath
            Effect: Allow
            Resource:
              !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/notifications/${Stage}/fakebreakingnews

  FakeBreakingNewsFunction:
    DependsOn:
      - ExecutionRole
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: !Sub ${Stack}-${App}-${Stage}
      Code:
        S3Bucket:
          Ref: DeployBucket
        S3Key: !Sub ${Stack}/${Stage}/fakebreakingnewslambda/fakebreakingnewslambda.jar
      Environment:
        Variables:
          Stage: !Ref Stage
          Stack: !Ref Stack
          App: !Ref App
      Description: Sends a fake breaking news
      Handler: fakebreakingnews.FakeBreakingNewsLambda::handleRequest
      MemorySize: 1024
      Role: !GetAtt ExecutionRole.Arn
      Runtime: java11
      Timeout: 300
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App

  FakeBreakingNewsScheduleEventRule:
    DependsOn:
      - FakeBreakingNewsFunction
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: "cron(5 * * * ? *)"
      Targets:
        - Id: !Sub ${Stack}-${App}-${Stage}
          Arn: !GetAtt FakeBreakingNewsFunction.Arn

  FakeBreakingNewsScheduleEventPermission:
    DependsOn:
      - FakeBreakingNewsFunction
      - FakeBreakingNewsScheduleEventRule
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt FakeBreakingNewsFunction.Arn
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt FakeBreakingNewsScheduleEventRule.Arn

  NotEnoughIosFakeBreakingNewsAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmActions: [!Ref AlarmTopic]
      OKActions: [!Ref AlarmTopic]
      AlarmDescription: !Sub Triggers if not enough Ios dry run notifications are happening on ${Stage}
      ComparisonOperator: LessThanThreshold
      Dimensions:
        - Name: platform
          Value: ios
      EvaluationPeriods: 1
      MetricName: dryrun
      Namespace: !Sub Notifications/${Stage}/workers
      Period: 7200
      Statistic: Sum
      Threshold: 1
      TreatMissingData: breaching

  NotEnoughAndroidFakeBreakingNewsAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmActions: [!Ref AlarmTopic]
      OKActions: [!Ref AlarmTopic]
      AlarmDescription: !Sub Triggers if not enough Android dry run notifications are happening on ${Stage}
      ComparisonOperator: LessThanThreshold
      Dimensions:
        - Name: platform
          Value: android
      EvaluationPeriods: 1
      MetricName: dryrun
      Namespace: !Sub Notifications/${Stage}/workers
      Period: 7200
      Statistic: Sum
      Threshold: 1
      TreatMissingData: breaching
