

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

# 搭配複本使用自動擴展
<a name="AutoScaling-Using-Replicas"></a>

ElastiCache 複寫群組可以設定一或多個快取，以做為單一邏輯節點運作。

以下提供目標追蹤和排程政策的詳細資訊，以及如何使用 AWS 管理主控台 AWS CLI 和 APIs 套用這些政策。

# 目標追蹤擴展政策
<a name="AutoScaling-Scaling-Policies-Replicas-Replicas"></a>

使用目標追蹤擴展政策，您可以選取指標及設定目標值。ElastiCache for Valkey 和 Redis OSS AutoScaling 會建立和管理 CloudWatch 警示，以觸發擴展政策，並根據指標和目標值計算擴展調整。規模調整政策會視需要平均在所有碎片間新增或移除複本，以讓指標保持在等於或接近指定目標值。除了讓指標保持在接近目標值之外，目標追蹤規模調整政策也會配合指標中，因為負載模式波動所造成的波動調整，並將機群容量中的快速波動降到最低。

## 複本的 Auto Scaling 條件
<a name="AutoScaling-Scaling-Criteria-Replicas"></a>

Auto Scaling 政策會為您的叢集定義下列預先定義的指標：

`ElastiCacheReplicaEngineCPUUtilization`：在 ElastiCache 用來觸發自動擴展操作的所有複本中彙總的 AVG EngineCPU 使用率閾值。您可以將使用率目標設定為 35% 到 70% 之間。

當服務偵測到 `ElastiCacheReplicaEngineCPUUtilization` 指標等於或大於目標設定，便會自動增加碎片間的複本數。ElastiCache 會將叢集複本擴展為等於兩個數字中較大者的計數：與目標和一個複本的百分比變化。對於縮減，除非整體指標值低於已定義目標的 75%，否則 ElastiCache 不會自動縮減。

以下提供水平擴展的範例，如果有 5 個碎片，各 1 個複本：

如果您的目標違規 30%，ElastiCache for Valkey 和 Redis OSS 會跨所有碎片向外擴展 1 個複本 (max(0.3，預設 1))。這會產生 5 個碎片，每個碎片各 2 個複本。

對於縮減範例，如果您已選取目標值 60%，ElastiCache for Valkey 和 Redis OSS 將不會自動縮減，直到指標小於或等於 45% （低於目標 60% 的 25%)。

### Auto Scaling 考量事項
<a name="AutoScaling-Scaling-Considerations-Replicas"></a>

請謹記以下幾點考量：
+ 目標追蹤擴展政策假設在指定的指標超過目標值時，應執行向外擴展。您無法使用目標追蹤擴展政策在指定的指標低於目標值時執行向外擴展。ElastiCache for Valkey 和 Redis OSS 會將複本擴展為叢集中所有碎片中現有複本的最大值 （與目標相差 %，預設 1)。
+ 所指定指標的資料不足時，目標追蹤擴展政策不會執行擴展。政策不會執行向內擴展，因為向內擴展不會將資料不足解釋為低使用率。
+ 您可能會看到目標值與實際指標資料點之間有些差距。這是因為當 ElastiCache Auto Scaling 決定要新增或移除多少容量時，一律會四捨五入或捨棄，以保守的方式運作。這樣可防止新增不足的容量，或移除過多的容量。
+ 為了確保應用程式的可用性，此服務可以配合指標依比例快速擴展規模，但是以更漸進的方式在叢集中碎片間最多縮減規模 1 個複本。
+ 您可以為 ElastiCache for Valkey 和 Redis OSS 叢集設定多個目標追蹤擴展政策，前提是每個叢集都使用不同的指標。Auto Scaling 的目的是一律優先考慮可用性，因此其行為會根據目標追蹤政策是否已準備好向外擴展或向內擴展而有所不同。如果任何目標追蹤政策已準備好向外擴展，它就會將服務向外擴展，但只有在所有目標追蹤政策 (已啟用向內擴展部分) 都已準備好要向內擴展時才會向內擴展。
+ 請勿編輯或刪除 ElastiCache Auto Scaling 針對目標追蹤擴展政策管理的 CloudWatch 警示。當您刪除擴展政策或刪除叢集時，Auto Scaling 會自動刪除警示。
+ ElastiCache Auto Scaling 不會阻止您跨碎片手動修改複本。這些手動調整不會影響附加至調整規模政策的任何現有 CloudWatch 警示，但會影響可能觸發這些 CloudWatch 警示的指標。
+ 這些由 Auto Scaling 管理的 CloudWatch 警示，是根據叢集中所有碎片的 AVG 指標所定義的。因此，使用熱碎片可能會導致以下任一情況：
  + 由於幾個熱碎片上的負載觸發了 CloudWatch 警示，因此在沒有需要時進行擴展
  + 由於跨影響警示的所有碎片之彙總 AVG 沒有超出限制，因此在需要時不進行擴展。
