Skip to content

Autoscale Abstractions

cloudspells.core.abstractions.autoscale

Cloud-neutral autoscaling abstractions for CloudSpells multi-cloud support.

Defines the scaling policy dataclasses and the scalable workload interface shared across all providers. Scaling concepts—CPU/memory thresholds and cron-schedule-based rules—are cloud-neutral and map directly to OCI Autoscaling Configurations, AWS Auto Scaling Groups, and GCP Managed Instance Group autoscalers.

The base LoadBalancerConfig omits provider-specific bandwidth fields (e.g. OCI flexible-shape Mbps). Provider implementations extend it with their own specialised subclass (e.g. OciLoadBalancerConfig).

Exports

ScalingMetric: Enum of supported autoscaling metric types. ScalingAction: Enum of scaling action kinds. MetricScalingPolicy: CPU/memory threshold-based scaling configuration. ScheduleEntry: A single cron-schedule scaling action. ScheduleScalingPolicy: Schedule-based scaling configuration. LoadBalancerConfig: Base cloud-neutral load balancer configuration. AbstractScalableWorkload: Interface for a horizontally-scalable compute tier.

ScalingMetric

Bases: Enum

Metrics available for autoscaling policies.

Attributes:

Name Type Description
CPU_UTILIZATION

Scale based on average CPU utilisation (percent).

MEMORY_UTILIZATION

Scale based on average memory utilisation (percent).

Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
32
33
34
35
36
37
38
39
40
41
class ScalingMetric(Enum):
    """Metrics available for autoscaling policies.

    Attributes:
        CPU_UTILIZATION: Scale based on average CPU utilisation (percent).
        MEMORY_UTILIZATION: Scale based on average memory utilisation (percent).
    """

    CPU_UTILIZATION = "CPU_UTILIZATION"
    MEMORY_UTILIZATION = "MEMORY_UTILIZATION"

ScalingAction

Bases: Enum

Action types for scheduled scaling policies.

Attributes:

Name Type Description
CHANGE_COUNT_BY

Change the instance count by a relative delta (e.g. +2 or -1).

CHANGE_COUNT_TO

Set the instance count to an absolute target value.

Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
44
45
46
47
48
49
50
51
52
53
54
class ScalingAction(Enum):
    """Action types for scheduled scaling policies.

    Attributes:
        CHANGE_COUNT_BY: Change the instance count by a relative delta
            (e.g. `+2` or `-1`).
        CHANGE_COUNT_TO: Set the instance count to an absolute target value.
    """

    CHANGE_COUNT_BY = "CHANGE_COUNT_BY"
    CHANGE_COUNT_TO = "CHANGE_COUNT_TO"

MetricScalingPolicy dataclass

Metric-based autoscaling policy configuration.

Triggers scale-out when scale_out_threshold is exceeded and scale-in when the metric falls below scale_in_threshold. Maps to OCI threshold autoscaling, AWS target tracking / step scaling, or GCP autoscaling policies.

Attributes:

Name Type Description
scale_out_threshold int

Percentage threshold to trigger scale-out (add instances). Default: 80.

scale_in_threshold int

Percentage threshold to trigger scale-in (remove instances). Default: 20.

scale_out_value int

Number of instances to add when scaling out. Default: 1.

scale_in_value int

Number of instances to remove when scaling in (negative value). Default: -1.

cooldown_in_seconds int

Minimum time between consecutive scaling actions (300-3600 s). Default: 300.

metric ScalingMetric

The metric to monitor. Default: ScalingMetric.CPU_UTILIZATION.

Example
policy = MetricScalingPolicy(
    scale_out_threshold=70,
    scale_in_threshold=25,
    cooldown_in_seconds=600,
)
Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
@dataclass
class MetricScalingPolicy:
    """Metric-based autoscaling policy configuration.

    Triggers scale-out when *scale_out_threshold* is exceeded and scale-in
    when the metric falls below *scale_in_threshold*.  Maps to OCI threshold
    autoscaling, AWS target tracking / step scaling, or GCP autoscaling
    policies.

    Attributes:
        scale_out_threshold: Percentage threshold to trigger scale-out
            (add instances).  Default: `80`.
        scale_in_threshold: Percentage threshold to trigger scale-in
            (remove instances).  Default: `20`.
        scale_out_value: Number of instances to add when scaling out.
            Default: `1`.
        scale_in_value: Number of instances to remove when scaling in
            (negative value).  Default: `-1`.
        cooldown_in_seconds: Minimum time between consecutive scaling
            actions (300-3600 s).  Default: `300`.
        metric: The metric to monitor.  Default:
            `ScalingMetric.CPU_UTILIZATION`.

    Example:
        ```python
        policy = MetricScalingPolicy(
            scale_out_threshold=70,
            scale_in_threshold=25,
            cooldown_in_seconds=600,
        )
        ```
    """

    scale_out_threshold: int = 80
    scale_in_threshold: int = 20
    scale_out_value: int = 1
    scale_in_value: int = -1
    cooldown_in_seconds: int = 300
    metric: ScalingMetric = ScalingMetric.CPU_UTILIZATION

