community.aws.ec2_scaling_policy (4.3.0) — module

Create or delete AWS scaling policies for Autoscaling groups

| "added in version" 1.0.0 of community.aws"

Authors: Zacharie Eakin (@zeekin), Will Thames (@willthames)

Install collection

Install with ansible-galaxy collection install community.aws:==4.3.0


Add to requirements.yml

  collections:
    - name: community.aws
      version: 4.3.0

Description

Can create or delete scaling policies for autoscaling groups.

Referenced autoscaling groups must already exist.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Simple Scale Down policy
  community.aws.ec2_scaling_policy:
    state: present
    region: US-XXX
    name: "scaledown-policy"
    adjustment_type: "ChangeInCapacity"
    asg_name: "application-asg"
    scaling_adjustment: -1
    min_adjustment_step: 1
    cooldown: 300
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# For an alarm with a breach threshold of 20, the
# following creates a stepped policy:
# From 20-40 (0-20 above threshold), increase by 50% of existing capacity
# From 41-infinity, increase by 100% of existing capacity
- community.aws.ec2_scaling_policy:
    state: present
    region: US-XXX
    name: "step-scale-up-policy"
    policy_type: StepScaling
    metric_aggregation: Maximum
    step_adjustments:
      - upper_bound: 20
        scaling_adjustment: 50
      - lower_bound: 20
        scaling_adjustment: 100
    adjustment_type: "PercentChangeInCapacity"
    asg_name: "application-asg"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: create TargetTracking predefined policy
  ec2_scaling_policy:
    name: "predefined-policy-1"
    policy_type: TargetTrackingScaling
    target_tracking_config:
      predefined_metric_spec:
        predefined_metric_type: ASGAverageCPUUtilization
      target_value: 98.0
    asg_name: "asg-test-1"
  register: result
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: create TargetTracking predefined policy with resource_label
  ec2_scaling_policy:
    name: "predefined-policy-1"
    policy_type: TargetTrackingScaling
    target_tracking_config:
      predefined_metric_spec:
        predefined_metric_type: ALBRequestCountPerTarget
        resource_label: app/my-alb/778d41231d141a0f/targetgroup/my-alb-target-group/942f017f100becff
      target_value: 98.0
    asg_name: "asg-test-1"
  register: result
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: create TargetTrackingScaling custom policy
  ec2_scaling_policy:
    name: "custom-policy-1"
    policy_type: TargetTrackingScaling
    target_tracking_config:
      customized_metric_spec:
        metric_name: metric_1
        namespace: namespace_1
        statistic: Minimum
        unit: Gigabits
        dimensions: [{'Name': 'dimension1', 'Value': 'value1'}]
      disable_scalein: true
      target_value: 98.0
    asg_name: asg-test-1
  register: result

Inputs

    
name:
    description:
    - Unique name for the scaling policy.
    required: true
    type: str

state:
    choices:
    - present
    - absent
    default: present
    description:
    - Register or deregister the policy.
    type: str

region:
    aliases:
    - aws_region
    - ec2_region
    description:
    - The AWS region to use. If not specified then the value of the AWS_REGION or EC2_REGION
      environment variable, if any, is used. See U(http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region)
    type: str

ec2_url:
    aliases:
    - aws_endpoint_url
    - endpoint_url
    description:
    - Url to use to connect to EC2 or your Eucalyptus cloud (by default the module will
      use EC2 endpoints). Ignored for modules where region is required. Must be specified
      for all other modules if region is not used. If not set then the value of the EC2_URL
      environment variable, if any, is used.
    type: str

profile:
    aliases:
    - aws_profile
    description:
    - Uses a boto profile. Only works with boto >= 2.24.0.
    - Using I(profile) will override I(aws_access_key), I(aws_secret_key) and I(security_token)
      and support for passing them at the same time as I(profile) has been deprecated.
    - I(aws_access_key), I(aws_secret_key) and I(security_token) will be made mutually
      exclusive with I(profile) after 2022-06-01.
    type: str

asg_name:
    description:
    - Name of the associated autoscaling group.
    - Required if I(state) is C(present).
    type: str

cooldown:
    description:
    - The minimum period of time (in seconds) between which autoscaling actions can take
      place.
    - Only used when I(policy_type) is C(SimpleScaling).
    type: int

