AWSTemplateFormatVersion: "2010-09-09"
Description: Resources used for monitoring the scale testing runs using Timestream
Parameters:
  DatabaseName:
    Type: String
    Description: "Timestream database to forward test metrics to"
  TableName:
    Type: String
    Description: "Timestream table to forward scale test metrics to "
  SweeperTableName:
    Type: String
    Description: "Timestream table to forward leaked resources to"
  ResourceCountTableName:
    Type: String
    Description: "Timestream table to count number of resources to"
  BackupDisasterRecoveryRegion:
    Type: String
    Default: "us-east-1"
    Description: "Disaster recovery region for the timestream backup"
Resources:
  TimestreamDatabase:
    Type: AWS::Timestream::Database
    Properties:
      DatabaseName: !Sub "${DatabaseName}"
  TimestreamTable:
    Type: "AWS::Timestream::Table"
    Properties:
      DatabaseName: !Ref "TimestreamDatabase"
      TableName: !Sub "${TableName}"
      MagneticStoreWriteProperties:
        EnableMagneticStoreWrites: true
      RetentionProperties:
        MemoryStoreRetentionPeriodInHours: "2160" # Three months of memory store retention
        MagneticStoreRetentionPeriodInDays: "1825" # 5 years of magnetic store retention
  SweeperTimestreamTable:
    Type: "AWS::Timestream::Table"
    Properties:
      DatabaseName: !Ref "TimestreamDatabase"
      TableName: !Sub "${SweeperTableName}"
      MagneticStoreWriteProperties:
        EnableMagneticStoreWrites: true
      RetentionProperties:
        MemoryStoreRetentionPeriodInHours: "2160" # Three months of memory store retention
        MagneticStoreRetentionPeriodInDays: "1825" # 5 years of magnetic store retention
  ResourceCountTimestreamTable:
    Type: "AWS::Timestream::Table"
    Properties:
      DatabaseName: !Ref "TimestreamDatabase"
      TableName: !Sub "${ResourceCountTableName}"
      MagneticStoreWriteProperties:
        EnableMagneticStoreWrites: true
      RetentionProperties:
        MemoryStoreRetentionPeriodInHours: "2160" # Three months of memory store retention
        MagneticStoreRetentionPeriodInDays: "1825" # 5 years of magnetic store retention
  TimestreamBackupPlan:
    Type: "AWS::Backup::BackupPlan"
    Properties:
      BackupPlan:
        BackupPlanName: !Sub "timestream-${DatabaseName}-backup"
        BackupPlanRule:
          - RuleName: "DailyBackups"
            TargetBackupVault: "Default"
            ScheduleExpression: "cron(0 0 ? * * *)"
            StartWindowMinutes: "480"
            CompletionWindowMinutes: "10080"
            Lifecycle:
              DeleteAfterDays: "35"
            CopyActions:
              - DestinationBackupVaultArn: !Sub "arn:${AWS::Partition}:backup:${BackupDisasterRecoveryRegion}:${AWS::AccountId}:backup-vault:Default"
                Lifecycle:
                  DeleteAfterDays: "70"
  TimestreamBackupPlanSelection:
    Type: "AWS::Backup::BackupSelection"
    Properties:
      BackupSelection:
        SelectionName: !Sub "timestream_${DatabaseName}_selection"
        IamRoleArn: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/service-role/AWSBackupDefaultServiceRole"
        Resources:
          - !GetAtt "TimestreamTable.Arn"
          - !GetAtt "SweeperTimestreamTable.Arn"
          - !GetAtt "ResourceCountTimestreamTable.Arn"
      BackupPlanId: !Ref "TimestreamBackupPlan"
Outputs:
  ScaleTimestreamTable:
    Description: "Scale Test Timestream Table ARN"
    Value: !GetAtt TimestreamTable.Arn
  SweeperTimestreamTable:
    Description: "Account Resource Timestream Table ARN"
    Value: !GetAtt SweeperTimestreamTable.Arn