

# Example scaling policies for the Amazon CLI
<a name="examples-scaling-policies"></a>

You can create scaling policies for Amazon EC2 Auto Scaling through the Amazon Web Services Management Console, Amazon Command Line Interface (Amazon CLI), or SDKs. 

The following examples show how you can create scaling policies for Amazon EC2 Auto Scaling with the Amazon CLI [put-scaling-policy](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/put-scaling-policy.html) command. Replace each {{user input placeholder}} with your own information.

To get started with writing scaling policies using the Amazon CLI, see the introductory exercises in [Target tracking scaling policies](as-scaling-target-tracking.md) and [Step and simple scaling policies](as-scaling-simple-step.md). 

**Example 1: To apply a target tracking scaling policy with a predefined metric specification**

```
aws autoscaling put-scaling-policy --policy-name {{cpu50-target-tracking-scaling-policy}} \
  --auto-scaling-group-name {{my-asg}} --policy-type TargetTrackingScaling \
  --target-tracking-configuration {{file://config.json}}
{
  "TargetValue": {{50.0}},
  "PredefinedMetricSpecification": {
    "PredefinedMetricType": "{{ASGAverageCPUUtilization}}"
  }
}
```

For more information, see [PredefinedMetricSpecification](https://docs.amazonaws.cn/autoscaling/ec2/APIReference/API_PredefinedMetricSpecification.html) in the *Amazon EC2 Auto Scaling API Reference*.

**Note**  
If the file is not in the current directory, type the full path to file. For more information about reading Amazon CLI parameter values from a file, see [Loading Amazon CLI parameters from a file](https://docs.amazonaws.cn/cli/latest/userguide/cli-usage-parameters-file.html) in the Amazon Command Line Interface User Guide.

**Example 2: To apply a target tracking scaling policy with a customized metric specification**

```
aws autoscaling put-scaling-policy --policy-name {{sqs100-target-tracking-scaling-policy}} \
  --auto-scaling-group-name {{my-asg}} --policy-type TargetTrackingScaling \
  --target-tracking-configuration {{file://config.json}}
{
  "TargetValue": {{100.0}},
  "CustomizedMetricSpecification": {
    "MetricName": "{{MyBacklogPerInstance}}",
    "Namespace": "{{MyNamespace}}",
    "Dimensions": [{
      "Name": "{{MyOptionalMetricDimensionName}}",
      "Value": "{{MyOptionalMetricDimensionValue}}"
    }],
    "Statistic": "{{Average}}",
    "Unit": "{{None}}"
  }
}
```

For more information, see [CustomizedMetricSpecification](https://docs.amazonaws.cn/autoscaling/ec2/APIReference/API_CustomizedMetricSpecification.html) in the *Amazon EC2 Auto Scaling API Reference*.

**Example 3: To apply a target tracking scaling policy for scale out only**

```
aws autoscaling put-scaling-policy --policy-name {{alb1000-target-tracking-scaling-policy}} \
  --auto-scaling-group-name {{my-asg}} --policy-type TargetTrackingScaling \
  --target-tracking-configuration {{file://config.json}}
{
  "TargetValue": {{1000.0}},
  "PredefinedMetricSpecification": {
    "PredefinedMetricType": "{{ALBRequestCountPerTarget}}",
    "ResourceLabel": "{{app/my-alb/778d41231b141a0f/targetgroup/my-alb-target-group/943f017f100becff}}"
  },
  "DisableScaleIn": true
}
```

**Example 4: To apply a step scaling policy for scale out**

```
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name {{my-asg}}  \
  --policy-name {{my-step-scale-out-policy}} \
  --policy-type StepScaling \
  --adjustment-type {{PercentChangeInCapacity}} \
  --metric-aggregation-type {{Average}} \
  --step-adjustments MetricIntervalLowerBound={{10.0}},MetricIntervalUpperBound={{20.0}},ScalingAdjustment={{10}} \
                     MetricIntervalLowerBound={{20.0}},MetricIntervalUpperBound={{30.0}},ScalingAdjustment={{20}} \
                     MetricIntervalLowerBound={{30.0}},ScalingAdjustment={{30}} \
  --min-adjustment-magnitude {{1}}
```

Record the policy's Amazon Resource Name (ARN). You need the ARN when you create the CloudWatch alarm.

**Example 5: To apply a step scaling policy for scale in**

```
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name {{my-asg}}  \
  --policy-name {{my-step-scale-in-policy}} \
  --policy-type StepScaling \
  --adjustment-type {{ChangeInCapacity}} \
  --step-adjustments MetricIntervalUpperBound={{0.0}},ScalingAdjustment={{-2}}
```

Record the policy's Amazon Resource Name (ARN). You need the ARN when you create the CloudWatch alarm.

**Example 6: To apply a simple scaling policy for scale out**

```
aws autoscaling put-scaling-policy --policy-name {{my-simple-scale-out-policy}} \
  --auto-scaling-group-name {{my-asg}} --scaling-adjustment {{30}} \
  --adjustment-type {{PercentChangeInCapacity}} --min-adjustment-magnitude {{2}}
```

Record the policy's Amazon Resource Name (ARN). You need the ARN when you create the CloudWatch alarm.

**Example 7: To apply a simple scaling policy for scale in**

```
aws autoscaling put-scaling-policy --policy-name {{my-simple-scale-in-policy}} \
  --auto-scaling-group-name {{my-asg}} --scaling-adjustment {{-1}} \
  --adjustment-type {{ChangeInCapacity}} --cooldown {{180}}
```

Record the policy's Amazon Resource Name (ARN). You need the ARN when you create the CloudWatch alarm.