

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

# 定義擴展政策
<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 * * ? *)'
```