ScheduleEntry dataclass

A single scheduled scaling action.

Attributes:

Name Type Description
cron_expression str

Quartz cron format expression in UTC (e.g. "0 0 9 ? * MON-FRI *").

action ScalingAction

Whether to change the count by a delta or to an absolute value.

value int

Magnitude of the scaling action.

display_name str

Human-readable label for this schedule entry.

Example
scale_up = ScheduleEntry(
    cron_expression="0 0 8 ? * MON-FRI *",
    action=ScalingAction.CHANGE_COUNT_TO,
    value=10,
    display_name="Business-hours scale-up",
)
Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@dataclass
class ScheduleEntry:
    """A single scheduled scaling action.

    Attributes:
        cron_expression: Quartz cron format expression in UTC
            (e.g. `"0 0 9 ? * MON-FRI *"`).
        action: Whether to change the count by a delta or to an absolute
            value.
        value: Magnitude of the scaling action.
        display_name: Human-readable label for this schedule entry.

    Example:
        ```python
        scale_up = ScheduleEntry(
            cron_expression="0 0 8 ? * MON-FRI *",
            action=ScalingAction.CHANGE_COUNT_TO,
            value=10,
            display_name="Business-hours scale-up",
        )
        ```
    """

    cron_expression: str
    action: ScalingAction
    value: int
    display_name: str

ScheduleScalingPolicy dataclass

Schedule-based autoscaling policy configuration.

Attributes:

Name Type Description
schedules list[ScheduleEntry]

Ordered list of ScheduleEntry objects (maximum 50 per policy on most providers).

Example
policy = ScheduleScalingPolicy(
    schedules=[
        ScheduleEntry("0 0 8 ? * MON-FRI *",
                      ScalingAction.CHANGE_COUNT_TO, 10,
                      "Scale up business hours"),
        ScheduleEntry("0 0 20 ? * MON-FRI *",
                      ScalingAction.CHANGE_COUNT_TO, 2,
                      "Scale down after hours"),
    ]
)
Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
@dataclass
class ScheduleScalingPolicy:
    """Schedule-based autoscaling policy configuration.

    Attributes:
        schedules: Ordered list of `ScheduleEntry` objects
            (maximum 50 per policy on most providers).

    Example:
        ```python
        policy = ScheduleScalingPolicy(
            schedules=[
                ScheduleEntry("0 0 8 ? * MON-FRI *",
                              ScalingAction.CHANGE_COUNT_TO, 10,
                              "Scale up business hours"),
                ScheduleEntry("0 0 20 ? * MON-FRI *",
                              ScalingAction.CHANGE_COUNT_TO, 2,
                              "Scale down after hours"),
            ]
        )
        ```
    """

    schedules: list[ScheduleEntry] = field(default_factory=list)

LoadBalancerConfig dataclass

Base cloud-neutral load balancer configuration.

Covers the properties that are meaningful on every major cloud provider. Provider-specific subclasses add extra fields; for example OciLoadBalancerConfig adds min_bandwidth_mbps and max_bandwidth_mbps for OCI's flexible load-balancer shape.

Attributes:

Name Type Description
backend_port int

Port on backend instances to receive forwarded traffic and health-check probes. Default: 80.

health_check_path str

HTTP path for health checks. Default: "/health".

is_public bool

Whether the load balancer should have a public IP. Default: True.

ssl_certificate_name str | None

Name of an SSL certificate for HTTPS termination. When set, an HTTPS listener on port 443 is created in addition to HTTP on port 80. Default: None (HTTP only).

Example
lb_cfg = LoadBalancerConfig(
    backend_port=8080,
    health_check_path="/api/health",
)
Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
@dataclass
class LoadBalancerConfig:
    """Base cloud-neutral load balancer configuration.

    Covers the properties that are meaningful on every major cloud
    provider.  Provider-specific subclasses add extra fields; for example
    `OciLoadBalancerConfig` adds `min_bandwidth_mbps` and `max_bandwidth_mbps`
    for OCI's flexible load-balancer shape.

    Attributes:
        backend_port: Port on backend instances to receive forwarded
            traffic and health-check probes.  Default: `80`.
        health_check_path: HTTP path for health checks.
            Default: `"/health"`.
        is_public: Whether the load balancer should have a public IP.
            Default: `True`.
        ssl_certificate_name: Name of an SSL certificate for HTTPS
            termination.  When set, an HTTPS listener on port 443 is
            created in addition to HTTP on port 80.
            Default: `None` (HTTP only).

    Example:
        ```python
        lb_cfg = LoadBalancerConfig(
            backend_port=8080,
            health_check_path="/api/health",
        )
        ```
    """

    backend_port: int = 80
    health_check_path: str = "/health"
    is_public: bool = True
    ssl_certificate_name: str | None = None

