AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sli lambda functions

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

Mappings:
  EnvConfigs:
    dev:
      Role: arn:aws:iam::552185537300:role/sli-dev-analyzer-role
      Layer: arn:aws:lambda:us-west-2:552185537300:layer:sli-layer:22
      SecurityGroupIds: 
        - sg-08bb42935021e024b
      SubnetIds:
        - subnet-098de5356152db3c2
        - subnet-0aea29f5add64487c
      S3Bucket: sli-dev-metrics
      DbHost: gateway01.us-west-2.prod.aws.tidbcloud.com
      DbUser: 2669JJhnvim6egm.root
      ActivityHubEndpoint: b9u12esjuf.execute-api.us-west-2.amazonaws.com
      JiraProject: O11YDEV
      JiraURL: https://tidb.atlassian.net
      JiraUsername: charles.m@pingcap.com
      ClinicEndpoint: http://clinic-staging-1072990385.us-west-2.elb.amazonaws.com:8085
      DevOpsUrl: https://staging.devops.pingcap.com
      ByocAssumedRole: tidbcloud-sli-assumed-role
      ByocLambdaRoleArn: arn:aws:iam::552185537300:role/sli-dev-byoc-dataplane-manager
    prod:
      Role: arn:aws:iam::557537366020:role/sli-prod-analyzer-role
      Layer: arn:aws:lambda:us-west-2:557537366020:layer:sli-layer:15
      SecurityGroupIds:
        - sg-0118e0f1069a02381
      SubnetIds:
        - subnet-0539a75851b24df03
        - subnet-0258434fb4a0d03ef
        - subnet-007f996591222aec2
        - subnet-068050d69d264d42a
      S3Bucket: sli-prod-metrics
      DbHost: gateway01.us-west-2.prod.aws.tidbcloud.com
      DbUser: RDgFArWiyRK5wbH.dbaas-sli
      ActivityHubEndpoint: e4hr1v4oqh.execute-api.us-west-2.amazonaws.com
      JiraProject: O11Y
      JiraURL: https://tidb.atlassian.net
      JiraUsername: charles.m@pingcap.com
      ClinicEndpoint: https://clinic.pingcap.com
      DevOpsUrl: https://prod.devops.pingcap.com
      ByocAssumedRole: tidbcloud-sli-assumed-role
      ByocLambdaRoleArn: arn:aws:iam::557537366020:role/sli-prod-byoc-dataplane-manager

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 180
    Runtime: python3.13
    Architectures:
      - x86_64
    Tags:
      tenant: sli-analyzer
    VpcConfig:
      SecurityGroupIds: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - SecurityGroupIds
      SubnetIds: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - SubnetIds
    Environment:
      Variables:
        SLI_STAGE: !Ref Stage
        SLI_DB_HOST: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - DbHost
        SLI_DB_USER: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - DbUser
        SLI_SECRET_NAME: !Sub 'sli-${Stage}-analyzer-secret'
        SLI_S3_BUCKET_NAME: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - S3Bucket
        ACTIVITY_HUB_ENDPOINT: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - ActivityHubEndpoint
        ACTIVITY_HUB_SOURCE: sli-analyzer
        JIRA_PROJECT: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - JiraProject
        JIRA_URL: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - JiraURL
        JIRA_USERNAME: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - JiraUsername
        CLINIC_ENDPOINT: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - ClinicEndpoint
        DEV_OPS_URL: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - DevOpsUrl

