---
policy: /ce/ca/aws/autoscaling/group-capacity-rebalancing
logic: /ce/ca/aws/autoscaling/group-capacity-rebalancing/prod.logic.yaml
executionTime: 2025-10-18T12:01:27.228151743Z
generationMs: 112
executionMs: 1096
rows:
  - id: test1
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10__disappearanceTime__c)
      actual: isDisappeared(CA10__disappearanceTime__c)
    runtimeError: {}
  - id: test2
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10A1__mixedInstancesLaunchTemplateId__c').isEmpty()
      actual: extract('CA10A1__mixedInstancesLaunchTemplateId__c').isEmpty()
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__capacityRebalancing__c') != 'Enable'
      actual: extract('CA10__capacityRebalancing__c') != 'Enable'
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test5
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__capacityRebalancing__c') != 'Enable'
      actual: extract('CA10__capacityRebalancing__c') != 'Enable'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/autoscaling/group-capacity-rebalancing/policy.yaml
    md5Hash: A4B285519365021E70F9E7DDFD716F43
    content: "---\nnames:\n  full: \"AWS EC2 Auto Scaling Group Capacity Rebalancing\
      \ is not enabled\"\n  contextual: \"Auto Scaling Group Capacity Rebalancing\
      \ is not enabled\"\ndescription: >\n  Ensure that Capacity Rebalancing is enabled\
      \ for AWS EC2 Auto Scaling Groups \n  that use a mixed instances launch template.\
      \ This feature helps improve application \n  availability by proactively launching\
      \ a replacement Spot Instance when an existing \n  one receives a rebalance\
      \ recommendation, which is a signal that the instance is \n  at elevated risk\
      \ of interruption.\ncategories:\n  - \"RELIABILITY\"\ntype: \"COMPLIANCE_POLICY\"\
      \nframeworkMappings:\n  - \"/frameworks/cloudaware/resource-reliability/system-configuration\"\
      \n  - \"/frameworks/aws-well-architected/perf/04/04\"\n  - \"/frameworks/aws-well-architected/rel/06/04\"\
      \n  - \"/frameworks/aws-well-architected/rel/07/02\"\n  - \"/frameworks/aws-well-architected/rel/11/03\"\
      \n  - \"/frameworks/aws-well-architected/rel/13/05\"\n  - \"/frameworks/aws-well-architected/sus/02/01\""
  - path: /ce/ca/aws/autoscaling/group-capacity-rebalancing/prod.logic.yaml
    md5Hash: B49D048794CC9D2D62346F720950C305
    content: "---\ninputType: \"CA10__CaAwsAutoScalingGroup__c\"\nimportExtracts:\n\
      \  - file: \"/types/CA10__CaAwsAutoScalingGroup__c/object.extracts.yaml\"\n\
      testData:\n  - file: \"test-data.json\"  \nconditions:\n  - status: \"INAPPLICABLE\"\
      \n    currentStateMessage: \"Capacity Rebalancing only applies to Auto Scaling\
      \ groups\\\n      \\ with a mixed instances launch template.\"\n    check:\n\
      \      IS_EMPTY:\n        arg:\n          EXTRACT: \"CA10A1__mixedInstancesLaunchTemplateId__c\"\
      \n  - status: \"INCOMPLIANT\"\n    currentStateMessage: \"Capacity Rebalancing\
      \ is not enabled for this Auto Scaling group.\"\n    remediationMessage: \"\
      Enable Capacity Rebalancing for the Auto Scaling group to proactively manage\
      \ Spot Instances.\"\n    check:\n      NOT_EQUAL:\n        left:\n         \
      \ EXTRACT: \"CA10__capacityRebalancing__c\"\n        right:\n          TEXT:\
      \ \"Enable\"\notherwise:\n  status: \"COMPLIANT\"\n  currentStateMessage: \"\
      Capacity Rebalancing is enabled for this Auto Scaling group.\""
  - path: /ce/ca/aws/autoscaling/group-capacity-rebalancing/test-data.json
    md5Hash: 1BABDDCA59043CECC1E09A6EA0F9B107
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-13T02:23:14Z"
              },
              "CA10__disappearanceTime__c": "2025-07-29T11:31:15Z",
              "CA10A1__mixedInstancesLaunchTemplateId__c": "",
              "CA10__capacityRebalancing__c": "",
              "Id": "test1"
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10A1__mixedInstancesLaunchTemplateId__c').isEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-13T02:23:14Z"
              },
              "CA10__disappearanceTime__c": null,
              "CA10A1__mixedInstancesLaunchTemplateId__c": "",
              "CA10__capacityRebalancing__c": "",
              "Id": "test2"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__capacityRebalancing__c') != 'Enable'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-13T02:23:14Z"
              },
              "CA10__disappearanceTime__c": null,
              "CA10A1__mixedInstancesLaunchTemplateId__c": "mixedInstancesLaunchTemplateId",
              "CA10__capacityRebalancing__c": "",
              "Id": "test3"
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "300",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-13T02:23:14Z"
              },
              "CA10__disappearanceTime__c": null,
              "CA10A1__mixedInstancesLaunchTemplateId__c": "mixedInstancesLaunchTemplateId",
              "CA10__capacityRebalancing__c": "Enable",
              "Id": "test4"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__capacityRebalancing__c') != 'Enable'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-13T02:23:14Z"
              },
              "CA10__disappearanceTime__c": null,
              "CA10A1__mixedInstancesLaunchTemplateId__c": "mixedInstancesLaunchTemplateId",
              "CA10__capacityRebalancing__c": "Disable",
              "Id": "test5"
          }
      ]
  - path: /types/CA10__CaAwsAutoScalingGroup__c/object.extracts.yaml
    md5Hash: DD26D73C0CF43F8CE9CBE35D8241D89C
    content: "---\nextracts:\n  # AvailabilityZones.member.N\n  # One or more Availability\
      \ Zones for the group.\n  # Type: Array of strings\n  # Length Constraints:\
      \ Minimum length of 1. Maximum length of 255.\n  # Pattern: [\\u0020-\\uD7FF\\\
      uE000-\\uFFFD\\uD800\\uDC00-\\uDBFF\\uDFFF\\r\\n\\t]*\n  # Required: Yes\n \
      \ - name: CA10__availabilityZones__c\n    value: \n      FIELD:\n        path:\
      \ CA10__availabilityZones__c\n        undeterminedIf:\n          isEmpty: Corrupted\
      \ data. Availability zones should always present\n  - name: caSetFrom_availabilityZones__c\n\
      \    value: \n      SET_FROM:\n        separator: \"\\n\"\n        arg:\n  \
      \        EXTRACT: CA10__availabilityZones__c\n# A comma-separated value string\
      \ of one or more health check types.\n# Values: EC2, EBS, ELB, VPC_LATTICE.\
      \  Not nullable. Can't have no access, retrieved via autoscaling:DescribeAutoScalingGroups\n\
      \  - name: CA10__healthCheckType__c\n    value: \n      FIELD:\n        path:\
      \ CA10__healthCheckType__c     \n# Nullable. Can't have no access, retrieved\
      \ via autoscaling:DescribeAutoScalingGroups     \n  - name: CA10__launchConfigurationName__c\n\
      \    value: \n      FIELD:\n        path: CA10__launchConfigurationName__c \
      \         \n# Nullable. Can't have no access, retrieved via autoscaling:DescribeAutoScalingGroups\
      \     \n  - name: CA10A1__launchTemplateId__c\n    value: \n      FIELD:\n \
      \       path: CA10A1__launchTemplateId__c          \n# Nullable. Can't have\
      \ no access, retrieved via autoscaling:DescribeAutoScalingGroups     \n  - name:\
      \ CA10A1__mixedInstancesLaunchTemplateId__c\n    value: \n      FIELD:\n   \
      \     path: CA10A1__mixedInstancesLaunchTemplateId__c          \n# Values: Enable\
      \ | Disable. Nullable, null == Disable. Can't have no access, retrieved via\
      \ autoscaling:DescribeAutoScalingGroups     \n  - name: CA10__capacityRebalancing__c\n\
      \    value: \n      FIELD:\n        path: CA10__capacityRebalancing__c     \
      \     \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" : "test1",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10A1__mixedInstancesLaunchTemplateId__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__capacityRebalancing__c') != 'Enable'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test5",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__capacityRebalancing__c') != 'Enable'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsAutoScalingGroup__c()
  RETURNS ARRAY<STRUCT<
      CA10__disappearanceTime__c TIMESTAMP,
      CA10A1__mixedInstancesLaunchTemplateId__c STRING,
      CA10__capacityRebalancing__c STRING,
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-10-13T02:23:14Z")
    },
    "CA10__disappearanceTime__c" : new Date("2025-07-29T11:31:15Z"),
    "CA10A1__mixedInstancesLaunchTemplateId__c" : "",
    "CA10__capacityRebalancing__c" : "",
    "Id" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-13T02:23:14Z")
    },
    "CA10A1__mixedInstancesLaunchTemplateId__c" : "",
    "CA10__capacityRebalancing__c" : "",
    "Id" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-13T02:23:14Z")
    },
    "CA10A1__mixedInstancesLaunchTemplateId__c" : "mixedInstancesLaunchTemplateId",
    "CA10__capacityRebalancing__c" : "",
    "Id" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-13T02:23:14Z")
    },
    "CA10A1__mixedInstancesLaunchTemplateId__c" : "mixedInstancesLaunchTemplateId",
    "CA10__capacityRebalancing__c" : "Enable",
    "Id" : "test4"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-13T02:23:14Z")
    },
    "CA10A1__mixedInstancesLaunchTemplateId__c" : "mixedInstancesLaunchTemplateId",
    "CA10__capacityRebalancing__c" : "Disable",
    "Id" : "test5"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsAutoScalingGroup__c(
      obj STRUCT<
          CA10__disappearanceTime__c TIMESTAMP,
          CA10A1__mixedInstancesLaunchTemplateId__c STRING,
          CA10__capacityRebalancing__c STRING,
          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 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.CA10A1__mixedInstancesLaunchTemplateId__c; } return this.out; };
      references1.push('Mixed Instances Launch Template ID [obj.CA10A1__mixedInstancesLaunchTemplateId__c]: ' + obj.CA10A1__mixedInstancesLaunchTemplateId__c);
      if (TextLib.isEmpty(extract3.call(extract3))) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10A1__mixedInstancesLaunchTemplateId__c').isEmpty()", currentStateMessage: "Capacity Rebalancing only applies to Auto Scaling groups with a mixed instances launch template.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__capacityRebalancing__c; } return this.out; };
      references1.push('Capacity Rebalancing [obj.CA10__capacityRebalancing__c]: ' + obj.CA10__capacityRebalancing__c);
      if (TextLib.notEqual(extract6.call(extract6), 'Enable')) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__capacityRebalancing__c') != 'Enable'", currentStateMessage: "Capacity Rebalancing is not enabled for this Auto Scaling group.", currentStateReferences: references1.join('\n'), remediation: "Enable Capacity Rebalancing for the Auto Scaling group to proactively manage Spot Instances.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "Capacity Rebalancing is enabled for this Auto Scaling group.", 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.CA10A1__mixedInstancesLaunchTemplateId__c AS CA10A1__mixedInstancesLaunchTemplateId__c,
          sObject.CA10__capacityRebalancing__c AS CA10__capacityRebalancing__c,
          sObject.Id AS Id,
          process_CA10__CaAwsAutoScalingGroup__c(
              STRUCT(
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10A1__mixedInstancesLaunchTemplateId__c AS CA10A1__mixedInstancesLaunchTemplateId__c,
                  sObject.CA10__capacityRebalancing__c AS CA10__capacityRebalancing__c,
                  sObject.Id AS Id
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsAutoScalingGroup__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