AbstractScalableWorkload

Bases: ABC

Interface for a horizontally-scalable compute tier.

Provider implementations (OCI ScalableWorkload, AWS AwsScalableWorkload, GCP GcpScalableWorkload) combine a load balancer, instance pool / auto scaling group, and autoscaling configuration behind this interface.

Attributes:

Name Type Description
id Output[str]

Provider resource ID of the instance pool / ASG.

auto_generated_keys bool

True when SSH keys were auto-generated.

Example
def export_workload(wl: AbstractScalableWorkload,
                    label: str) -> None:
    pulumi.export(f"{label}_lb_ip", wl.get_load_balancer_ip())
    pulumi.export(f"{label}_pool_id", wl.get_instance_pool_id())

export_workload(oci_pool, "web")
Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
class AbstractScalableWorkload(ABC):
    """Interface for a horizontally-scalable compute tier.

    Provider implementations (OCI `ScalableWorkload`, AWS
    `AwsScalableWorkload`, GCP `GcpScalableWorkload`) combine a load
    balancer, instance pool / auto scaling group, and autoscaling
    configuration behind this interface.

    Attributes:
        id: Provider resource ID of the instance pool / ASG.
        auto_generated_keys: `True` when SSH keys were auto-generated.

    Example:
        ```python
        def export_workload(wl: AbstractScalableWorkload,
                            label: str) -> None:
            pulumi.export(f"{label}_lb_ip", wl.get_load_balancer_ip())
            pulumi.export(f"{label}_pool_id", wl.get_instance_pool_id())

        export_workload(oci_pool, "web")
        ```
    """

    id: pulumi.Output[str]
    auto_generated_keys: bool

    @abstractmethod
    def get_load_balancer_ip(self) -> pulumi.Output[str]:
        """Return the public IP of the load balancer.

        Returns:
            `pulumi.Output[str]` resolving to the load balancer IP.
        """

    @abstractmethod
    def get_instance_pool_id(self) -> pulumi.Output[str]:
        """Return the provider resource ID of the instance pool.

        Returns:
            `pulumi.Output[str]` resolving to the pool resource ID.
        """

    @abstractmethod
    def get_load_balancer_id(self) -> pulumi.Output[str]:
        """Return the provider resource ID of the load balancer.

        Returns:
            `pulumi.Output[str]` resolving to the load balancer ID.
        """

    @abstractmethod
    def export(self) -> None:
        """Publish standard scalable workload stack outputs."""

get_load_balancer_ip() -> pulumi.Output[str] abstractmethod

Return the public IP of the load balancer.

Returns:

Type Description
Output[str]

pulumi.Output[str] resolving to the load balancer IP.

Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
215
216
217
218
219
220
221
@abstractmethod
def get_load_balancer_ip(self) -> pulumi.Output[str]:
    """Return the public IP of the load balancer.

    Returns:
        `pulumi.Output[str]` resolving to the load balancer IP.
    """

get_instance_pool_id() -> pulumi.Output[str] abstractmethod

Return the provider resource ID of the instance pool.

Returns:

Type Description
Output[str]

pulumi.Output[str] resolving to the pool resource ID.

Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
223
224
225
226
227
228
229
@abstractmethod
def get_instance_pool_id(self) -> pulumi.Output[str]:
    """Return the provider resource ID of the instance pool.

    Returns:
        `pulumi.Output[str]` resolving to the pool resource ID.
    """

get_load_balancer_id() -> pulumi.Output[str] abstractmethod

Return the provider resource ID of the load balancer.

Returns:

Type Description
Output[str]

pulumi.Output[str] resolving to the load balancer ID.

Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
231
232
233
234
235
236
237
@abstractmethod
def get_load_balancer_id(self) -> pulumi.Output[str]:
    """Return the provider resource ID of the load balancer.

    Returns:
        `pulumi.Output[str]` resolving to the load balancer ID.
    """

export() -> None abstractmethod

Publish standard scalable workload stack outputs.

Source code in packages/cloudspells-core/src/cloudspells/core/abstractions/autoscale.py
239
240
241
@abstractmethod
def export(self) -> None:
    """Publish standard scalable workload stack outputs."""