Resources:
  SliDedicatedClusterDowntimeFunction:
    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:
      FunctionName: sli-dedicated-cluster-downtime
      CodeUri: jobs/sli_dedicated_cluster_downtime/
      Handler: app.lambda_handler
      MemorySize: 512
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(2 minutes)'
            Name: sli-dedicated-cluster-downtime-hourly
            Enabled: True
  SliByocClusterDowntimeFunction:
    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:
      FunctionName: sli-byoc-cluster-downtime
      CodeUri: jobs/sli_byoc_cluster_downtime/
      Handler: app.lambda_handler
      MemorySize: 512
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - ByocLambdaRoleArn
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Environment:
        Variables:
          SLI_ASSUME_ROLE: !FindInMap
          - EnvConfigs
          - Ref: Stage
          - ByocAssumedRole
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(2 minutes)'
            Name: sli-byoc-cluster-downtime-hourly
            Enabled: True
  SlaProbeTiDBClusterFunction:
    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:
      FunctionName: sla-probe-tidb-cluster
      CodeUri: jobs/sla_probe_tidb_cluster/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(5 minutes)'
            Name: sla-probe-tidb-cluster-hourly
            Enabled: True
  SliO11yPrometheusAvailableFunction:
    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:
      FunctionName: sli-o11y-prometheus-available
      CodeUri: jobs/sli_o11y_prometheus_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(30 minutes)'
            Name: sli-o11y-prometheus-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SlaO11yPrometheusAvailableFunction:
    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:
      FunctionName: sla-o11y-prometheus-available
      CodeUri: jobs/sla_o11y_prometheus_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(1 day)'
            Name: sla-o11y-prometheus-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SliO11yCreateClusterAvailableFunction:
    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:
      FunctionName: sli-o11y-create-cluster-available
      CodeUri: jobs/sli_o11y_create_cluster_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(30 minutes)'
            Name: sli-o11y-create-cluster-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SlaO11yCreateClusterAvailableFunction:
    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:
      FunctionName: sla-o11y-create-cluster-available
      CodeUri: jobs/sla_o11y_create_cluster_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(1 day)'
            Name: sla-o11y-create-cluster-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SliO11yGetClusterAvailableFunction:
    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:
      FunctionName: sli-o11y-get-cluster-available
      CodeUri: jobs/sli_o11y_get_cluster_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(30 minutes)'
            Name: sli-o11y-get-cluster-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SlaO11yGetClusterAvailableFunction:
    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:
      FunctionName: sla-o11y-get-cluster-available
      CodeUri: jobs/sla_o11y_get_cluster_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(1 day)'
            Name: sla-o11y-get-cluster-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SliO11yLokiAvailableFunction:
    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:
      FunctionName: sli-o11y-loki-available
      CodeUri: jobs/sli_o11y_loki_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(30 minutes)'
            Name: sli-o11y-loki-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SlaO11yLokiAvailableFunction:
    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:
      FunctionName: sla-o11y-loki-available
      CodeUri: jobs/sla_o11y_loki_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(1 day)'
            Name: sla-o11y-loki-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SliO11yVmAvailableFunction:
    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:
      FunctionName: sli-o11y-vm-available
      CodeUri: jobs/sli_o11y_vm_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(30 minutes)'
            Name: sli-o11y-vm-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SlaO11yVmAvailableFunction:
    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:
      FunctionName: sla-o11y-vm-available
      CodeUri: jobs/sla_o11y_vm_available/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(1 day)'
            Name: sla-o11y-vm-available-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SliO11yPrometheusUploadDelayFunction:
    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:
      FunctionName: sli-o11y-prometheus-upload-delay
      CodeUri: jobs/sli_o11y_prometheus_upload_delay/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(30 minutes)'
            Name: sli-o11y-prometheus-upload-delay-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SlaO11yPrometheusUploadDelayFunction:
    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:
      FunctionName: sla-o11y-prometheus-upload-delay
      CodeUri: jobs/sla_o11y_prometheus_upload_delay/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(1 day)'
            Name: sla-o11y-prometheus-upload-delay-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SliO11yS3LogsUploadDelayFunction:
    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:
      FunctionName: sli-o11y-s3-logs-upload-delay
      CodeUri: jobs/sli_o11y_s3_logs_upload_delay/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(30 minutes)'
            Name: sli-o11y-s3-logs-upload-delay-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SlaO11yS3LogsUploadDelayFunction:
    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:
      FunctionName: sla-o11y-s3-logs-upload-delay
      CodeUri: jobs/sla_o11y_s3_logs_upload_delay/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(1 day)'
            Name: sla-o11y-s3-logs-upload-delay-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SliO11yS3MetricsUploadDelayFunction:
    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:
      FunctionName: sli-o11y-s3-metrics-upload-delay
      CodeUri: jobs/sli_o11y_s3_metrics_upload_delay/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(30 minutes)'
            Name: sli-o11y-s3-metrics-upload-delay-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
  SlaO11yS3MetricsUploadDelayFunction:
    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:
      FunctionName: sla-o11y-s3-metrics-upload-delay
      CodeUri: jobs/sla_o11y_s3_metrics_upload_delay/
      Handler: app.lambda_handler
      MemorySize: 256
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - Role
      Layers:
        - !FindInMap
          - EnvConfigs
          - Ref: Stage
          - Layer
      Events:
        EventBridge:
          Type: Schedule
          Properties:
            Schedule: 'rate(1 day)'
            Name: sla-o11y-s3-metrics-upload-delay-hourly
            Enabled: SCHEDULE_ENABLED_PLACEHOLDER
