官术网_书友最值得收藏!

How to do it...

We will be using the ec2_lc module for creating launch configurations, the ec2_asg module for creating auto scaling groups, the ec2_scaling_policy module to define scaling policies and the ec2_metric_alarm module for creating a metric alarm to take actions like scaling up EC2 instances.

  1. In this task we will be creating an auto scaling group with a scale-up policy piped with a CloudWatch alarm configured with CPU utilization metrics; and if it goes above a defined threshold, it will scale up EC2 instances in auto scaling groups:
- name: Create Launch Configuration
ec2_lc:
region: ""{{ aws_region }}""
aws_access_key: ""{{ access_key }}""
aws_secret_key: ""{{ secret_key }}""
name: my_first_lc
image_id: ""{{ ami_id }}""
key_name: my_first_key
instance_type: ""{{ instance_type }}""
security_groups: ""{{ my_first_sg.group_id }}""
instance_monitoring: yes

In the preceding task, we created a launch configuration which would be used to create new instances in case of a scale-up event. We used the same parameters that we used to create an EC2 instance; that is an AMI ID, a key pair name, a security group and instance ID, and a region. We used one additional parameter (instance_monitoring) which will enable CloudWatch metric monitoring such as CPU utilization metric.

  1. Let us create the auto scaling group next:
- name: Create Auto Scaling group
ec2_asg:
name: my_first_asg
region: "{{ aws_region }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
load_balancers:
- first-elb
launch_config_name: my_first_lc
min_size: 1
max_size: 5
desired_capacity: 3
vpc_zone_identifier:
- "{{ my_private_subnet.subnet.id }}"
tags:
- environment: test

In this task, we are creating an auto scaling group for our EC2 instances. This task will need the launch configuration name, the min/max count of instances we want to keep in our auto scaling group, the desired capacity we want to keep in our auto scaling group, the VPC subnet where we want to add more EC2 instances, and the tags as input parameters. We should note here that we are using the subnet ID registered while creating the VPC subnet.

  1. Next, we will create a scaling policy:
- name: Configure Scaling Policies (scale-up)
ec2_scaling_policy:
region: "{{ aws_region }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
name: scale-up policy
asg_name: my_first_asg
state: present
adjustment_type: ChangeInCapacity
min_adjustment_step: 1
scaling_adjustment: +1
register: scale_up_policy

Here we have defined an auto scaling policy for scaling up EC2 instances. This task requires the name of the auto scaling group, the amount by which the auto scaling group is adjusted (scaling_adjustment), the minimum amount type by which the auto scaling group is adjusted by the policy (min_adjustment), and the type of change in capacity (adjustment_type), which can be set as ChangeInCapacity, ExactCapacity, or PercentageChangeInCapacity.

  1. Now, we will configure CloudWatch alarm:
- name: Configure CloudWatch Metric for Scaling Up Policy
ec2_metric_alarm:
state: present
region: "{{ aws_region }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
name: "scale-up-metric"
description: "This alarm notify auto scaling policy to step up instance"
metric: "CPUUtilization"
namespace: "AWS/EC2"
statistic: "Average"
comparison: ">="
threshold: 60.0
unit: "Percent"
period: 300
evaluation_periods: 3
dimensions:
AutoScalingGroupName: "my_first_asg"
alarm_actions:
- "{{ scale_up_policy.arn }}"

In the preceding task, we are creating a CloudWatch metric alert which will trigger the scaling policy we defined in the preceding task. This task will need the following parameters:

  • Metric to monitor, which we have selected as CPU utilization.
  • Namespace of that metric, which, in our case, is AWS/EC2 as we are working with our EC2 instances.
  • Aggregation (statistics) we want to perform on the metric. We will be doing the average over data points.
  • Unit of metric. We are using percentage here.
  • Will look at data points for a period 300 seconds.
  • Threshold to trigger the alert, which is 60.0 % of CPU utilization.
  • Evaluation period of 3, which means, over a period of 300 seconds, if the average CPU utilization is greater than 60% for 3 consecutive runs, it will trigger an alarm.
  • Action required if the alarm is triggered. We are notifying our auto scaling policy defined in the previous task as an alarm_actions.
主站蜘蛛池模板: 罗山县| 金溪县| 津南区| 印江| 淮滨县| 青铜峡市| 安福县| 青海省| 渝中区| 宝兴县| 盐津县| 丰都县| 富锦市| 北安市| 辽阳县| 鲁甸县| 临颍县| 佛冈县| 囊谦县| 柘城县| 延安市| 宝山区| 六枝特区| 青河县| 土默特左旗| 南城县| 惠水县| 莆田市| 揭东县| 通化市| 民县| 徐闻县| 蒙城县| 仪陇县| 楚雄市| 井冈山市| 昭通市| 瑞丽市| 那曲县| 彰化市| 金川县|