AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  run-detector-agents

Parameters:
  Stage:
    Type: String
    Default: staging
    Description: Parameter for getting the deployment stage

Mappings:
  EnvConfigs:
    staging:
      TaskRole: arn:aws:iam::552185537300:role/ce-task
      SecurityGroupIds:
        - sg-08bb42935021e024b
      SubnetIds:
        - subnet-098de5356152db3c2
        - subnet-07b011c7c70726009
        - subnet-0289b81f1633629b6
      RuntimeDestARN: arn:aws:lambda:us-west-2:552185537300:function:ce-runtime
      BaseLayerPy3: arn:aws:lambda:us-west-2:552185537300:layer:base:4
      DeadLetterQueueTargetArn: arn:aws:sns:us-west-2:552185537300:compute-engine-alarm
    prod:
      TaskRole: arn:aws:iam::557537366020:role/ce-task
      SecurityGroupIds:
        - sg-0118e0f1069a02381
      SubnetIds:
        - subnet-0539a75851b24df03
        - subnet-0258434fb4a0d03ef
        - subnet-007f996591222aec2
        - subnet-068050d69d264d42a
        - subnet-0ed2ca77533d511e4
      RuntimeDestARN: arn:aws:lambda:us-west-2:557537366020:function:ce-runtime-prod-v2_0
      BaseLayerPy3: arn:aws:lambda:us-west-2:557537366020:layer:base:31
      DeadLetterQueueTargetArn: arn:aws:sns:us-west-2:557537366020:compute-engine-alarm

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 900

Resources:
  TaskHandler:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      MemorySize: 256 # normally used < 100M
      FunctionName: JOB_FUNC_NAME_PLACEHOLDER
      CodeUri: .
      Handler: lambda_function.lambda_handler
      Runtime: python3.9
      Events:
        AutoTrigger:
          Type: Schedule
          Properties:
             Schedule: rate(3 minutes)
      Architectures:
        - x86_64
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - TaskRole
      DeadLetterQueue:
        Type: SNS
        TargetArn: !FindInMap
          - EnvConfigs
          - Ref: Stage
          - DeadLetterQueueTargetArn
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - BaseLayerPy3
      Tags:
        tenant: autonomous-service-diagnostics
      EventInvokeConfig:
        DestinationConfig:
          OnFailure:
            Type: Lambda
            Destination: !FindInMap
              - EnvConfigs
              - Ref: Stage
              - RuntimeDestARN
          OnSuccess:
            Type: Lambda
            Destination: !FindInMap
              - EnvConfigs
              - Ref: Stage
              - RuntimeDestARN
        MaximumRetryAttempts: 0
      Environment:
        Variables:
          ACTIVITY_BUCKET: diagnostics-activities-prod
          CE_ENDPOINT: CE_ENDPOINT_PLACEHOLDER
          CLINIC_API_ENDPOINT: CLINIC_ENDPOINT_PLACEHOLDER
          CLINIC_API_TOKEN: CLINIC_API_KEY_PLACEHOLDER
          INTERVAL_SECONDS: 180
          DELAY_SECONDS: 240
          LAST_TS_FILE: last_end_ts.json
          OOM_DETECTOR: ce-job-oom-detector
          TIKV_OOM_DETECTOR: ce-job-tikv-oom-detector
          REBOOT_DETECTOR: ce-job-reboot-detector
          TIFLASH_OOM_DETECTOR: ce-job-tiflash-oom-detector
      VpcConfig:
        SecurityGroupIds: !FindInMap
          - EnvConfigs
          - Ref: Stage
          - SecurityGroupIds
        SubnetIds: !FindInMap
          - EnvConfigs
          - Ref: Stage
          - SubnetIds
