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

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.
主站蜘蛛池模板: 河曲县| 车致| 师宗县| 咸丰县| 阿图什市| 崇阳县| 长治县| 博野县| 东宁县| 攀枝花市| 修水县| 和田市| 邳州市| 雅江县| 海兴县| 锦屏县| 红原县| 廉江市| 阳朔县| 开阳县| 和田市| 亚东县| 萨迦县| 威海市| 尉氏县| 繁昌县| 百色市| 龙州县| 台山市| 金华市| 天等县| 宝丰县| 灵宝市| 瓮安县| 龙口市| 崇信县| 扶沟县| 巩留县| 讷河市| 延安市| 安徽省|