AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Exports mobile purchases data

Parameters:
  Stack:
    Description: Stack name
    Type: String
  App:
    Description: Application name
    Type: String
  Stage:
    Description: Stage name
    Type: String
    AllowedValues:
      - CODE
      - PROD
  DeployBucket:
    Description: Bucket where RiffRaff uploads artifacts on deploy
    Type: String
  AlarmTopic:
    Type: String
    Description: The ARN of the SNS topic to send all the cloudwatch alarms to
  AccountId:
    Type: String
    Description: The account id of the mobile account
  SubscriptionExportBucket:
    Type: String
    Description: The name of the export subscription bucket
  SubscriptionExportBucketOwner:
    Type: String
    Description: The id of the owner of the export subscription bucket
  UserSubscriptionExportBucket:
    Type: String
    Description: The name of the export user subscription bucket
  SubscriptionEventsExportBucket:
    Type: String
    Description: The name of the export events bucket
  GoogleSubscriptionHistoryExportBucket:
    Type: String
    Description: The name of the export bucket containing the subscription history from Google
  AppleSubscriptionHistoryExportBucket:
    Type: String
    Description: The name of the export bucket containing the subscription history from Apple

Resources:
  ExportLambdasRole:
    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
                - logs:CreateLogStream
                - logs:PutLogEvents
                - cloudwatch:putMetricData
              Resource: "*"
        - PolicyName: dynamo
          PolicyDocument:
            Statement:
              Effect: Allow
              Action:
                - "dynamodb:Scan"
                - "dynamodb:Query"
                - "dynamodb:ExportTableToPointInTime"
              Resource:
                - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${App}-${Stage}-subscriptions
                - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${App}-${Stage}-user-subscriptions
                - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${App}-${Stage}-subscription-events-v2/*
        - PolicyName: s3
          PolicyDocument:
            Statement:
              Action:
                - s3:PutObject
                - s3:PutObjectAcl
              Effect: Allow
              Resource:
                - !Sub arn:aws:s3:::${SubscriptionExportBucket}/*
                - !Sub arn:aws:s3:::${UserSubscriptionExportBucket}/*
                - !Sub arn:aws:s3:::${SubscriptionEventsExportBucket}/*
                - !Sub arn:aws:s3:::${GoogleSubscriptionHistoryExportBucket}/*
                - !Sub arn:aws:s3:::${AppleSubscriptionHistoryExportBucket}/*
        - PolicyName: sqs
          PolicyDocument:
            Statement:
              Action: sqs:*
              Effect: Allow
              Resource:
                - !GetAtt AppleHistoricalSubscriptionsQueue.Arn
                - !GetAtt GoogleHistoricalSubscriptionsQueue.Arn

  ExportSubscriptionTableLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: export-subscription-tables.handler
      Runtime: nodejs14.x
      CodeUri:
        Bucket: !Ref DeployBucket
        Key: !Sub ${Stack}/${Stage}/${App}-export-subscription-tables/export-subscription-tables.zip
      FunctionName: !Sub ${App}-export-subscription-table-${Stage}
      Role: !GetAtt ExportLambdasRole.Arn
      Environment:
        Variables:
          App: !Sub ${App}
          Stack: !Sub ${Stack}
          Stage: !Sub ${Stage}
          ExportBucket: !Ref SubscriptionExportBucket
          ClassName: Subscription
      Description: Export subscription table to the datalake
      MemorySize: 10240
      Timeout: 900
      Events:
        Schedule:
          Type: Schedule
          Properties:
            Schedule: cron(3 0 1/1 * ? *)
      Tags:
        Stage: !Ref Stage
        Stack: !Ref Stack
        App: !Ref App

  ExportUserSubscriptionTableLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: export-subscription-tables.handler
      Runtime: nodejs14.x
      CodeUri:
        Bucket: !Ref DeployBucket
        Key: !Sub ${Stack}/${Stage}/${App}-export-subscription-tables/export-subscription-tables.zip
      FunctionName: !Sub ${App}-export-user-subscription-table-${Stage}
      Role: !GetAtt ExportLambdasRole.Arn
      Environment:
        Variables:
          App: !Sub ${App}
          Stack: !Sub ${Stack}
          Stage: !Sub ${Stage}
          ExportBucket: !Ref UserSubscriptionExportBucket
          ClassName: UserSubscription
      Description: Export the user subscription table to the datalake
      MemorySize: 512
      Timeout: 900
      Events:
        Schedule:
          Type: Schedule
          Properties:
            Schedule: cron(3 0 1/1 * ? *)
      Tags:
        Stage: !Ref Stage
        Stack: !Ref Stack
        App: !Ref App

  ExportSubscriptionTableLambdaV2:
    Type: AWS::Serverless::Function
    Properties:
      Handler: export-subscription-table-v2.handler
      Runtime: nodejs14.x
      CodeUri:
        Bucket: !Ref DeployBucket
        Key: !Sub ${Stack}/${Stage}/${App}-export-subscription-table-v2/export-subscription-table-v2.zip
      FunctionName: !Sub ${App}-export-subscription-table-v2-${Stage}
      Role: !GetAtt ExportLambdasRole.Arn
      Environment:
        Variables:
          App: !Sub ${App}
          Stack: !Sub ${Stack}
          Stage: !Sub ${Stage}
          ExportBucket: !Ref SubscriptionExportBucket
          BucketOwner: !Ref SubscriptionExportBucketOwner
          AccountId: !Ref AccountId
          ClassName: subscriptions
      Description: Export subscription table to the datalake
      MemorySize: 512
      Timeout: 900
      Events:
        Schedule:
          Type: Schedule
          Properties:
            Schedule: cron(3 0 1/1 * ? *)
      Tags:
        Stage: !Ref Stage
        Stack: !Ref Stack
        App: !Ref App

  ExportUserSubscriptionTableLambdaV2:
    Type: AWS::Serverless::Function
    Properties:
      Handler: export-subscription-table-v2.handler
      Runtime: nodejs14.x
      CodeUri:
        Bucket: !Ref DeployBucket
        Key: !Sub ${Stack}/${Stage}/${App}-export-subscription-table-v2/export-subscription-table-v2.zip
      FunctionName: !Sub ${App}-export-user-subscription-table-v2-${Stage}
      Role: !GetAtt ExportLambdasRole.Arn
      Environment:
        Variables:
          App: !Sub ${App}
          Stack: !Sub ${Stack}
          Stage: !Sub ${Stage}
          ExportBucket: !Ref UserSubscriptionExportBucket
          BucketOwner: !Ref SubscriptionExportBucketOwner
          AccountId: !Ref AccountId
          ClassName: user-subscriptions
      Description: Export the user subscription table to the datalake
      MemorySize: 512
      Timeout: 900
      Events:
        Schedule:
          Type: Schedule
          Properties:
            Schedule: cron(3 0 1/1 * ? *)
      Tags:
        Stage: !Ref Stage
        Stack: !Ref Stack
        App: !Ref App

  ExportUserSubscriptionEventsTableLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: export-subscription-events-table.handler
      Runtime: nodejs14.x
      CodeUri:
        Bucket: !Ref DeployBucket
        Key: !Sub ${Stack}/${Stage}/${App}-export-subscription-events-table/export-subscription-events-table.zip
      FunctionName: !Sub ${App}-export-subscription-events-table-${Stage}
      Role: !GetAtt ExportLambdasRole.Arn
      Environment:
        Variables:
          App: !Sub ${App}
          Stack: !Sub ${Stack}
          Stage: !Sub ${Stage}
          ExportBucket: !Ref SubscriptionEventsExportBucket
      Description: Export the subscription event table to the datalake
      MemorySize: 512
      Timeout: 900
      Events:
        Schedule:
          Type: Schedule
          Properties:
            Schedule: cron(3 0 1/1 * ? *)
      Tags:
        Stage: !Ref Stage
        Stack: !Ref Stack
        App: !Ref App

  GoogleHistoricalSubscriptionsQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub ${App}-${Stage}-google-historical-subscriptions
      RedrivePolicy:
        deadLetterTargetArn: !GetAtt GoogleHistoricalSubscriptionsDlq.Arn
        maxReceiveCount: 8
      KmsMasterKeyId: alias/aws/sqs
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App

  GoogleHistoricalSubscriptionsDlq:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub ${App}-${Stage}-google-historical-subscriptions-dlq
      KmsMasterKeyId: alias/aws/sqs
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App

  AppleHistoricalSubscriptionsQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub ${App}-${Stage}-apple-historical-subscriptions
      RedrivePolicy:
        deadLetterTargetArn: !GetAtt AppleHistoricalSubscriptionsDlq.Arn
        maxReceiveCount: 8
      KmsMasterKeyId: alias/aws/sqs
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App

  AppleHistoricalSubscriptionsDlq:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub ${App}-${Stage}-apple-historical-subscriptions-dlq
      KmsMasterKeyId: alias/aws/sqs
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App

  ExportAppleHistoricalSubscriptionsLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: export-historical-data.handler
      Runtime: nodejs14.x
      CodeUri:
        Bucket: !Ref DeployBucket
        Key: !Sub ${Stack}/${Stage}/${App}-export-historical-data/export-historical-data.zip
      FunctionName: !Sub ${App}-export-apple-historical-data-${Stage}
      Role: !GetAtt ExportLambdasRole.Arn
      Environment:
        Variables:
          App: !Sub ${App}
          Stack: !Sub ${Stack}
          Stage: !Sub ${Stage}
          ExportBucket: !Ref AppleSubscriptionHistoryExportBucket
          SqsUrl: !Ref AppleHistoricalSubscriptionsQueue
      Description: Export the historical Apple subscriptions to the data lake
      MemorySize: 3008
      Timeout: 900
      Events:
        Schedule:
          Type: Schedule
          Properties:
            Schedule: cron(3 0 1/1 * ? *)
      Tags:
        Stage: !Ref Stage
        Stack: !Ref Stack
        App: !Ref App

  ExportGoogleHistoricalSubscriptionsLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: export-historical-data.handler
      Runtime: nodejs14.x
      CodeUri:
        Bucket: !Ref DeployBucket
        Key: !Sub ${Stack}/${Stage}/${App}-export-historical-data/export-historical-data.zip
      FunctionName: !Sub ${App}-export-google-historical-data-${Stage}
      Role: !GetAtt ExportLambdasRole.Arn
      Environment:
        Variables:
          App: !Sub ${App}
          Stack: !Sub ${Stack}
          Stage: !Sub ${Stage}
          ExportBucket: !Ref GoogleSubscriptionHistoryExportBucket
          SqsUrl: !Ref GoogleHistoricalSubscriptionsQueue
      Description: Export the historical Google subscriptions to the data lake
      MemorySize: 512
      Timeout: 900
      Events:
        Schedule:
          Type: Schedule
          Properties:
            Schedule: cron(3 0 1/1 * ? *)
      Tags:
        Stage: !Ref Stage
        Stack: !Ref Stack
        App: !Ref App
