service: retrofit-platform

provider:
  name: aws
  region: eu-west-2
  runtime: python3.11
  architecture: x86_64
  environment:
    API_KEY: ${env:API_KEY}
    ENVIRONMENT: ${env:ENVIRONMENT}
    SECRET_KEY: ${env:SECRET_KEY}
    PLAN_TRIGGER_BUCKET: ${env:PLAN_TRIGGER_BUCKET}
    DATA_BUCKET: ${env:DATA_BUCKET}
    PREDICTIONS_BUCKET: ${env:PREDICTIONS_BUCKET}
    DOMAIN_NAME: ${env:DOMAIN_NAME}
    EPC_AUTH_TOKEN: ${env:EPC_AUTH_TOKEN}
    DB_HOST: ${env:DB_HOST}
    DB_NAME: ${env:DB_NAME}
    DB_USERNAME: ${env:DB_USERNAME}
    DB_PASSWORD: ${env:DB_PASSWORD}
    DB_PORT: ${env:DB_PORT}
    ECR_URI: ${env:ECR_URI}
    GITHUB_SHA: ${env:GITHUB_SHA}
    SAP_PREDICTIONS_BUCKET: ${env:SAP_PREDICTIONS_BUCKET}
    CARBON_PREDICTIONS_BUCKET: ${env:CARBON_PREDICTIONS_BUCKET}
    HEAT_PREDICTIONS_BUCKET: ${env:HEAT_PREDICTIONS_BUCKET}
    HEATING_KWH_PREDICTIONS_BUCKET: ${env:HEATING_KWH_PREDICTIONS_BUCKET}
    HOTWATER_KWH_PREDICTIONS_BUCKET: ${env:HOTWATER_KWH_PREDICTIONS_BUCKET}
    ENERGY_ASSESSMENTS_BUCKET: ${env:ENERGY_ASSESSMENTS_BUCKET}
    GOOGLE_SOLAR_API_KEY: ${env:GOOGLE_SOLAR_API_KEY}
    SAP_BASELINE_PREDICTIONS_BUCKET: ${env:SAP_BASELINE_PREDICTIONS_BUCKET}
    CARBON_BASELINE_PREDICTIONS_BUCKET: ${env:CARBON_BASELINE_PREDICTIONS_BUCKET}
    HEAT_BASELINE_PREDICTIONS_BUCKET: ${env:HEAT_BASELINE_PREDICTIONS_BUCKET}
    ENGINE_SQS_URL:
      Ref: EngineQueue
    # hardcoded queues (created in terraform)
    CATEGORISATION_SQS_URL: "https://sqs.eu-west-2.amazonaws.com/337213553626/categorisation-queue-dev"
    POSTCODE_SPLITTER_SQS_URL: "https://sqs.eu-west-2.amazonaws.com/337213553626/postcode-splitter-queue-dev"
    COMBINER_SQS_URL: "https://sqs.eu-west-2.amazonaws.com/337213553626/bulk-address2uprn-combiner-queue-dev"

plugins:
  - serverless-python-requirements
  - serverless-domain-manager

custom:
  pythonRequirements:
    fileName: backend/app/requirements/requirements.txt
    dockerizePip: true
  customDomain:
    domainName: api.${self:provider.environment.DOMAIN_NAME}
    createRoute53Record: true
    certificateArn: ${ssm:/ssl_certificate_arn}

functions:

  fastapi-backend:
    handler: backend.app.main.handler
    timeout: 600
    memorySize: 512
    role: FastApiLambdaRole
    events:
      - http:
          path: /{proxy+}
          method: ANY

  model-engine-lambda:
    image:
      uri: ${env:ECR_URI}:${env:GITHUB_SHA}
    timeout: 900
    memorySize: 3008
    role: EngineLambdaRole
    events:
      - sqs:
          arn: arn:aws:sqs:${self:provider.region}:${aws:accountId}:model-engine-queue
          batchSize: 1
          maximumConcurrency: 12  # Heavily restricts concurrency to avoid overwhelming the ldmbda limits


