---
policy: /ce/ca/aws/dynamodb/table-unused
logic: /ce/ca/aws/dynamodb/table-unused/prod.logic.yaml
executionTime: 2025-12-27T12:01:46.195993615Z
generationMs: 68
executionMs: 603
rows:
  - id: '001'
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__tableStatus__c') != 'ACTIVE'
      actual: extract('CA10__tableStatus__c') != 'ACTIVE'
    runtimeError: {}
  - id: '002'
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: CA10__creationDateTime__c.withinLastDays(30)
      actual: CA10__creationDateTime__c.withinLastDays(30)
    runtimeError: {}
  - id: '003'
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: extract('CA10__itemCount__c') == number(0.0)
      actual: extract('CA10__itemCount__c') == number(0.0)
    runtimeError: {}
  - id: '004'
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 400
      actual: 400
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/dynamodb/table-unused/policy.yaml
    md5Hash: B5979C3E4755A572EADD36F7A82BB076
    content: "---\nnames:\n  full: \"AWS DynamoDB Table is unused\"\n  contextual:\
      \ \"Table is unused\"\ndescription: >\n  Identify AWS DynamoDB Tables that appear\
      \ to be unused (empty). A table is \n  considered unused if it has been active\
      \ for more than 30 days but contains \n  zero items. Removing unused tables\
      \ helps reduce storage costs and clutter.\ntype: \"COMPLIANCE_POLICY\"\ncategories:\n\
      \  - \"COST\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction\"\
      \n  - \"/frameworks/aws-well-architected/cost/04/02\"\nsimilarPolicies:\n  cloudConformity:\n\
      \    - url: https://www.trendmicro.com/cloudoneconformity/knowledge-base/aws/DynamoDB/unused-dynamodb-tables.html\n\
      \      name: Unused Table"
  - path: /ce/ca/aws/dynamodb/table-unused/prod.logic.yaml
    md5Hash: 0249B119F58AA73435DDBDAC60F0E9CE
    content: |
      inputType: "CA10__CaAwsDynamoDbTable__c"
      testData:
        - file: "test-data.json"
      importExtracts:
        - file: /types/CA10__CaAwsDynamoDbTable__c/object.extracts.yaml
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The table is not active."
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__tableStatus__c"
              right:
                TEXT: "ACTIVE"
        - status: "INAPPLICABLE"
          currentStateMessage: "The table was created less than 30 days ago."
          check:
            IS_WITHIN_LAST_DAYS:
              offsetDays: 30
              arg:
                FIELD:
                  path: "CA10__creationDateTime__c"
        - status: "INCOMPLIANT"
          currentStateMessage: "The DynamoDB Table is not used and contains 0 items."
          remediationMessage: "Consider deleting the Table."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10__itemCount__c"
              right:
                NUMBER: 0.0
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The Table contains items and is considered in use."
  - path: /ce/ca/aws/dynamodb/table-unused/test-data.json
    md5Hash: 546CA49183A182C061EDF86B1C14AD35
    content: |
      [
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__tableStatus__c') != 'ACTIVE'",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-12-02T01:10:46Z" },
          "CA10__disappearanceTime__c": null,
          "CA10__tableStatus__c": "INACTIVE",
          "CA10__creationDateTime__c": "2024-12-02T01:10:46Z",
          "CA10__itemCount__c": null,
          "Id": "001"
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "299",
            "conditionText": "CA10__creationDateTime__c.withinLastDays(30)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-12-02T01:10:46Z" },
          "CA10__disappearanceTime__c": null,
          "CA10__tableStatus__c": "ACTIVE",
          "CA10__creationDateTime__c": "2025-11-02T01:10:46Z",
          "CA10__itemCount__c": 0,
          "Id": "002"
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "399",
            "conditionText": "extract('CA10__itemCount__c') == number(0.0)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-12-02T01:10:46Z" },
          "CA10__disappearanceTime__c": null,
          "CA10__tableStatus__c": "ACTIVE",
          "CA10__creationDateTime__c": "2024-12-02T01:10:46Z",
          "CA10__itemCount__c": 0,
          "Id": "003"
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "400",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-12-02T01:10:46Z" },
          "CA10__disappearanceTime__c": null,
          "CA10__tableStatus__c": "ACTIVE",
          "CA10__creationDateTime__c": "2024-12-02T01:10:46Z",
          "CA10__itemCount__c": 7,
          "Id": "004"
        }
      ]
  - path: /types/CA10__CaAwsDynamoDbTable__c/object.extracts.yaml
    md5Hash: EC959FF9A6572890388AA7C1C47122EF
    content: "---\nextracts:\n# Nullable, null is equal to PROVISIONED\n  - name:\
      \ \"CA10__billingMode__c\"\n    value:\n      FIELD:\n        path: CA10__billingMode__c\n\
      \        # undeterminedIf:\n        #   noAccessDelegate:\n        #     path:\
      \ \"CA10__billingMode__c\"\n        #     currentStateMessage: \"Unable to determine\
      \ Table Billing Mode. Possible permission issue with dynamodb:DescribeTable.\"\
      \n# Values: ENABLED | DISABLED. Not nullable. \n  - name: \"CA10__pointInTimeRecoveryStatus__c\"\
      \n    value:\n      FIELD:\n        path: CA10__pointInTimeRecoveryStatus__c\n\
      \        # undeterminedIf:\n        #   noAccessDelegate:\n        #     path:\
      \ \"CA10__pointInTimeRecoveryStatus__c\"\n        #     currentStateMessage:\
      \ \"Unable to determine Point In Time Recovery Status. Possible permission issue\
      \ with dynamodb:DescribeTable.\"\n# Nullable.\n  - name: \"CA10__regionName2__c\"\
      \n    value:\n      FIELD:\n        path: \"CA10__regionName2__c\"\n# Values:\
      \ CREATING | UPDATING | DELETING | ACTIVE | INACCESSIBLE_ENCRYPTION_CREDENTIALS\
      \ | \n# ARCHIVING | ARCHIVED | REPLICATION_NOT_AUTHORIZED\n# Not nullable. Possible\
      \ permission issue with dynamodb:DescribeTable.\n  - name: \"CA10__tableStatus__c\"\
      \n    value:\n      FIELD:\n        path: \"CA10__tableStatus__c\"\n# Values:\
      \ ENABLED | null. Possible permission issue with dynamodb:DescribeTable.\n \
      \ - name: \"CA10__sseDescriptionStatus__c\"\n    value:\n      FIELD:\n    \
      \    path: \"CA10__sseDescriptionStatus__c\"\n# Not nullable. Number\n  - name:\
      \ \"CA10__itemCount__c\"\n    value:\n      FIELD:\n        path: \"CA10__itemCount__c\"\
      \n        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__itemCount__c\"\n            currentStateMessage: \"Dynamo DB Table item\
      \ count cannot be empty. Possible permission issue with dynamodb:DescribeTable\"\
      \n# Not nullable.\n  - name: \"CA10__creationDateTime__c\"\n    value:\n   \
      \   FIELD:\n        path: \"CA10__creationDateTime__c\"\n        undeterminedIf:\n\
      \          noAccessDelegate:\n            path: \"CA10__creationDateTime__c\"\
      \n            currentStateMessage: \"Dynamo DB Table creation date cannot be\
      \ empty. Possible permission issue with dynamodb:DescribeTable\"\n"
