AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

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-0aea29f5add64487c
    prod:
      TaskRole: arn:aws:iam::557537366020:role/ce-task
      SecurityGroupIds:
        - sg-0118e0f1069a02381
      SubnetIds:
        - subnet-0539a75851b24df03
        - subnet-0258434fb4a0d03ef
        - subnet-007f996591222aec2
        - subnet-068050d69d264d42a

Globals:
  Function:
    Runtime: provided
    Architectures: ["x86_64"]
    Handler: handler
    Timeout: 900
    VpcConfig:
      SecurityGroupIds: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - SecurityGroupIds
      SubnetIds: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - SubnetIds

Resources:
  RuntimeFunc:
    Type: AWS::Serverless::Function
    Properties:
      MemorySize: 256
      FunctionName: ce-framework-runtime
      CodeUri: runtime/
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - TaskRole
      Environment:
        Variables:
          CLINIC_API_KEY: CLINIC_API_KEY_PLACEHOLDER
          MYSQL_DSN: MYSQL_DSN_PLACEHOLDER

  SchedulerFunc:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: ce-framework-scheduler
      MemorySize: 512
      CodeUri: scheduler/
      Events:
        SchedulerApi:
          Type: Api
          Properties:
            Path: /
            Method: ANY
        SchedulerApiProd:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: ANY
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - TaskRole
      Environment:
        Variables:
          PROXY_FORMAT: v1
          MYSQL_DSN: MYSQL_DSN_PLACEHOLDER

  TriggerFunc:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: ce-framework-trigger
      CodeUri: trigger/
      Handler: trigger.lambda_handler
      Runtime: python3.9
      Events:
        AutoTrigger:
          Type: Schedule
          Properties:
             Schedule: rate(1 minute)
      Role: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - TaskRole
      Environment:
        Variables:
          CE_ENDPOINT: CE_ENDPOINT_PLACEHOLDER
          CLINIC_ENDPOINT: CLINIC_ENDPOINT_PLACEHOLDER
          CLINIC_API_KEY: CLINIC_API_KEY_PLACEHOLDER
