Skip to content

Commit

Permalink
Change ASG relation from LC to LT
Browse files Browse the repository at this point in the history
  • Loading branch information
MariuszJozwiak authored and Szpadel committed Feb 16, 2023
1 parent ec93a86 commit f45cf22
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 39 deletions.
Binary file removed .DS_Store
Binary file not shown.
Binary file removed docs/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions roles/cs.aws-autoscaling/tasks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
resource_tags: "{{ aws_tags_default | combine(autoscaling_instance_tags) }}"
# This has zero costs on current-gen instances
ebs_optimized: true
vars:
aws_ec2_user_launch_script: "{{ autoscaling_instance_start_script }}"

- name: Create AutoScaling Group
ec2_asg:
Expand Down
3 changes: 1 addition & 2 deletions roles/cs.aws-ec2-cleanup/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
aws_ec2_cleanup_lcs_to_keep: 10
aws_ec2_cleanup_lc_name_prefixes: []
aws_ec2_cleanup_lt_to_keep: 10
26 changes: 25 additions & 1 deletion roles/cs.aws-ec2-cleanup/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
vars:
query: "[?DefaultVersion]"
loop: "{{ _launch_template_version_output.stdout_lines | map('from_json') | list }}"
when: aws_ec2_cleanup_lt_to_keep > 0

- name: Keep last N versions
set_fact:
_to_keep: "{{ _to_keep + item.LaunchTemplateVersions | json_query(query) }}"
vars:
query: "[:{{aws_ec2_cleanup_lt_to_keep}}]"
loop: "{{ _launch_template_version_output.stdout_lines | map('from_json') | list }}"
when: aws_ec2_cleanup_lt_to_keep > 0

- name: Gather amis to keep
set_fact:
Expand All @@ -72,7 +74,7 @@
_version:
name: "{{ item.LaunchTemplateName }}"
version: "{{ item.VersionNumber }}"
when: item not in _to_keep
when: item not in _to_keep and aws_ec2_cleanup_lt_to_keep > 0
loop: "{{ _launch_template_version_output.stdout_lines | map('from_json') | map(attribute='LaunchTemplateVersions') | flatten }}"

- name: Get list of App AMIs
Expand Down Expand Up @@ -115,3 +117,25 @@
{% for item in _amis_to_remove %}
{{ item }}
{% endfor %}
- name: Remove obsoled LT version
command:
cmd: "aws ec2 delete-launch-template-versions --launch-template-name {{ item.name | quote }} --versions {{ item.version }} --region {{ aws_region }}"
loop: "{{ _remove_template_version }}"
when: aws_ec2_cleanup_lt_to_keep > 0

- name: Delete whole LT
ec2_launch_template:
name: "{{item.name | quote }}"
state: absent
region: "{{ aws_region }}"
loop: "{{ _remove_template_version }}"
when: aws_ec2_cleanup_lt_to_keep == 0

- name : Remove obsoled AMIs
ec2_ami:
image_id: "{{ item }}"
state: absent
delete_snapshot: True
region: "{{ aws_region }}"
loop: "{{ _amis_to_remove }}"
48 changes: 18 additions & 30 deletions site.maintenance.aws-remove-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
register: _aws_asgs
- name: Set list of ASG to drop
set_fact:
_aws_asgs_to_remove: "{{ _aws_asgs | json_query('results[].{ name: auto_scaling_group_name, lc: launch_configuration_name}')}}"
_aws_asgs_to_remove: "{{ _aws_asgs | json_query('results[].{ name: auto_scaling_group_name } ') }}"
- name: Remove ASGs
ec2_asg:
name: "{{ item.name }}"
launch_config_name: "{{ item.lc }}"
region: "{{ aws_region }}"
state: absent
with_items: "{{ _aws_asgs_to_remove }}"
when: not dry_run_mode
when: remove_asg

- name: Remove other EC2 Servers
Expand All @@ -44,8 +44,11 @@
- name: Set list of EC2 instances to drop
set_fact:
_aws_ec2_to_remove: "{{ _aws_ec2|json_query('instances[].instance_id')}}"
- name: Show instances to terminate
debug:
msg: "{{ _aws_ec2_to_remove }}"
- name: Terminate instances
when: _aws_ec2_to_remove | length > 0
when: (_aws_ec2_to_remove | length > 0) and not dry_run_mode
ec2:
instance_ids: "{{ _aws_ec2_to_remove }}"
region: "{{ aws_region }}"
Expand Down Expand Up @@ -78,7 +81,7 @@
aws_rds_instance_id: "{{ (_aws_rds_instances | first).db_instance_identifier }}"
when: _aws_rds_instances | length > 0
- name: Terminate RDS instance
when: _aws_rds_instance_id == aws_rds_instance_name
when: _aws_rds_instance_id == aws_rds_instance_name and not dry_run_mode
rds:
command: delete
instance_name: "{{ aws_rds_instance_name }}"
Expand Down Expand Up @@ -116,6 +119,7 @@
region: "{{ aws_region }}"
state: absent
with_items: "{{ _aws_efs_to_remove }}"
when: not dry_run_mode
when: remove_efs

