Created
November 1, 2016 01:22
-
-
Save peterbsmyth/4d1a85dd8c87e50d4fd9531bb5ecf007 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
# roles/auto-scaling/tasks/main.yml | |
- name: Retrieve current Auto Scaling Group properties | |
command: "aws --region {{ region }} autoscaling describe-auto-scaling-groups --auto-scaling-group-names webapp" | |
register: asg_properties_result | |
- name: Set asg_properties variable from JSON output if the Auto Scaling Group already exists | |
set_fact: | |
asg_properties: "{{ (asg_properties_result.stdout | from_json).AutoScalingGroups[0] }}" | |
when: (asg_properties_result.stdout | from_json).AutoScalingGroups | count | |
- name: Configure Auto Scaling Group and perform rolling deploy | |
ec2_asg: | |
region: "{{ region }}" | |
name: webapp | |
launch_config_name: Launch | |
availability_zones: ['us-west-2a', 'us-west-2b', 'us-west-2c'] | |
health_check_type: ELB | |
health_check_period: 300 | |
desired_capacity: 2 | |
# desired_capacity: "{{ asg_properties.DesiredCapacity | default(2) }}" | |
# ^^^ There is a bug in ansible 2.1.2.0 causing this line to fail | |
replace_all_instances: yes | |
replace_batch_size: 1 | |
# replace_batch_size: "{{ (asg_properties.DesiredCapacity | default(2) / 4) | round(0, 'ceil') | int }}" | |
# ^^^ There is a bug in ansible 2.1.2.0 causing this line to fail | |
min_size: 2 | |
max_size: 10 | |
load_balancers: | |
- webapp | |
state: present | |
register: asg_result | |
- name: Configure Scaling Policies | |
ec2_scaling_policy: | |
region: "{{ region }}" | |
name: "{{ item.name }}" | |
asg_name: webapp | |
state: present | |
adjustment_type: "{{ item.adjustment_type }}" | |
min_adjustment_step: "{{ item.min_adjustment_step }}" | |
scaling_adjustment: "{{ item.scaling_adjustment }}" | |
cooldown: "{{ item.cooldown }}" | |
with_items: | |
- name: "Increase Group Size" | |
adjustment_type: "ChangeInCapacity" | |
scaling_adjustment: +1 | |
min_adjustment_step: 1 | |
cooldown: 180 | |
- name: "Decrease Group Size" | |
adjustment_type: "ChangeInCapacity" | |
scaling_adjustment: -1 | |
min_adjustment_step: 1 | |
cooldown: 300 | |
register: sp_result | |
- name: Debug | |
debug: var=asg_name | |
- name: Determine Metric Alarm configuration | |
set_fact: | |
metric_alarms: | |
- name: "{{ asg_name }}-ScaleUp" | |
comparison: ">=" | |
threshold: 50.0 | |
alarm_actions: | |
- "{{ sp_result.results[0].arn }}" | |
- name: "{{ asg_name }}-ScaleDown" | |
comparison: "<=" | |
threshold: 20.0 | |
alarm_actions: | |
- "{{ sp_result.results[1].arn }}" | |
- name: Configure Metric Alarms and link to Scaling Policies | |
ec2_metric_alarm: | |
region: "{{ region }}" | |
name: "{{ item.name }}" | |
state: present | |
metric: "CPUUtilization" | |
namespace: "AWS/EC2" | |
statistic: "Average" | |
comparison: "{{ item.comparison }}" | |
threshold: "{{ item.threshold }}" | |
period: 60 | |
evaluation_periods: 5 | |
unit: "Percent" | |
dimensions: | |
AutoScalingGroupName: "{{ asg_name }}" | |
alarm_actions: "{{ item.alarm_actions }}" | |
with_items: "{{metric_alarms}}" | |
# when: "{{ max_size }}" > 1 | |
register: ma_result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment