0
# Autoscaling
1
2
Automated resource scaling based on metrics, schedules, and predictive policies. Manages autoscale settings that automatically adjust resource capacity to meet demand while optimizing costs and performance.
3
4
## Capabilities
5
6
### Autoscale Settings Management
7
8
Create, update, and manage autoscale settings with support for metric-based and schedule-based scaling rules.
9
10
```python { .api }
11
def create_or_update(resource_group_name: str, autoscale_setting_name: str, parameters: AutoscaleSettingResource, **kwargs: Any) -> AutoscaleSettingResource:
12
"""
13
Creates or updates an autoscale setting.
14
15
Parameters:
16
- resource_group_name: str - Name of the resource group
17
- autoscale_setting_name: str - Name of the autoscale setting
18
- parameters: AutoscaleSettingResource - Autoscale configuration
19
20
Returns:
21
AutoscaleSettingResource - The created or updated autoscale setting
22
"""
23
24
def get(resource_group_name: str, autoscale_setting_name: str, **kwargs: Any) -> AutoscaleSettingResource:
25
"""
26
Gets an autoscale setting.
27
28
Parameters:
29
- resource_group_name: str - Name of the resource group
30
- autoscale_setting_name: str - Name of the autoscale setting
31
32
Returns:
33
AutoscaleSettingResource - The autoscale setting details
34
"""
35
36
def delete(resource_group_name: str, autoscale_setting_name: str, **kwargs: Any) -> None:
37
"""
38
Deletes an autoscale setting.
39
40
Parameters:
41
- resource_group_name: str - Name of the resource group
42
- autoscale_setting_name: str - Name of the autoscale setting
43
"""
44
45
def update(resource_group_name: str, autoscale_setting_name: str, autoscale_setting_resource: AutoscaleSettingResourcePatch, **kwargs: Any) -> AutoscaleSettingResource:
46
"""
47
Updates an autoscale setting.
48
49
Parameters:
50
- resource_group_name: str - Name of the resource group
51
- autoscale_setting_name: str - Name of the autoscale setting
52
- autoscale_setting_resource: AutoscaleSettingResourcePatch - Properties to update
53
54
Returns:
55
AutoscaleSettingResource - The updated autoscale setting
56
"""
57
```
58
59
### Autoscale Settings Listing
60
61
List autoscale settings within subscriptions and resource groups.
62
63
```python { .api }
64
def list_by_subscription(**kwargs: Any) -> ItemPaged[AutoscaleSettingResource]:
65
"""
66
Lists the autoscale settings for a subscription.
67
68
Returns:
69
ItemPaged[AutoscaleSettingResource] - Paginated list of autoscale settings
70
"""
71
72
def list_by_resource_group(resource_group_name: str, **kwargs: Any) -> ItemPaged[AutoscaleSettingResource]:
73
"""
74
Lists the autoscale settings for a resource group.
75
76
Parameters:
77
- resource_group_name: str - Name of the resource group
78
79
Returns:
80
ItemPaged[AutoscaleSettingResource] - Paginated list of autoscale settings
81
"""
82
```
83
84
### Predictive Metrics
85
86
Access predictive autoscale metric data for forecast-based scaling decisions.
87
88
```python { .api }
89
def get(resource_group_name: str, autoscale_setting_name: str, timespan: str, interval: str, metricnamespace: str, metricname: str, aggregation: str, **kwargs: Any) -> PredictiveResponse:
90
"""
91
Get predictive autoscale metric future data.
92
93
Parameters:
94
- resource_group_name: str - Name of the resource group
95
- autoscale_setting_name: str - Name of the autoscale setting
96
- timespan: str - Time range for prediction (ISO 8601)
97
- interval: str - Prediction interval (ISO 8601)
98
- metricnamespace: str - Metric namespace
99
- metricname: str - Metric name
100
- aggregation: str - Aggregation type
101
102
Returns:
103
PredictiveResponse - Predictive metric data
104
"""
105
```
106
107
## Usage Examples
108
109
### Creating Metric-Based Autoscale Setting
110
111
```python
112
from azure.mgmt.monitor.models import (
113
AutoscaleSettingResource, AutoscaleProfile, ScaleRule,
114
MetricTrigger, ScaleAction, ScaleCapacity, TimeWindow
115
)
116
117
# Define metric trigger for scale out
118
scale_out_trigger = MetricTrigger(
119
metric_name="Percentage CPU",
120
metric_namespace="Microsoft.Compute/virtualMachineScaleSets",
121
metric_resource_uri=f"/subscriptions/{subscription_id}/resourceGroups/web-rg/providers/Microsoft.Compute/virtualMachineScaleSets/web-vmss",
122
time_grain="PT1M",
123
statistic="Average",
124
time_window="PT5M",
125
time_aggregation="Average",
126
operator="GreaterThan",
127
threshold=75.0
128
)
129
130
# Define scale out action
131
scale_out_action = ScaleAction(
132
direction="Increase",
133
type="ChangeCount",
134
value="1",
135
cooldown="PT5M" # 5-minute cooldown
136
)
137
138
# Define scale in rule
139
scale_in_trigger = MetricTrigger(
140
metric_name="Percentage CPU",
141
metric_namespace="Microsoft.Compute/virtualMachineScaleSets",
142
metric_resource_uri=f"/subscriptions/{subscription_id}/resourceGroups/web-rg/providers/Microsoft.Compute/virtualMachineScaleSets/web-vmss",
143
time_grain="PT1M",
144
statistic="Average",
145
time_window="PT5M",
146
time_aggregation="Average",
147
operator="LessThan",
148
threshold=25.0
149
)
150
151
scale_in_action = ScaleAction(
152
direction="Decrease",
153
type="ChangeCount",
154
value="1",
155
cooldown="PT5M"
156
)
157
158
# Create scale rules
159
rules = [
160
ScaleRule(metric_trigger=scale_out_trigger, scale_action=scale_out_action),
161
ScaleRule(metric_trigger=scale_in_trigger, scale_action=scale_in_action)
162
]
163
164
# Define capacity limits
165
capacity = ScaleCapacity(
166
minimum="2", # Minimum 2 instances
167
maximum="10", # Maximum 10 instances
168
default="2" # Default 2 instances
169
)
170
171
# Create autoscale profile
172
profile = AutoscaleProfile(
173
name="Default",
174
capacity=capacity,
175
rules=rules
176
)
177
178
# Create autoscale setting
179
autoscale_setting = AutoscaleSettingResource(
180
location="East US",
181
profiles=[profile],
182
enabled=True,
183
target_resource_uri=f"/subscriptions/{subscription_id}/resourceGroups/web-rg/providers/Microsoft.Compute/virtualMachineScaleSets/web-vmss",
184
target_resource_location="East US"
185
)
186
187
result = client.autoscale_settings.create_or_update(
188
resource_group_name="web-rg",
189
autoscale_setting_name="web-vmss-autoscale",
190
parameters=autoscale_setting
191
)
192
```
193
194
### Schedule-Based Autoscaling
195
196
```python
197
from azure.mgmt.monitor.models import (
198
Recurrence, RecurrentSchedule, RecurrenceFrequency
199
)
200
201
# Create schedule for business hours (scale up)
202
business_hours_schedule = RecurrentSchedule(
203
time_zone="Pacific Standard Time",
204
days=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
205
hours=[8], # 8 AM
206
minutes=[0]
207
)
208
209
business_hours_recurrence = Recurrence(
210
frequency=RecurrenceFrequency.WEEK,
211
schedule=business_hours_schedule
212
)
213
214
# Business hours profile (higher capacity)
215
business_profile = AutoscaleProfile(
216
name="BusinessHours",
217
capacity=ScaleCapacity(minimum="5", maximum="20", default="10"),
218
rules=[], # No metric rules, just scheduled scaling
219
recurrence=business_hours_recurrence
220
)
221
222
# Create schedule for off hours (scale down)
223
off_hours_schedule = RecurrentSchedule(
224
time_zone="Pacific Standard Time",
225
days=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
226
hours=[18], # 6 PM
227
minutes=[0]
228
)
229
230
off_hours_recurrence = Recurrence(
231
frequency=RecurrenceFrequency.WEEK,
232
schedule=off_hours_schedule
233
)
234
235
# Off hours profile (lower capacity)
236
off_hours_profile = AutoscaleProfile(
237
name="OffHours",
238
capacity=ScaleCapacity(minimum="2", maximum="5", default="2"),
239
rules=[],
240
recurrence=off_hours_recurrence
241
)
242
```
243
244
### Predictive Autoscaling
245
246
```python
247
from azure.mgmt.monitor.models import PredictiveAutoscalePolicy
248
249
# Enable predictive autoscaling
250
predictive_policy = PredictiveAutoscalePolicy(
251
scale_mode="Enabled", # Enable predictive scaling
252
scale_look_ahead_time="PT30M" # Look ahead 30 minutes
253
)
254
255
# Add predictive policy to profile
256
profile_with_prediction = AutoscaleProfile(
257
name="PredictiveProfile",
258
capacity=capacity,
259
rules=rules,
260
predictive_autoscale_policy=predictive_policy
261
)
262
263
# Get predictive metrics
264
predictive_data = client.predictive_metric.get(
265
resource_group_name="web-rg",
266
autoscale_setting_name="web-vmss-autoscale",
267
timespan="2024-01-01T00:00:00Z/2024-01-01T12:00:00Z",
268
interval="PT5M",
269
metricnamespace="Microsoft.Compute/virtualMachineScaleSets",
270
metricname="Percentage CPU",
271
aggregation="Average"
272
)
273
274
print(f"Predicted values: {predictive_data.data}")
275
```
276
277
## Types
278
279
```python { .api }
280
class AutoscaleSettingResource:
281
"""Autoscale setting resource."""
282
location: str # Resource location
283
profiles: List[AutoscaleProfile] # Autoscale profiles
284
enabled: Optional[bool] # Whether autoscaling is enabled
285
target_resource_uri: Optional[str] # Target resource to scale
286
target_resource_location: Optional[str] # Target resource location
287
notifications: Optional[List[AutoscaleNotification]] # Notifications
288
289
class AutoscaleProfile:
290
"""Autoscale profile with scaling rules."""
291
name: str # Profile name
292
capacity: ScaleCapacity # Scaling capacity limits
293
rules: List[ScaleRule] # Scaling rules
294
fixed_date: Optional[TimeWindow] # Fixed date profile
295
recurrence: Optional[Recurrence] # Recurring schedule
296
predictive_autoscale_policy: Optional[PredictiveAutoscalePolicy] # Predictive scaling
297
298
class ScaleCapacity:
299
"""Scaling capacity configuration."""
300
minimum: str # Minimum instance count
301
maximum: str # Maximum instance count
302
default: str # Default instance count
303
304
class ScaleRule:
305
"""Autoscale rule definition."""
306
metric_trigger: MetricTrigger # Metric that triggers scaling
307
scale_action: ScaleAction # Action to take when triggered
308
309
class MetricTrigger:
310
"""Metric trigger for autoscaling."""
311
metric_name: str # Metric name
312
metric_namespace: str # Metric namespace
313
metric_resource_uri: str # Resource to monitor
314
time_grain: str # Metric collection frequency
315
statistic: MetricStatisticType # Statistic calculation method
316
time_window: str # Time window for evaluation
317
time_aggregation: TimeAggregationType # Time aggregation method
318
operator: ComparisonOperationType # Comparison operator
319
threshold: float # Threshold value
320
dimensions: Optional[List[ScaleRuleMetricDimension]] # Metric dimensions
321
divide_per_instance: Optional[bool] # Divide by instance count
322
323
class ScaleAction:
324
"""Action to perform when scaling."""
325
direction: ScaleDirection # Scale direction (Increase/Decrease)
326
type: ScaleType # Scale type (ChangeCount/PercentChangeCount/ExactCount)
327
value: str # Scale amount
328
cooldown: str # Cooldown period after scaling
329
330
class AutoscaleNotification:
331
"""Notification configuration."""
332
operation: str # Operation type
333
email: Optional[EmailNotification] # Email notification settings
334
webhooks: Optional[List[WebhookNotification]] # Webhook notifications
335
336
class PredictiveResponse:
337
"""Predictive autoscale response."""
338
timespan: Optional[str] # Time range
339
interval: Optional[str] # Prediction interval
340
metric_name: Optional[str] # Metric name
341
target_resource_id: Optional[str] # Target resource
342
data: Optional[List[PredictiveValue]] # Predicted values
343
344
class PredictiveValue:
345
"""Predicted metric value."""
346
time_stamp: Optional[datetime] # Prediction timestamp
347
value: Optional[float] # Predicted value
348
349
class PredictiveAutoscalePolicy:
350
"""Predictive autoscale configuration."""
351
scale_mode: PredictiveAutoscalePolicyScaleMode # Prediction mode
352
scale_look_ahead_time: Optional[str] # Look-ahead time
353
354
ScaleDirection = Union["None", "Increase", "Decrease"]
355
ScaleType = Union["ChangeCount", "PercentChangeCount", "ExactCount"]
356
MetricStatisticType = Union["Average", "Min", "Max", "Sum", "Count"]
357
TimeAggregationType = Union["Average", "Minimum", "Maximum", "Total", "Count", "Last"]
358
PredictiveAutoscalePolicyScaleMode = Union["Disabled", "ForecastOnly", "Enabled"]
359
```