resources:
  Resources:
    EngineQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: model-engine-queue
        VisibilityTimeout: 910 # must be >= lambda timeout (900)

    FastApiLambdaRole:
      Type: AWS::IAM::Role
      Properties:
        RoleName: retrofit-fastapi-lambda-role-${self:provider.region}
        AssumeRolePolicyDocument:
          Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - lambda.amazonaws.com
              Action:
                - sts:AssumeRole
        Policies:
          - PolicyName: FastApiLambdaPolicy
            PolicyDocument:
              Version: '2012-10-17'
              Statement:
                - Effect: Allow
                  Action:
                    - logs:CreateLogGroup
                    - logs:CreateLogStream
                    - logs:PutLogEvents
                  Resource: arn:aws:logs:*:*:*
                - Effect: Allow
                  Action:
                    - sqs:SendMessage
                  Resource:
                    - Fn::GetAtt: [ EngineQueue, Arn ]
                    - "arn:aws:sqs:eu-west-2:337213553626:categorisation-queue-dev"
                    - "arn:aws:sqs:eu-west-2:337213553626:postcode-splitter-queue-dev"
                    - "arn:aws:sqs:eu-west-2:337213553626:bulk-address2uprn-combiner-queue-dev"
                - Effect: Allow
                  Action:
                    - s3:GetObject
                    - s3:ListBucket
                  Resource:
                    - arn:aws:s3:::${env:PLAN_TRIGGER_BUCKET}
                    - arn:aws:s3:::${env:PLAN_TRIGGER_BUCKET}/*
                    - arn:aws:s3:::${env:DATA_BUCKET}/*
                    - arn:aws:s3:::${env:ENERGY_ASSESSMENTS_BUCKET}/*
                    - arn:aws:s3:::${env:SAP_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:CARBON_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:HEAT_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:HEATING_KWH_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:HOTWATER_KWH_PREDICTIONS_BUCKET}/*

    EngineLambdaRole:
      Type: AWS::IAM::Role
      Properties:
        RoleName: retrofit-engine-lambda-role-${self:provider.region}
        AssumeRolePolicyDocument:
          Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - lambda.amazonaws.com
              Action:
                - sts:AssumeRole
        Policies:
          - PolicyName: EngineLambdaPolicy
            PolicyDocument:
              Version: '2012-10-17'
              Statement:
                - Effect: Allow
                  Action:
                    - logs:CreateLogGroup
                    - logs:CreateLogStream
                    - logs:PutLogEvents
                  Resource: arn:aws:logs:*:*:*
                - Effect: Allow
                  Action:
                    - sqs:ReceiveMessage
                    - sqs:DeleteMessage
                    - sqs:GetQueueAttributes
                  Resource:
                    - Fn::GetAtt: [ EngineQueue, Arn ]
                - Effect: Allow
                  Action:
                    - s3:*
                  Resource:
                    - arn:aws:s3:::${env:PLAN_TRIGGER_BUCKET}
                    - arn:aws:s3:::${env:PLAN_TRIGGER_BUCKET}/*
                    - arn:aws:s3:::${env:PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:DATA_BUCKET}
                    - arn:aws:s3:::${env:DATA_BUCKET}/*
                    - arn:aws:s3:::${env:ENERGY_ASSESSMENTS_BUCKET}
                    - arn:aws:s3:::${env:ENERGY_ASSESSMENTS_BUCKET}/*
                    - arn:aws:s3:::${env:SAP_PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:SAP_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:CARBON_PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:CARBON_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:HEAT_PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:HEAT_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:HEATING_KWH_PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:HEATING_KWH_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:HOTWATER_KWH_PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:HOTWATER_KWH_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:SAP_BASELINE_PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:SAP_BASELINE_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:CARBON_BASELINE_PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:CARBON_BASELINE_PREDICTIONS_BUCKET}/*
                    - arn:aws:s3:::${env:HEAT_BASELINE_PREDICTIONS_BUCKET}
                    - arn:aws:s3:::${env:HEAT_BASELINE_PREDICTIONS_BUCKET}/*