+ 每個叢集節點的 ElastiCache 預設限制仍然適用。因此，當選擇使用 Auto Scaling 且您希望節點數上限超過預設配額時，請至 [AWS 服務配額](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)請求增加配額，並選擇配額類型 **Nodes per cluster per instance type (每執行個體類型每叢集的節點數)**。
+ 確保 VPC 中有足夠的 ENI (彈性網路介面) 可用，進行水平擴展時會需要用到。如需詳細資訊，請參閱[彈性網路介面](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_ElasticNetworkInterfaces.html)。
+ 如果 EC2 沒有足夠的容量可用，ElastiCache Auto Scaling 將不會橫向擴展，直到容量可用，或者如果您將叢集手動修改為具有足夠容量的執行個體類型。
+ ElastiCache Auto Scaling 不支援擴展叢集`ReservedMemoryPercent`低於 25% 的複本。如需詳細資訊，請參閱[管理 Valkey 和 Redis OSS 的預留記憶體](redis-memory-management.md)。

# 新增擴展原則
<a name="AutoScaling-Adding-Policy-Replicas"></a>

您可以使用 新增擴展政策 AWS 管理主控台。

**使用 新增擴展政策 AWS 管理主控台**

將自動擴展政策新增至 ElastiCache for Valkey 和 Redis OSS

1. 登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/)：// 開啟 Amazon ElastiCache 主控台。

1. 在導覽窗格中，選擇 **Valkey** 或 **Redis OSS**。

1. 選擇要新增政策至其中的叢集 (請選擇叢集名稱而非其左側的按鈕)。

1. 選擇 **Auto Scaling policies (Auto Scaling 政策)** 索引標籤。

1. 選擇 **Dynamic scaling** (動態擴展)。

1. 根據 **Scaling policies** (擴展政策)，選擇 **Add dynamic scaling** (新增動態擴展)。

1. 在 **Policy Name (政策名稱)** 輸入政策的名稱。

1. 在 **Scalable Dimension (可擴展的維度)**，從對話方塊中選取 **Replicas (複本)**。

1. 針對目標值，輸入您要在 ElastiCache 複本上維護的 CPU 使用率平均百分比。此值必須 >=35 且 <=70。系統會新增或移除叢集複本，讓指標接近指定的值。

1. (選用) 主控台不支援規模縮減或水平擴展冷卻時間。使用 AWS CLI 修改冷卻值。

1. 針對**最小容量**，輸入 ElastiCache Auto Scaling 政策需要維護的複本數量下限。

1. 針對**最大容量**，輸入 ElastiCache Auto Scaling 政策需要維護的複本數量上限。此值必須 >=5。

1. 選擇**建立**。

# 註冊可擴展的目標
<a name="AutoScaling-Register-Policy"></a>

您可以根據預先定義的指標或自訂指標，套用規模調整政策。若要這樣做，您可以使用 AWS CLI 或 Application Auto Scaling API。第一步是向 Auto Scaling 註冊 ElastiCache for Valkey 和 Redis OSS 複寫群組。

您必須先向 ElastiCache 自動擴展註冊叢集，才能搭配叢集使用 ElastiCache 自動擴展。這是為了定義要套用到叢集的擴展維度和限制。ElastiCache 自動擴展會沿著`elasticache:replication-group:Replicas`可擴展維度動態擴展叢集，這代表每個碎片的叢集複本數量。

**使用 CLI** 