- name: Remove Lambdas
Expand All @@ -128,22 +132,9 @@
with_items:
- "handleAutoscalingEvent-{{ mageops_app_name }}"
- "updateVarnishBackends-{{ mageops_app_name }}"
- name: Find ENIs
ec2_eni_facts:
region: "{{ aws_region }}"
register: _aws_fact
- name: Set list of ENIs to drop
set_fact:
_aws_eni_to_remove: "{{ _aws_fact | json_query(query) }}"
vars:
query: "network_interfaces[?vpc_id=='{{ _aws_vpc_id }}'].id"
- name: Remove ENI for Lambda
ec2_eni:
state: absent
region: "{{ aws_region }}"
eni_id: "{{ item }}"
force_detach: true
with_items: "{{ _aws_eni_to_remove }}"
- "handleNodeCoordinatorAutoscalingEvent-{{ mageops_app_name }}"
- "handleVarnishAutoscalingEvent-{{ mageops_app_name }}"
when: not dry_run_mode
when: remove_lambda

- name: Remove Security Groups
Expand All @@ -168,12 +159,15 @@
name: "{{ item.name }}"
description: "To be removed"
with_items: "{{ _aws_groups_to_remove }}"
when: not dry_run_mode
- name: Drop Security Groups
ec2_group:
region: "{{ aws_region }}"
state: absent
name: "{{ item.name }}"
with_items: "{{ _aws_groups_to_remove }}"
ignore_errors: true
when: not dry_run_mode
when: remove_sg

- name: Remove S3 Buckets
Expand All @@ -187,20 +181,13 @@
with_items:
- "{{ aws_s3_media_bucket }}"
- "{{ aws_s3_secret_bucket }}"
when: remove_s3
when: remove_s3 and not dry_run_mode

roles:
- role: cs.aws-ec2-cleanup
aws_ec2_cleanup_lcs_to_keep: 0
aws_ec2_cleanup_lc_name_prefixes:
- "{{ mageops_app_name }}-app-"
- "{{ mageops_app_name }}-extra-app-"
when: remove_asg
aws_ec2_cleanup_lt_to_keep: 0
when: remove_asg and not dry_run_mode

vars_files:
- vars/app/credentials.yml
- vars/app/env.yml
- vars/aws/env.yml
vars:
remove_asg: yes
remove_ec2: yes
Expand All @@ -210,3 +197,4 @@
remove_sg: yes
remove_vol: no
remove_s3: no
dry_run_mode : no
6 changes: 2 additions & 4 deletions site.maintenance.aws-start.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
- name: Scale up ASG
ec2_asg:
name: "{{ item.name }}"
launch_config_name: "{{ item.lc }}"
region: "{{ aws_region }}"
state: present
desired_capacity: "{{ aws_app_asg_desired_capacity | default(1) }}"
Expand All @@ -48,7 +47,7 @@
wait_timeout: 600
with_items: "{{ start_asgs }}"
vars:
start_asgs: "{{ start_asg_facts | json_query('results[].{ name: auto_scaling_group_name, lc: launch_configuration_name}') }}"
start_asgs: "{{ start_asg_facts | json_query('results[].{ name: auto_scaling_group_name } ') }}"

- name: Scale up Extra App Node ASGs
when: start_asg and (aws_extra_app_asg_enable or magento_aws_ondemand_import_instance_enable)
Expand All @@ -62,7 +61,6 @@
- name: Scale up ASG
ec2_asg:
name: "{{ item.name }}"
launch_config_name: "{{ item.lc }}"
region: "{{ aws_region }}"
state: present
desired_capacity: "{{ aws_extra_app_asg_desired_capacity | default(0) }}"
Expand All @@ -71,7 +69,7 @@
wait_timeout: 600
with_items: "{{ start_asgs }}"
vars:
start_asgs: "{{ start_asg_facts | json_query('results[].{ name: auto_scaling_group_name, lc: launch_configuration_name}') }}"
start_asgs: "{{ start_asg_facts | json_query('results[].{ name: auto_scaling_group_name } ') }}"

- name: Start RDS instance
rds_instance:
Expand Down
3 changes: 1 addition & 2 deletions site.maintenance.aws-stop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
- name: Scale down ASG to zero
ec2_asg:
name: "{{ item.name }}"
launch_config_name: "{{ item.lc }}"
region: "{{ aws_region }}"
state: present
desired_capacity: 0
min_size: 0
wait_for_instances: no
with_items: "{{ stop_asgs }}"
vars:
stop_asgs: "{{ stop_asg_facts | json_query('results[].{ name: auto_scaling_group_name, lc: launch_configuration_name}') }}"
stop_asgs: "{{ stop_asg_facts | json_query('results[].{ name: auto_scaling_group_name }') }}"

- name: Stop standalone EC2 instances
when: stop_ec2
Expand Down

0 comments on commit f45cf22

Please sign in to comment.