

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 建立 Application Auto Scaling 的排程動作 AWS CLI
<a name="create-scheduled-actions"></a>

下列範例示範如何使用 AWS CLI [put-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 命令建立排程動作。指定新的容量時，可指定最低容量、最高容量或一併指定最低和最高容量。

這些範例針對與 Application Auto Scaling 整合的一些服務使用可擴展的目標。若要使用不同的可擴展目標，請在 中指定其命名空間`--service-namespace`、在 中指定其可擴展維度`--scalable-dimension`，以及在 中指定其資源 ID`--resource-id`。

使用 時 AWS CLI，請記住，您的命令會在為您的設定檔 AWS 區域 設定的 中執行。如果您想在不同區域中執行命令，則可變更設定檔的預設區域，或搭配 `--region` 參數使用命令。

**Topics**
+ [

## 建立只發生一次的排程動作
](#one-time-schedule)
+ [

## 建立依週期性間隔執行的排定動作
](#recurrence-schedule-rate)
+ [

## 建立依週期性排程執行的排程動作
](#recurrence-schedule-cron)
+ [

## 建立一次性排定動作並指定時區
](#one-time-schedule-set-time-zone)
+ [

## 建立指定時區的週期性排程動作
](#recurring-schedule-set-time-zone)

## 建立只發生一次的排程動作
<a name="one-time-schedule"></a>

若只要在指定的日期和時間自動擴展可擴展的目標一次，請使用 `--schedule "at(yyyy-mm-ddThh:mm:ss)"` 選項。

**Example 範例：僅擴增一次**  
以下是建立排定動作在特定日期和時間水平擴展容量的範例。  
在 `--schedule` 指定的日期和時間 (2021 年 3 月 31 日下午 10 點 UTC)，如果 `MinCapacity` 指定的值高於目前的容量，則 Application Auto Scaling 會水平擴展至 `MinCapacity`。  
**Linux、macOS 或 Unix**  

```
aws application-autoscaling put-scheduled-action --service-namespace custom-resource \
  --scalable-dimension custom-resource:ResourceType:Property \
  --resource-id file://~/custom-resource-id.txt \
  --scheduled-action-name scale-out \
  --schedule "at(2021-03-31T22:00:00)" \
  --scalable-target-action MinCapacity=3
```
**Windows**  

```
aws application-autoscaling put-scheduled-action --service-namespace custom-resource ^
  --scalable-dimension custom-resource:ResourceType:Property ^
  --resource-id file://~/custom-resource-id.txt ^
  --scheduled-action-name scale-out ^
  --schedule "at(2021-03-31T22:00:00)" ^
  --scalable-target-action MinCapacity=3
```
執行此排定的動作時，如果容量上限小於容量下限指定的值，則您必須指定新的容量上限和下限，而不只是容量下限。

**Example 範例：僅縮減一次**  
以下是建立排定動作在特定日期和時間縮減容量的範例。  
在 `--schedule` 指定的日期和時間 (2021 年 3 月 31 日下午 10 點 30 分 UTC)，如果 `MaxCapacity` 指定的值低於目前的容量，則 Application Auto Scaling 會縮減至 `MaxCapacity`。  
**Linux、macOS 或 Unix**  

```
aws application-autoscaling put-scheduled-action --service-namespace custom-resource \
  --scalable-dimension custom-resource:ResourceType:Property \
  --resource-id file://~/custom-resource-id.txt \
  --scheduled-action-name scale-in \
  --schedule "at(2021-03-31T22:30:00)" \
  --scalable-target-action MinCapacity=0,MaxCapacity=0
```
**Windows**  

```
aws application-autoscaling put-scheduled-action --service-namespace custom-resource ^
  --scalable-dimension custom-resource:ResourceType:Property ^
  --resource-id file://~/custom-resource-id.txt ^
  --scheduled-action-name scale-in ^
  --schedule "at(2021-03-31T22:30:00)" ^
  --scalable-target-action MinCapacity=0,MaxCapacity=0
```

## 建立依週期性間隔執行的排定動作
<a name="recurrence-schedule-rate"></a>

若要依據週期性間隔來執行排程擴展，請使用 `--schedule "rate(value unit)"` 選項。其值必須為正整數。單位可以是 `minute`、`minutes`、`hour`、`hours`、`day` 或 `days`。如需詳細資訊，請參閱《*Amazon EventBridge 使用者指南*》中的 [Rate 運算式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-scheduled-rule-pattern.html#eb-rate-expressions)。

以下是使用 Rate 表達式的排定動作範例。

按照指定的排程 (從 2021 年 1 月 30 日下午中午 12 點 UTC 開始至 2021 年 1 月 31 日下午 10 點 UTC 結束，每隔 5 小時)，如果 `MinCapacity` 指定的值高於目前的容量，則 Application Auto Scaling 會水平擴展至 `MinCapacity`。如果 `MaxCapacity` 指定的值低於目前的容量，則 Application Auto Scaling 會縮減至 `MaxCapacity`。

**Linux、macOS 或 Unix**

```
aws application-autoscaling put-scheduled-action --service-namespace ecs \
  --scalable-dimension ecs:service:DesiredCount \
  --resource-id service/my-cluster/my-service \
  --scheduled-action-name my-recurring-action \
  --schedule "rate(5 hours)" \
  --start-time 2021-01-30T12:00:00 \
  --end-time 2021-01-31T22:00:00 \
  --scalable-target-action MinCapacity=3,MaxCapacity=10
```

**Windows**

```
aws application-autoscaling put-scheduled-action --service-namespace ecs ^
  --scalable-dimension ecs:service:DesiredCount ^
  --resource-id service/my-cluster/my-service ^
  --scheduled-action-name my-recurring-action ^
  --schedule "rate(5 hours)" ^
  --start-time 2021-01-30T12:00:00 ^
  --end-time 2021-01-31T22:00:00 ^
  --scalable-target-action MinCapacity=3,MaxCapacity=10
```

## 建立依週期性排程執行的排程動作
<a name="recurrence-schedule-cron"></a>

若要依據週期性排程來執行排程擴展，請使用 `--schedule "cron(fields)"` 選項。如需詳細資訊，請參閱[使用 Application Auto Scaling 排程週期性擴展動作](scheduled-scaling-using-cron-expressions.md)。

以下是使用 Rate 表達式的排定動作範例。

按照指定的排程 (每天上午 9 點 UTC)，如果 `MinCapacity` 指定的值高於目前的容量，則 Application Auto Scaling 會水平擴展至 `MinCapacity`。如果 `MaxCapacity` 指定的值低於目前的容量，則 Application Auto Scaling 會縮減至 `MaxCapacity`。

**Linux、macOS 或 Unix**

```
aws application-autoscaling put-scheduled-action --service-namespace appstream \
  --scalable-dimension appstream:fleet:DesiredCapacity \
  --resource-id fleet/sample-fleet \
  --scheduled-action-name my-recurring-action \
  --schedule "cron(0 9 * * ? *)" \
  --scalable-target-action MinCapacity=10,MaxCapacity=50
```

**Windows**

```
aws application-autoscaling put-scheduled-action --service-namespace appstream ^
  --scalable-dimension appstream:fleet:DesiredCapacity ^
  --resource-id fleet/sample-fleet ^
  --scheduled-action-name my-recurring-action ^
  --schedule "cron(0 9 * * ? *)" ^
  --scalable-target-action MinCapacity=10,MaxCapacity=50
```

## 建立一次性排定動作並指定時區
<a name="one-time-schedule-set-time-zone"></a>

排定的動作預設為 UTC 時區。若要指定不同的時區，請包含 `--timezone` 選項，並指定時區的正式名稱 (例如 `America/New_York`)。如需詳細資訊，請參閱 [https://www.joda.org/joda-time/timezones.html](https://www.joda.org/joda-time/timezones.html)，其中提供呼叫 [put-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 時所支援 IANA 時區的相關資訊。

以下是建立排定動作在特定日期和時間擴展容量時使用 `--timezone` 選項的範例。

在 `--schedule` 指定的日期和時間 (2021 年 1 月 31 日下午 5 點當地時間)，如果 `MinCapacity` 指定的值高於目前的容量，則 Application Auto Scaling 會水平擴展至 `MinCapacity`。如果 `MaxCapacity` 指定的值低於目前的容量，則 Application Auto Scaling 會縮減至 `MaxCapacity`。

**Linux、macOS 或 Unix**

```
aws application-autoscaling put-scheduled-action --service-namespace comprehend \
  --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits \
  --resource-id arn:aws:comprehend:us-west-2:123456789012:document-classifier-endpoint/EXAMPLE \
  --scheduled-action-name  my-one-time-action \
  --schedule "at(2021-01-31T17:00:00)" --timezone "America/New_York" \
  --scalable-target-action MinCapacity=1,MaxCapacity=3
```

**Windows**

```
aws application-autoscaling put-scheduled-action --service-namespace comprehend ^
  --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits ^
  --resource-id arn:aws:comprehend:us-west-2:123456789012:document-classifier-endpoint/EXAMPLE ^
  --scheduled-action-name  my-one-time-action ^
  --schedule "at(2021-01-31T17:00:00)" --timezone "America/New_York" ^
  --scalable-target-action MinCapacity=1,MaxCapacity=3
```

## 建立指定時區的週期性排程動作
<a name="recurring-schedule-set-time-zone"></a>

以下範例使用 `--timezone` 選項，在建立週期性排程動作時擴展容量。如需詳細資訊，請參閱[使用 Application Auto Scaling 排程週期性擴展動作](scheduled-scaling-using-cron-expressions.md)。

按照指定的排程 (每週一到週五下午 6 點當地時間)，如果 `MinCapacity` 指定的值高於目前的容量，則 Application Auto Scaling 會水平擴展至 `MinCapacity`。如果 `MaxCapacity` 指定的值低於目前的容量，則 Application Auto Scaling 會縮減至 `MaxCapacity`。

**Linux、macOS 或 Unix**

```
aws application-autoscaling put-scheduled-action --service-namespace lambda \
  --scalable-dimension lambda:function:ProvisionedConcurrency \
  --resource-id function:my-function:BLUE \
  --scheduled-action-name my-recurring-action \
  --schedule "cron(0 18 ? * MON-FRI *)" --timezone "Etc/GMT+9" \
  --scalable-target-action MinCapacity=10,MaxCapacity=50
```

**Windows**

```
aws application-autoscaling put-scheduled-action --service-namespace lambda ^
  --scalable-dimension lambda:function:ProvisionedConcurrency ^
  --resource-id function:my-function:BLUE ^
  --scheduled-action-name my-recurring-action ^
  --schedule "cron(0 18 ? * MON-FRI *)" --timezone "Etc/GMT+9" ^
  --scalable-target-action MinCapacity=10,MaxCapacity=50
```