若要註冊 ElastiCache 叢集，請使用 [register-scalable-target](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/register-scalable-target.html) 命令搭配下列參數：
+ --service-namespace - 將此值設為 elasticache。
+ --resource-id – ElastiCache 叢集的資源識別符。在此參數中，資源類型為 ReplicationGroup，唯一識別符為叢集的名稱，例如 `replication-group/myscalablecluster`。
+ --scalable-dimension - 將此值設定為 `elasticache:replication-group:Replicas`。
+ --min-capacity – ElastiCache Auto Scaling 要管理的複本數量下限。如需了解 --min-capacity、--max-capacity 以及叢集中複本數之間的關係，請參閱「[容量下限和上限](AutoScaling-Policies.md#AutoScaling-MinMax)」。
+ --max-capacity – ElastiCache Auto Scaling 要管理的複本數量上限。如需了解 --min-capacity、--max-capacity 以及叢集中複本數之間的關係，請參閱「[容量下限和上限](AutoScaling-Policies.md#AutoScaling-MinMax)」。

**Example**  
在下列範例中，您會註冊名為 的 ElastiCache 叢集`myscalablecluster`。註冊中表明應該動態擴展叢集，使其具有 1 到 5 個複本。  
若為 Linux、macOS 或 Unix：  

```
aws application-autoscaling register-scalable-target \
    --service-namespace elasticache \
    --resource-id replication-group/myscalablecluster \
    --scalable-dimension elasticache:replication-group:Replicas \
    --min-capacity 1 \
    --max-capacity 5 \
```
針對 Windows：  

```
aws application-autoscaling register-scalable-target ^
    --service-namespace elasticache ^
    --resource-id replication-group/myscalablecluster ^
    --scalable-dimension elasticache:replication-group:Replicas ^
    --min-capacity 1 ^
    --max-capacity 5 ^
```

**使用 API**

若要註冊 ElastiCache 叢集，請使用 [register-scalable-target](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/register-scalable-target.html) 命令搭配下列參數：
+ ServiceNamespace - 將此值設為 elasticache。
+ ResourceID - ElastiCache 叢集的資源識別碼。在此參數中，資源類型為 ReplicationGroup，唯一識別符為叢集的名稱，例如 `replication-group/myscalablecluster`。
+ ScalableDimension - 將此值設為 `elasticache:replication-group:Replicas`。
+ MinCapacity：由 ElastiCache 自動擴展管理的複本數目下限。如需了解 --min-capacity、--max-capacity 以及叢集中複本數之間的關係，請參閱「[容量下限和上限](AutoScaling-Policies.md#AutoScaling-MinMax)」。
+ MaxCapacity：由 ElastiCache 自動擴展管理的複本數量上限。如需了解 --min-capacity、--max-capacity 以及叢集中複本數之間的關係，請參閱「[容量下限和上限](AutoScaling-Policies.md#AutoScaling-MinMax)」。

**Example**  
在下列範例中，您會`myscalablecluster`使用 Application Auto Scaling API 註冊名為 的叢集。此註冊中表明應該動態擴展叢集，使其具有 1 到 5 個複本。

```
POST / HTTP/1.1
Host: autoscaling.us-east-2.amazonaws.com
Accept-Encoding: identity
Content-Length: 219
X-Amz-Target: AnyScaleFrontendService.RegisterScalableTarget
X-Amz-Date: 20160506T182145Z
User-Agent: aws-cli/1.10.23 Python/2.7.11 Darwin/15.4.0 botocore/1.4.8
Content-Type: application/x-amz-json-1.1
Authorization: AUTHPARAMS
{
    "ServiceNamespace": "elasticache",
    "ResourceId": "replication-group/myscalablecluster",
    "ScalableDimension": "elasticache:replication-group:Replicas",
    "MinCapacity": 1,
    "MaxCapacity": 5
}
```

# 定義擴展政策
<a name="AutoScaling-Defining-Policy"></a>

目標追踪規模調整政策組態由 JSON 區塊表示，其中定義了指標和目標值。您可以將規模調整政策的組態設定，儲存為文字檔案中的 JSON 區塊。叫用 AWS CLI 或 Application Auto Scaling API 時，您可以使用該文字檔案。如需政策組態語法的詳細資訊，請參閱 *Application Auto Scaling API 參考*中的 [TargetTrackingScalingPolicyConfiguration](https://docs.aws.amazon.com/ApplicationAutoScaling/latest/APIReference/API_TargetTrackingScalingPolicyConfiguration.html)。

您可使用下列選項來定義目標追蹤擴展政策的組態：

**Topics**
+ [使用預先定義的指標](#AutoScaling-Predefined-Metric)
+ [編輯擴展原則](AutoScaling-Editing-Policy.md)
+ [刪除擴展原則](AutoScaling-Deleting-Policy.md)
+ [CloudFormation 使用 Auto Scaling 政策](AutoScaling-with-Cloudformation.md)
+ [排程擴展](AutoScaling-with-Scheduled-Scaling-Replicas.md)

## 使用預先定義的指標
<a name="AutoScaling-Predefined-Metric"></a>

目標追踪規模調整政策組態由 JSON 區塊表示，其中定義了指標和目標值。您可以將規模調整政策的組態設定，儲存為文字檔案中的 JSON 區塊。叫用 AWS CLI 或 Application Auto Scaling API 時，您可以使用該文字檔案。如需政策組態語法的詳細資訊，請參閱 *Application Auto Scaling API 參考*中的 [TargetTrackingScalingPolicyConfiguration](https://docs.aws.amazon.com/ApplicationAutoScaling/latest/APIReference/API_TargetTrackingScalingPolicyConfiguration.html)。

您可使用下列選項來定義目標追蹤擴展政策的組態：

**Topics**
+ [使用預先定義的指標](#AutoScaling-Predefined-Metric)
+ [使用自訂的指標](#AutoScaling-Custom-Metric)
+ [使用冷卻時間](#AutoScaling-Using-Cooldowns)
+ [停用規模縮減活動](#AutoScaling-Disabling-Scalein)
+ [將擴展政策套用至 ElastiCache for Valkey 和 Redis OSS 叢集](#AutoScaling-Applying-Policy)

### 使用預先定義的指標
<a name="AutoScaling-Predefined-Metric"></a>

透過使用預先定義的指標，您可以快速定義 ElastiCache for Valkey 和 Redis OSS 叢集的目標追蹤擴展政策，該叢集可與 ElastiCache Auto Scaling 中的目標追蹤搭配使用。目前，ElastiCache 在 ElastiCache 複本 Auto Scaling 中支援下列預先定義的指標：

`ElastiCacheReplicaEngineCPUUtilization` – 叢集中所有複本在 CloudWatch 中 EngineCPUUtilization 指標的平均值。您可以在 ElastiCache 下的 CloudWatch 中找到`ReplicationGroupId, Role`所需的 ReplicationGroupId 和角色複本的彙總指標值。

若要在您的規模調整政策中使用預先定義的指標，請為規模調整政策建立目標追蹤組態設定。此組態設定必須加入用於預先定義指標的 `PredefinedMetricSpecification`，以及用於該指標目標值的 `TargetValue`。

### 使用自訂的指標
<a name="AutoScaling-Custom-Metric"></a>

使用自訂的指標，您可以定義目標追蹤規模調整政策來滿足您的自訂需求。您可以根據隨擴展比例變更的任何 ElastiCache for Valkey 和 Redis OSS 指標來定義自訂指標。並非所有 ElastiCache 指標都適用於目標追蹤。指標必須是有效的使用率指標，而且能夠表示執行個體的忙碌程度。指標的值必須根據叢集中複本的數量依比例增加或減少。若要使用指標資料來依比例增加或減少碎片數量，這樣子成比例的增加或減少是必要的。

**Example**  
下列的範例描述規模調整政策的目標追蹤組態設定。在此組態中，自訂指標會根據名為 的叢集中所有複本的平均 CPU 使用率 50% 來調整叢集`my-db-cluster`。  

```
{"TargetValue": 50,
    "CustomizedMetricSpecification":
    {"MetricName": "EngineCPUUtilization",
        "Namespace": "AWS/ElastiCache",
        "Dimensions": [
            {"Name": "ReplicationGroup","Value": "my-db-cluster"},
            {"Name": "Role","Value": "REPLICA"}
        ],
        "Statistic": "Average",
        "Unit": "Percent"
    }
}
```

### 使用冷卻時間
<a name="AutoScaling-Using-Cooldowns"></a>

您可以指定一個值 (單位為秒)，讓 `ScaleOutCooldown` 新增冷卻時間以便水平擴展您的叢集。同樣的，您可以指定一個值 (單位為秒)，讓 `ScaleInCooldown` 新增冷卻時間以便為您的叢集縮減規模。如需有關 `ScaleInCooldown` 和 `ScaleOutCooldown` 的詳細資訊，請參閱 *Application Auto Scaling API 參考*中的 [TargetTrackingScalingPolicyConfiguration](https://docs.aws.amazon.com/ApplicationAutoScaling/latest/APIReference/API_TargetTrackingScalingPolicyConfiguration.html)。下列的範例描述規模調整政策的目標追蹤組態設定。在此組態中，`ElastiCacheReplicaEngineCPUUtilization`預先定義的指標會根據該叢集中所有複本的平均 CPU 使用率 40% 來調整叢集。這個組態設定分別提供了 10 分鐘的規模縮減冷卻時間，和 5 分鐘的規模擴展冷卻時間。

```
{"TargetValue": 40.0,
    "PredefinedMetricSpecification":
    {"PredefinedMetricType": "ElastiCacheReplicaEngineCPUUtilization"
    },
    "ScaleInCooldown": 600,
    "ScaleOutCooldown": 300
}
```

### 停用規模縮減活動
<a name="AutoScaling-Disabling-Scalein"></a>

您可以停用縮減活動，防止目標追蹤擴展政策組態在 ElastiCache for Valkey 和 Redis OSS 叢集中擴展。停用規模縮減的動作，可防止規模調整政策刪除複本，同時讓規模調整政策仍然能視需要新增複本。

您可以為 `DisableScaleIn` 指定布林值，以啟用或停用 叢集的規模縮減活動。如需有關 `DisableScaleIn` 的詳細資訊，請參閱 *Application Auto Scaling API 參考*中的 [TargetTrackingScalingPolicyConfiguration](https://docs.aws.amazon.com/ApplicationAutoScaling/latest/APIReference/API_TargetTrackingScalingPolicyConfiguration.html)。

**Example**  
下列的範例描述規模調整政策的目標追蹤組態設定。在此組態中，`ElastiCacheReplicaEngineCPUUtilization`預先定義的指標會根據該叢集中所有複本的平均 CPU 使用率 40% 來調整叢集。此組態設定停用了規模調整政策的規模縮減動作。

```
{"TargetValue": 40.0,
    "PredefinedMetricSpecification":
    {"PredefinedMetricType": "ElastiCacheReplicaEngineCPUUtilization"
    },
    "DisableScaleIn": true
}
```

### 將擴展政策套用至 ElastiCache for Valkey 和 Redis OSS 叢集
<a name="AutoScaling-Applying-Policy"></a>

向 ElastiCache for Valkey 和 Redis OSS 自動擴展註冊叢集並定義擴展政策之後，您可以將擴展政策套用至已註冊的叢集。若要將擴展政策套用至 ElastiCache for Valkey 和 Redis OSS 叢集，您可以使用 AWS CLI 或 Application Auto Scaling API。

**使用 AWS CLI**

若要將擴展政策套用至 ElastiCache for Valkey 和 Redis OSS 叢集，請使用 [put-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/autoscaling/put-scaling-policy.html) 命令搭配下列參數：
+ --policy-name - 規模調整政策的名稱。
+ --policy-type - 將此值設為 `TargetTrackingScaling`。
+ --resource-id – 叢集的資源識別符。在此參數中，資源類型為 ReplicationGroup，唯一識別符為叢集的名稱，例如 `replication-group/myscalablecluster`。
+ --service-namespace - 將此值設為 elasticache。
+ --scalable-dimension - 將此值設定為 `elasticache:replication-group:Replicas`。
+ --target-tracking-scaling-policy-configuration – 要用於叢集的目標追蹤擴展政策組態。

**Example**  
在下列範例中，您將名為 的目標追蹤擴展政策套用`myscalablepolicy`到`myscalablecluster`使用 ElastiCache 自動擴展名為 的叢集。做法是使用儲存於 `config.json` 檔案中的政策組態設定。

針對 Linux、macOS 或 Unix：

```
aws application-autoscaling put-scaling-policy \
    --policy-name myscalablepolicy \
    --policy-type TargetTrackingScaling \
    --resource-id replication-group/myscalablecluster \
    --service-namespace elasticache \
    --scalable-dimension elasticache:replication-group:Replicas \
    --target-tracking-scaling-policy-configuration file://config.json
```

```
{"TargetValue": 40.0,
    "PredefinedMetricSpecification":
    {"PredefinedMetricType": "ElastiCacheReplicaEngineCPUUtilization"
    },
    "DisableScaleIn": true
}
```

針對 Windows：

```
aws application-autoscaling put-scaling-policy ^
    --policy-name myscalablepolicy ^
    --policy-type TargetTrackingScaling ^
    --resource-id replication-group/myscalablecluster ^
    --service-namespace elasticache ^
    --scalable-dimension elasticache:replication-group:Replicas ^
    --target-tracking-scaling-policy-configuration file://config.json
```

**使用 API**

若要使用 Application Auto Scaling API 將擴展政策套用至 ElastiCache 叢集，請使用 [PutScalingPolicy](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PutScalingPolicy.html) Application Auto Scaling API 操作搭配下列參數：
+ PolicyName - 規模調整政策的名稱。
+ PolicyType - 將此值設為 `TargetTrackingScaling`。
+ ResourceID – 叢集的資源識別符。在此參數中，資源類型為 ReplicationGroup，唯一識別符為 ElastiCache for Redis OSS 叢集的名稱，例如 `replication-group/myscalablecluster`。
+ ServiceNamespace - 將此值設為 elasticache。
+ ScalableDimension - 將此值設為 `elasticache:replication-group:Replicas`。
+ TargetTrackingScalingPolicyConfiguration – 要用於叢集的目標追蹤擴展政策組態。

**Example**  
在下列範例中，您將名為 的目標追蹤擴展政策套用`scalablepolicy`到`myscalablecluster`使用 ElastiCache 自動擴展名為 的叢集。您使用的政策組態設定，是以 `ElastiCacheReplicaEngineCPUUtilization` 這個預先定義的指標為根據。

```
POST / HTTP/1.1
Host: autoscaling.us-east-2.amazonaws.com
Accept-Encoding: identity
Content-Length: 219
X-Amz-Target: AnyScaleFrontendService.PutScalingPolicy
X-Amz-Date: 20160506T182145Z
User-Agent: aws-cli/1.10.23 Python/2.7.11 Darwin/15.4.0 botocore/1.4.8
Content-Type: application/x-amz-json-1.1
Authorization: AUTHPARAMS
{
    "PolicyName": "myscalablepolicy",
    "ServiceNamespace": "elasticache",
    "ResourceId": "replication-group/myscalablecluster",
    "ScalableDimension": "elasticache:replication-group:Replicas",
    "PolicyType": "TargetTrackingScaling",
    "TargetTrackingScalingPolicyConfiguration": {
        "TargetValue": 40.0,
        "PredefinedMetricSpecification":
        {
            "PredefinedMetricType": "ElastiCacheReplicaEngineCPUUtilization"
        }
    }
}
```

# 編輯擴展原則
<a name="AutoScaling-Editing-Policy"></a>

您可以使用 AWS 管理主控台、 AWS CLI或 Application Auto Scaling API 編輯擴展政策。

**使用 編輯擴展政策 AWS 管理主控台**

您只能使用 AWS 管理主控台來編輯具有預先定義指標類型的政策

1. 登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/)：// 開啟 Amazon ElastiCache 主控台。

1. 在導覽窗格中，選擇 **Valkey** 或 **Redis OSS**

1. 選擇要新增政策至其中的叢集 (請選擇叢集名稱而非其左側的按鈕)。

1. 選擇 **Auto Scaling policies (Auto Scaling 政策)** 索引標籤。

1. 根據 **Scaling policies** (擴展政策)，找到您要變更的自動擴展政策，並選擇 **Modify** (修改)。

1. 對政策進行必要的變更。

1. 選擇 **Modify** (修改)。

1. 對政策進行變更。

1. 選擇 **Modify** (修改)。

**使用 AWS CLI 或 Application Auto Scaling API 編輯擴展政策 **

您可以使用 AWS CLI 或 Application Auto Scaling API，以套用擴展政策的相同方式編輯擴展政策：
+ 使用 Application Auto Scaling API 時，請在 `PolicyName` 參數中指定您所要編輯之政策的名稱。針對您想要變更的參數指定新的參數值。

如需詳細資訊，請參閱[將擴展政策套用至 ElastiCache for Valkey 和 Redis OSS 叢集](AutoScaling-Defining-Policy.md#AutoScaling-Applying-Policy)。

# 刪除擴展原則
<a name="AutoScaling-Deleting-Policy"></a>

您可以使用 AWS 管理主控台、 AWS CLI 或 Application Auto Scaling API 刪除擴展政策

**使用 刪除擴展政策 AWS 管理主控台**

您只能使用 AWS 管理主控台來編輯具有預先定義指標類型的政策

1. 登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/)：// 開啟 Amazon ElastiCache 主控台。

1. 在導覽窗格中，選擇 **Valkey** 或 **Redis OSS**

1. 選擇您要刪除其 Auto Scaling 政策的叢集。

1. 選擇 **Auto Scaling policies (Auto Scaling 政策)** 索引標籤。

1. 在 **Scaling Policies** (擴展政策) 區段中，選擇自動擴展政策，然後選擇 **Delete** (刪除)。

**使用 AWS CLI 或 Application Auto Scaling API 刪除擴展政策 **

您可以使用 AWS CLI 或 Application Auto Scaling API，從 ElastiCache 叢集刪除擴展政策。

**CLI**

若要從 ElastiCache for Valkey 和 Redis OSS 叢集中刪除擴展政策，請使用 [delete-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/delete-scaling-policy.html) 命令搭配下列參數：
+ --policy-name - 規模調整政策的名稱。
+ --resource-id – 叢集的資源識別符。在此參數中，資源類型為 ReplicationGroup，唯一識別符為叢集的名稱，例如 `replication-group/myscalablecluster`。
+ --service-namespace - 將此值設為 elasticache。
+ --scalable-dimension - 將此值設定為 `elasticache:replication-group:Replicas`。

**Example**  
在下列範例中，會將名為 `myscalablepolicy` 的目標追蹤擴展政策，從名為 `myscalablecluster` 的 ELC; 叢集中刪除。

若為 Linux、macOS 或 Unix：

```
aws application-autoscaling delete-scaling-policy \
    --policy-name myscalablepolicy \
    --resource-id replication-group/myscalablecluster \
    --service-namespace elasticache \
    --scalable-dimension elasticache:replication-group:Replicas \
```

針對 Windows：

```
aws application-autoscaling delete-scaling-policy ^
    --policy-name myscalablepolicy ^
    --resource-id replication-group/myscalablecluster ^
    --service-namespace elasticache ^
    --scalable-dimension elasticache:replication-group:Replicas ^
```

**API**

若要從 ElastiCache for Valkey 和 Redis OSS 叢集刪除擴展政策，請使用 [DeleteScalingPolicy](https://docs.aws.amazon.com/ApplicationAutoScaling/latest/APIReference/API_DeleteScalingPolicy.html) Application Auto Scaling API 操作搭配下列參數：
+ PolicyName - 規模調整政策的名稱。
+ ResourceID – 叢集的資源識別符。在此參數中，資源類型為 ReplicationGroup，唯一識別符為叢集的名稱，例如 `replication-group/myscalablecluster`。
+ ServiceNamespace - 將此值設為 elasticache。
+ ScalableDimension - 將此值設為 `elasticache:replication-group:Replicas`。

在下列範例中，您會`myscalablepolicy`使用 Application Auto Scaling API 從名為 `myscalablecluster`的叢集中刪除名為 的目標追蹤擴展政策。

```
POST / HTTP/1.1
>>>>>>> mainline
Host: autoscaling.us-east-2.amazonaws.com
Accept-Encoding: identity
Content-Length: 219
X-Amz-Target: AnyScaleFrontendService.DeleteScalingPolicy
X-Amz-Date: 20160506T182145Z
User-Agent: aws-cli/1.10.23 Python/2.7.11 Darwin/15.4.0 botocore/1.4.8
Content-Type: application/x-amz-json-1.1
Authorization: AUTHPARAMS
{
    "PolicyName": "myscalablepolicy",
    "ServiceNamespace": "elasticache",
    "ResourceId": "replication-group/myscalablecluster",
    "ScalableDimension": "elasticache:replication-group:Replicas"
}
```

# CloudFormation 使用 Auto Scaling 政策
<a name="AutoScaling-with-Cloudformation"></a>

此程式碼片段顯示如何建立排定的動作，並使用資源 [AWS::ApplicationAutoScaling::ScalableTarget](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-applicationautoscaling-scalabletarget.html) 將其套用到 [AWS::ElastiCache::ReplicationGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticache-replicationgroup.html) 資源。其使用 [Fn::Join](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-join.html) 和 [Ref](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html) 內部函數來建構 `ResourceId` 屬性，該屬性具有相同範本中指定的 `AWS::ElastiCache::ReplicationGroup` 資源的邏輯名稱。

```
ScalingTarget:
   Type: 'AWS::ApplicationAutoScaling::ScalableTarget'
   Properties:
     MaxCapacity: 0
     MinCapacity: 0
     ResourceId: !Sub replication-group/${logicalName}
     ScalableDimension: 'elasticache:replication-group:Replicas'
     ServiceNamespace: elasticache
     RoleARN: !Sub "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/elasticache.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ElastiCacheRG"

  ScalingPolicy:
    Type: "AWS::ApplicationAutoScaling::ScalingPolicy"
    Properties:
      ScalingTargetId: !Ref ScalingTarget
      ServiceNamespace: elasticache
      PolicyName: testpolicy
      PolicyType: TargetTrackingScaling
      ScalableDimension: 'elasticache:replication-group:Replicas'
      TargetTrackingScalingPolicyConfiguration:
        PredefinedMetricSpecification:
          PredefinedMetricType: ElastiCacheReplicaEngineCPUUtilization
        TargetValue: 40
```

# 排程擴展
<a name="AutoScaling-with-Scheduled-Scaling-Replicas"></a>

按照排程進行擴展讓您能夠因應可預測的需求變化，據以擴展您的應用程式。若要使用排程擴展，您可以建立排程動作，指示 ElastiCache for Valkey 和 Redis OSS 在特定時間執行擴展活動。當您建立排程動作時，您可以指定現有的 ElastiCache 叢集、應進行擴展活動的時間、最小容量和最大容量。您可以建立僅擴展一次或依週期性排程擴展的排程動作。

 您只能為已存在的 ElastiCache 叢集建立排程動作。您無法在建立叢集的同時建立排程動作。

如需排程動作建立、管理和刪除作業相關術語的詳細資訊，請參閱[排程動作建立、管理和刪除作業常用的命令](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-scheduled-scaling.html#scheduled-scaling-commonly-used-commands) 

**建立一次性排程動作：**

類似於碎片維度。請參閱 [排程擴展](AutoScaling-with-Scheduled-Scaling-Shards.md)。

**刪除排程動作**

類似於碎片維度。請參閱 [排程擴展](AutoScaling-with-Scheduled-Scaling-Shards.md)。

**使用 AWS CLI 管理排定的擴展**

使用下列 application-autoscaling API：
+ [put-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 
+ [describe-scheduled-actions](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/describe-scheduled-actions.html) 
+ [delete-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/delete-scheduled-action.html) 

## 使用 CloudFormation 建立 Auto Scaling 政策
<a name="AutoScaling-with-Cloudformation-Update-Action"></a>

此程式碼片段顯示如何建立排定的動作，並使用資源 [AWS::ApplicationAutoScaling::ScalableTarget](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-applicationautoscaling-scalabletarget.html) 將其套用到 [AWS::ElastiCache::ReplicationGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticache-replicationgroup.html) 資源。其使用 [Fn::Join](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-join.html) 和 [Ref](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html) 內部函數來建構 `ResourceId` 屬性，該屬性具有相同範本中指定的 `AWS::ElastiCache::ReplicationGroup` 資源的邏輯名稱。

```
ScalingTarget:
   Type: 'AWS::ApplicationAutoScaling::ScalableTarget'
   Properties:
     MaxCapacity: 0
     MinCapacity: 0
     ResourceId: !Sub replication-group/${logicalName}
     ScalableDimension: 'elasticache:replication-group:Replicas'
     ServiceNamespace: elasticache
     RoleARN: !Sub "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/elasticache.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ElastiCacheRG"
     ScheduledActions:
       - EndTime: '2020-12-31T12:00:00.000Z'
         ScalableTargetAction:
           MaxCapacity: '5'
           MinCapacity: '2'
         ScheduledActionName: First
         Schedule: 'cron(0 18 * * ? *)'
```