aws_config:
    description:
    - A dictionary to modify the botocore configuration.
    - Parameters can be found at U(https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config).
    - Only the 'user_agent' key is used for boto modules. See U(http://boto.cloudhackers.com/en/latest/boto_config_tut.html#boto)
      for more boto configuration.
    type: dict

policy_type:
    choices:
    - StepScaling
    - SimpleScaling
    - TargetTrackingScaling
    default: SimpleScaling
    description:
    - Auto scaling adjustment policy.
    type: str

aws_ca_bundle:
    description:
    - The location of a CA Bundle to use when validating SSL certificates.
    - Only used for boto3 based modules.
    - 'Note: The CA Bundle is read ''module'' side and may need to be explicitly copied
      from the controller if not run locally.'
    type: path

aws_access_key:
    aliases:
    - ec2_access_key
    - access_key
    description:
    - AWS access key. If not set then the value of the AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY
      or EC2_ACCESS_KEY environment variable is used.
    - If I(profile) is set this parameter is ignored.
    - Passing the I(aws_access_key) and I(profile) options at the same time has been deprecated
      and the options will be made mutually exclusive after 2022-06-01.
    type: str

aws_secret_key:
    aliases:
    - ec2_secret_key
    - secret_key
    description:
    - AWS secret key. If not set then the value of the AWS_SECRET_ACCESS_KEY, AWS_SECRET_KEY,
      or EC2_SECRET_KEY environment variable is used.
    - If I(profile) is set this parameter is ignored.
    - Passing the I(aws_secret_key) and I(profile) options at the same time has been deprecated
      and the options will be made mutually exclusive after 2022-06-01.
    type: str

security_token:
    aliases:
    - aws_security_token
    - access_token
    description:
    - AWS STS security token. If not set then the value of the AWS_SECURITY_TOKEN or EC2_SECURITY_TOKEN
      environment variable is used.
    - If I(profile) is set this parameter is ignored.
    - Passing the I(security_token) and I(profile) options at the same time has been deprecated
      and the options will be made mutually exclusive after 2022-06-01.
    type: str

validate_certs:
    default: true
    description:
    - When set to "no", SSL certificates will not be validated for boto versions >= 2.6.0.
    type: bool

adjustment_type:
    choices:
    - ChangeInCapacity
    - ExactCapacity
    - PercentChangeInCapacity
    description:
    - The type of change in capacity of the autoscaling group.
    - Required if I(state) is C(present).
    type: str

step_adjustments:
    description:
    - list of dicts containing I(lower_bound), I(upper_bound) and I(scaling_adjustment)
    - Intervals must not overlap or have a gap between them.
    - At most, one item can have an undefined I(lower_bound). If any item has a negative
      lower_bound, then there must be a step adjustment with an undefined I(lower_bound).
    - At most, one item can have an undefined I(upper_bound). If any item has a positive
      upper_bound, then there must be a step adjustment with an undefined I(upper_bound).
    - The bounds are the amount over the alarm threshold at which the adjustment will
      trigger. This means that for an alarm threshold of 50, triggering at 75 requires
      a lower bound of 25. See U(http://docs.aws.amazon.com/AutoScaling/latest/APIReference/API_StepAdjustment.html).
    elements: dict
    suboptions:
      lower_bound:
        description:
        - The lower bound for the difference between the alarm threshold and the CloudWatch
          metric.
        type: int
      scaling_adjustment:
        description:
        - The amount by which to scale.
        required: true
        type: int
      upper_bound:
        description:
        - The upper bound for the difference between the alarm threshold and the CloudWatch
          metric.
        type: int
    type: list

metric_aggregation:
    choices:
    - Minimum
    - Maximum
    - Average
    default: Average
    description:
    - The aggregation type for the CloudWatch metrics.
    - Only used when I(policy_type) is not C(SimpleScaling).
    type: str

scaling_adjustment:
    description:
    - The amount by which the autoscaling group is adjusted by the policy.
    - A negative number has the effect of scaling down the ASG.
    - Units are numbers of instances for C(ExactCapacity) or C(ChangeInCapacity) or percent
      of existing instances for C(PercentChangeInCapacity).
    - Required when I(policy_type) is C(SimpleScaling).
    type: int

min_adjustment_step:
    description:
    - Minimum amount of adjustment when policy is triggered.
    - Only used when I(adjustment_type) is C(PercentChangeInCapacity).
    type: int

target_tracking_config:
    description:
    - Allows you to specify a I(target_tracking_config) for autoscaling policies in AWS.
    - I(target_tracking_config) can accept nested dicts for I(customized_metric_spec)
      or I(predefined_metric_spec). Each specification aligns with their boto3 equivalent.
    - Required when I(TargetTrackingScaling) policy is specified.
    suboptions:
      customized_metric_spec:
        description:
        - Specify a dict will be passed in as a call for C(TargetTrackingConfiguration).
        suboptions:
          dimensions:
            description:
            - The dimensions of the metric. The element of the list should be a dict.
            elements: dict
            type: list
          metric_name:
            description:
            - The name of the metric.
            required: true
            type: str
          namespace:
            description:
            - The namespace of the metric.
            required: true
            type: str
          statistic:
            choices:
            - Average
            - Minimum
            - Maximum
            - SampleCount
            - Sum
            description:
            - The statistic of the metric.
            required: true
            type: str
          unit:
            description:
            - The unit of the metric. Reference AmazonCloudWatch API for valid Units.
            type: str
        type: dict
      disable_scalein:
        description:
        - Indicate whether scaling in by the target tracking scaling policy is disabled.
        type: bool
      predefined_metric_spec:
        description:
        - Specify a dict will be passed in as a call for I(TargetTrackingConfiguration).
        suboptions:
          predefined_metric_type:
            choices:
            - ASGAverageCPUUtilization
            - ASGAverageNetworkIn
            - ASGAverageNetworkOut
            - ALBRequestCountPerTarget
            description:
            - Required if C(predefined_metric_spec) is used.
            required: true
            type: str
          resource_label:
            description:
            - Uniquely identifies a specific ALB target group from which to determine
              the average request count served by your Auto Scaling group.
            - You can't specify a resource label unless the target group is attached to
              the Auto Scaling group.
            type: str
        type: dict
      target_value:
        description:
        - Specify a float number for target utilization.
        - Required when I(target_tracking_config) is specified.
        required: true
        type: float
    type: dict
    version_added: 4.1.0
    version_added_collection: community.aws

estimated_instance_warmup:
    description:
    - The estimated time, in seconds, until a newly launched instance can contribute to
      the CloudWatch metrics.
    type: int

debug_botocore_endpoint_logs:
    default: 'no'
    description:
    - Use a botocore.endpoint logger to parse the unique (rather than total) "resource:action"
      API calls made during a task, outputing the set to the resource_actions key in the
      task results. Use the aws_resource_action callback to output to total list made
      during a playbook. The ANSIBLE_DEBUG_BOTOCORE_LOGS environment variable may also
      be used.
    type: bool

Outputs

adjustment_type:
  description: Scaling policy adjustment type
  returned: always
  sample: PercentChangeInCapacity
  type: str
alarms:
  contains:
    alarm_arn:
      description: ARN of the Cloudwatch alarm
      returned: always
      sample: arn:aws:cloudwatch:us-east-2:1234567890:alarm:cpu-very-high
      type: str
    alarm_name:
      description: name of the Cloudwatch alarm
      returned: always
      sample: cpu-very-high
      type: str
  description: Cloudwatch alarms related to the policy
  returned: always
  type: complex
arn:
  description: ARN of the scaling policy. Provided for backward compatibility, value
    is the same as I(policy_arn)
  returned: always
  sample: arn:aws:autoscaling:us-east-2:123456789012:scalingPolicy:59e37526-bd27-42cf-adca-5cd3d90bc3b9:autoScalingGroupName/app-asg:policyName/app-policy
  type: str
as_name:
  description: Auto Scaling Group name. Provided for backward compatibility, value
    is the same as I(auto_scaling_group_name)
  returned: always
  sample: app-asg
  type: str
auto_scaling_group_name:
  description: Name of Auto Scaling Group
  returned: always
  sample: app-asg
  type: str
metric_aggregation_type:
  description: Method used to aggregate metrics
  returned: when I(policy_type) is C(StepScaling)
  sample: Maximum
  type: str
name:
  description: Name of the scaling policy. Provided for backward compatibility, value
    is the same as I(policy_name)
  returned: always
  sample: app-policy
  type: str
policy_arn:
  description: ARN of scaling policy.
  returned: always
  sample: arn:aws:autoscaling:us-east-2:123456789012:scalingPolicy:59e37526-bd27-42cf-adca-5cd3d90bc3b9:autoScalingGroupName/app-asg:policyName/app-policy
  type: str
policy_name:
  description: Name of scaling policy
  returned: always
  sample: app-policy
  type: str
policy_type:
  description: Type of auto scaling policy
  returned: always
  sample: StepScaling
  type: str
scaling_adjustment:
  description: Adjustment to make when alarm is triggered
  returned: When I(policy_type) is C(SimpleScaling)
  sample: 1
  type: int
step_adjustments:
  contains:
    metric_interval_lower_bound:
      description: Lower bound for metric interval
      returned: if step has a lower bound
      sample: 20.0
      type: float
    metric_interval_upper_bound:
      description: Upper bound for metric interval
      returned: if step has an upper bound
      sample: 40.0
      type: float
    scaling_adjustment:
      description: Adjustment to make if this step is reached
      returned: always
      sample: 50
      type: int
  description: List of step adjustments
  returned: always
  type: complex