script: |-
  CREATE TEMP FUNCTION mock_ExpectedResult()
  RETURNS ARRAY<STRUCT<
          Id STRING,
          expectedResult STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, runtimeError STRING>
      >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "Id" : "001",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__tableStatus__c') != 'ACTIVE'",
      "runtimeError" : null
    }
  }, {
    "Id" : "002",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "299",
      "conditionText" : "CA10__creationDateTime__c.withinLastDays(30)",
      "runtimeError" : null
    }
  }, {
    "Id" : "003",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "399",
      "conditionText" : "extract('CA10__itemCount__c') == number(0.0)",
      "runtimeError" : null
    }
  }, {
    "Id" : "004",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "400",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsDynamoDbTable__c()
  RETURNS ARRAY<STRUCT<
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__tableStatus__c STRING,
      CA10__creationDateTime__c TIMESTAMP,
      CA10__itemCount__c FLOAT64,
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-12-02T01:10:46Z")
    },
    "CA10__tableStatus__c" : "INACTIVE",
    "CA10__creationDateTime__c" : new Date("2024-12-02T01:10:46Z"),
    "CA10__itemCount__c" : null,
    "Id" : "001"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-02T01:10:46Z")
    },
    "CA10__tableStatus__c" : "ACTIVE",
    "CA10__creationDateTime__c" : new Date("2025-11-02T01:10:46Z"),
    "CA10__itemCount__c" : 0,
    "Id" : "002"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-02T01:10:46Z")
    },
    "CA10__tableStatus__c" : "ACTIVE",
    "CA10__creationDateTime__c" : new Date("2024-12-02T01:10:46Z"),
    "CA10__itemCount__c" : 0,
    "Id" : "003"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-02T01:10:46Z")
    },
    "CA10__tableStatus__c" : "ACTIVE",
    "CA10__creationDateTime__c" : new Date("2024-12-02T01:10:46Z"),
    "CA10__itemCount__c" : 7,
    "Id" : "004"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsDynamoDbTable__c(
      obj STRUCT<
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__tableStatus__c STRING,
          CA10__creationDateTime__c TIMESTAMP,
          CA10__itemCount__c FLOAT64,
          Id STRING
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
  var TextLib = new function () {
      this.normalize = function(arg) {
          return arg == null ? '' : arg.replace(/\s+/g, ' ').trim().toLowerCase();
      };
      this.isEmpty = function(arg) {
          return this.normalize(arg) == '';
      };
      this.isNotEmpty = function(arg) {
          return this.normalize(arg) != '';
      };
      this.equal = function(left, right) {
          return this.normalize(left) == this.normalize(right);
      };
      this.notEqual = function(left, right) {
          return this.normalize(left) != this.normalize(right);
      };
      this.startsWith = function(arg, substring) {
          return this.normalize(arg).startsWith(this.normalize(substring));
      };
      this.endsWith = function(arg, substring) {
          return this.normalize(arg).endsWith(this.normalize(substring));
      };
      this.contains = function(arg, substring) {
          return this.normalize(arg).includes(this.normalize(substring));
      };
      this.containsAll = function(arg, substrings) {
          if (substrings == null || substrings.length === 0) return false;
          let normalizedArg = this.normalize(arg);
          return substrings.every(sub => normalizedArg.includes(this.normalize(sub)));
      };
      this.containsAny = function(arg, substrings) {
          if (substrings == null || substrings.length === 0) return false;
          let normalizedArg = this.normalize(arg);
          return substrings.some(sub => normalizedArg.includes(this.normalize(sub)));
      };
  }();
  var IsEmptyLib = new function () {
      this.simpleIsEmpty = function(arg) {
          return arg == null;
      };
      this.simpleIsNotEmpty = function(arg) {
          return arg != null;
      };
  }();
      var todayMinus30Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (-30 * 86400000);
      var todayPlus1Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (1 * 86400000);
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From AWS [CA10__disappearanceTime__c]: ' + obj.CA10__disappearanceTime__c);
      if (obj.CA10__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10__disappearanceTime__c)", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[1], conditionIndex:[100..199]
      function extract3() { if (!this.out) { this.out = obj.CA10__tableStatus__c; } return this.out; };
      references1.push('Table Status [obj.CA10__tableStatus__c]: ' + obj.CA10__tableStatus__c);
      if (TextLib.notEqual(extract3.call(extract3), 'ACTIVE')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__tableStatus__c') != 'ACTIVE'", currentStateMessage: "The table is not active.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      references1.push('Creation Date Time [obj.CA10__creationDateTime__c]: ' + obj.CA10__creationDateTime__c);
      if (obj.CA10__creationDateTime__c != null && obj.CA10__creationDateTime__c.getTime() >= todayMinus30Day && obj.CA10__creationDateTime__c.getTime() < todayPlus1Day) {
          return {status: 'INAPPLICABLE', conditionIndex: 299, conditionText: "CA10__creationDateTime__c.withinLastDays(30)", currentStateMessage: "The table was created less than 30 days ago.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      function fieldChecked9() {
          if (IsEmptyLib.simpleIsEmpty(obj.CA10__itemCount__c)) {
              throw new Error("UNDETERMINED condition:301", {cause: {status: 'UNDETERMINED', conditionIndex: 301, conditionText: "CA10__itemCount__c.delegatedTo(CA10__itemCount__c).isEmpty()", currentStateMessage: "Dynamo DB Table item count cannot be empty. Possible permission issue with dynamodb:DescribeTable", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__itemCount__c;
      }
      function extract8() { if (!this.out) { this.out = fieldChecked9(); } return this.out; };
      references1.push('Item Count [obj.CA10__itemCount__c]: ' + obj.CA10__itemCount__c);
      try {
          if (extract8.call(extract8) == 0.0) {
              return {status: 'INCOMPLIANT', conditionIndex: 399, conditionText: "extract('CA10__itemCount__c') == number(0.0)", currentStateMessage: "The DynamoDB Table is not used and contains 0 items.", currentStateReferences: references1.join('\n'), remediation: "Consider deleting the Table.", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'COMPLIANT', conditionIndex: 400, conditionText: "otherwise", currentStateMessage: "The Table contains items and is considered in use.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;


  SELECT
    expectedResult.Id as Id,
    IF (
      IFNULL(expectedResult.expectedResult.status, '') = IFNULL(sObject.result.status, '')
      AND IFNULL(expectedResult.expectedResult.conditionIndex, -1) = IFNULL(sObject.result.conditionIndex, -1)
      AND IFNULL(expectedResult.expectedResult.conditionText, '') = IFNULL(sObject.result.conditionText, '')
      AND IFNULL(expectedResult.expectedResult.runtimeError, '') = IFNULL(sObject.result.runtimeError, ''),
      "MATCH",
      "FAIL"
    ) as match,
    expectedResult.expectedResult.status as expectedStatus, sObject.result.status as actualStatus,
    expectedResult.expectedResult.conditionIndex as expectedConditionIndex, sObject.result.conditionIndex as actualConditionIndex,
    expectedResult.expectedResult.conditionText as expectedConditionText, sObject.result.conditionText as actualConditionText,
    expectedResult.expectedResult.runtimeError as expectedRuntimeError, sObject.result.runtimeError as actualRuntimeError
  FROM UNNEST(mock_ExpectedResult()) expectedResult
  LEFT JOIN (
      SELECT
          sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__tableStatus__c AS CA10__tableStatus__c,
          sObject.CA10__creationDateTime__c AS CA10__creationDateTime__c,
          sObject.CA10__itemCount__c AS CA10__itemCount__c,
          sObject.Id AS Id,
          process_CA10__CaAwsDynamoDbTable__c(
              STRUCT(
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__tableStatus__c AS CA10__tableStatus__c,
                  sObject.CA10__creationDateTime__c AS CA10__creationDateTime__c,
                  sObject.CA10__itemCount__c AS CA10__itemCount__c,
                  sObject.Id AS Id
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsDynamoDbTable__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
