255 lines
9.4 KiB
YAML
255 lines
9.4 KiB
YAML
---
|
|
# AWS Cleanup Playbook
|
|
# Used to tear down AWS infrastructure
|
|
|
|
- name: Clean up AWS infrastructure
|
|
hosts: localhost
|
|
connection: local
|
|
gather_facts: false
|
|
vars_files:
|
|
- vars.yaml
|
|
vars:
|
|
aws_region: "{{ aws_region | default(aws_region_choices | random) }}"
|
|
cleanup_redirector: "{{ (redirector_name is defined and redirector_name != '') | ternary(true, false) }}"
|
|
cleanup_c2: "{{ (c2_name is defined and c2_name != '') | ternary(true, false) }}"
|
|
confirm_cleanup: "{{ confirm_cleanup | default(true) }}"
|
|
|
|
tasks:
|
|
- name: Validate required AWS credentials
|
|
assert:
|
|
that:
|
|
- aws_access_key is defined and aws_access_key != ""
|
|
- aws_secret_key is defined and aws_secret_key != ""
|
|
fail_msg: "AWS credentials are required. Set aws_access_key and aws_secret_key."
|
|
|
|
- name: Confirm cleanup if required
|
|
pause:
|
|
prompt: "WARNING: This will permanently delete all infrastructure. Type 'yes' to continue"
|
|
register: confirmation
|
|
when: confirm_cleanup
|
|
|
|
- name: Exit if not confirmed
|
|
meta: end_play
|
|
when: confirm_cleanup and confirmation.user_input != 'yes'
|
|
|
|
# Find instance IDs for cleanup
|
|
- name: Find redirector instance ID
|
|
command: >
|
|
aws ec2 describe-instances
|
|
--filters "Name=tag:Name,Values={{ redirector_name }}"
|
|
--query "Reservations[*].Instances[*].InstanceId"
|
|
--output text
|
|
--region {{ aws_region }}
|
|
environment:
|
|
AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
|
|
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
|
|
register: redirector_id_result
|
|
failed_when: false
|
|
changed_when: false
|
|
when: cleanup_redirector
|
|
|
|
- name: Find C2 instance ID
|
|
command: >
|
|
aws ec2 describe-instances
|
|
--filters "Name=tag:Name,Values={{ c2_name }}"
|
|
--query "Reservations[*].Instances[*].InstanceId"
|
|
--output text
|
|
--region {{ aws_region }}
|
|
environment:
|
|
AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
|
|
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
|
|
register: c2_id_result
|
|
failed_when: false
|
|
changed_when: false
|
|
when: cleanup_c2
|
|
|
|
- name: Set instance IDs as facts
|
|
set_fact:
|
|
redirector_instance_id: "{{ redirector_id_result.stdout | trim }}"
|
|
when: cleanup_redirector and redirector_id_result.stdout is defined and redirector_id_result.stdout | trim != ""
|
|
|
|
- name: Set C2 instance ID as fact
|
|
set_fact:
|
|
c2_instance_id: "{{ c2_id_result.stdout | trim }}"
|
|
when: cleanup_c2 and c2_id_result.stdout is defined and c2_id_result.stdout | trim != ""
|
|
|
|
# Find VPCs with specific redirector tag
|
|
- name: Find VPCs with specific redirector tag
|
|
amazon.aws.ec2_vpc_net_info:
|
|
region: "{{ aws_region }}"
|
|
filters:
|
|
"tag:redirector_name": "{{ redirector_name }}"
|
|
register: tagged_vpcs_info
|
|
when: cleanup_redirector | default(false) | bool
|
|
|
|
- name: Display matched VPCs for cleanup
|
|
debug:
|
|
msg: "Found {{ tagged_vpcs_info.vpcs | default([]) | length }} VPCs with tag redirector_name={{ redirector_name }} to clean up"
|
|
when: cleanup_redirector | default(false) | bool and tagged_vpcs_info is defined
|
|
|
|
- name: Clean up each VPC and its dependencies
|
|
block:
|
|
- name: Get all subnets for target VPC
|
|
amazon.aws.ec2_vpc_subnet_info:
|
|
filters:
|
|
vpc-id: "{{ item.id }}"
|
|
"tag:redirector_name": "{{ redirector_name }}"
|
|
region: "{{ aws_region }}"
|
|
register: vpc_subnets
|
|
with_items: "{{ tagged_vpcs_info.vpcs }}"
|
|
|
|
- name: Get all internet gateways for target VPC
|
|
amazon.aws.ec2_vpc_igw_info:
|
|
filters:
|
|
attachment.vpc-id: "{{ item.id }}"
|
|
region: "{{ aws_region }}"
|
|
register: vpc_igws
|
|
with_items: "{{ tagged_vpcs_info.vpcs }}"
|
|
|
|
- name: Get all route tables for target VPC
|
|
amazon.aws.ec2_vpc_route_table_info:
|
|
filters:
|
|
vpc-id: "{{ item.id }}"
|
|
"tag:redirector_name": "{{ redirector_name }}"
|
|
region: "{{ aws_region }}"
|
|
register: vpc_route_tables
|
|
with_items: "{{ tagged_vpcs_info.vpcs }}"
|
|
|
|
- name: Get security groups for target VPC
|
|
amazon.aws.ec2_security_group_info:
|
|
filters:
|
|
vpc-id: "{{ item.id }}"
|
|
group-name: "{{ redirector_name }}-sg"
|
|
region: "{{ aws_region }}"
|
|
register: vpc_security_groups
|
|
with_items: "{{ tagged_vpcs_info.vpcs }}"
|
|
|
|
# Start deleting resources in proper dependency order
|
|
- name: Delete non-main route tables
|
|
amazon.aws.ec2_vpc_route_table:
|
|
route_table_id: "{{ item.route_table_id }}"
|
|
region: "{{ aws_region }}"
|
|
state: absent
|
|
loop: "{{ vpc_route_tables.results | map(attribute='route_tables') | flatten | selectattr('associations', 'defined') | list }}"
|
|
ignore_errors: yes
|
|
|
|
- name: Detach internet gateways
|
|
shell: >
|
|
aws ec2 detach-internet-gateway
|
|
--region {{ aws_region }}
|
|
--internet-gateway-id {{ item.internet_gateway_id }}
|
|
--vpc-id {{ item.attachments[0].vpc_id }}
|
|
environment:
|
|
AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
|
|
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
|
|
loop: "{{ vpc_igws.results | map(attribute='internet_gateways') | flatten | selectattr('attachments', 'defined') | list }}"
|
|
ignore_errors: yes
|
|
|
|
- name: Delete internet gateways
|
|
amazon.aws.ec2_vpc_igw:
|
|
igw_id: "{{ item.internet_gateway_id }}"
|
|
region: "{{ aws_region }}"
|
|
state: absent
|
|
loop: "{{ vpc_igws.results | map(attribute='internet_gateways') | flatten | list }}"
|
|
ignore_errors: yes
|
|
|
|
- name: Delete subnets
|
|
amazon.aws.ec2_vpc_subnet:
|
|
subnet_id: "{{ item.subnet_id }}"
|
|
region: "{{ aws_region }}"
|
|
state: absent
|
|
loop: "{{ vpc_subnets.results | map(attribute='subnets') | flatten | list }}"
|
|
ignore_errors: yes
|
|
|
|
- name: Delete security groups
|
|
amazon.aws.ec2_security_group:
|
|
group_id: "{{ item.group_id }}"
|
|
region: "{{ aws_region }}"
|
|
state: absent
|
|
loop: "{{ vpc_security_groups.results | map(attribute='security_groups') | flatten | list }}"
|
|
ignore_errors: yes
|
|
|
|
- name: Delete VPCs
|
|
amazon.aws.ec2_vpc_net:
|
|
vpc_id: "{{ item.id }}"
|
|
region: "{{ aws_region }}"
|
|
state: absent
|
|
loop: "{{ tagged_vpcs_info.vpcs }}"
|
|
ignore_errors: yes
|
|
when: cleanup_redirector | bool and tagged_vpcs_info is defined and tagged_vpcs_info.vpcs | default([]) | length > 0
|
|
|
|
# Instance, security group, and key pair cleanup
|
|
- name: Terminate redirector instance
|
|
amazon.aws.ec2_instance:
|
|
state: absent
|
|
instance_ids:
|
|
- "{{ redirector_instance_id }}"
|
|
region: "{{ aws_region }}"
|
|
when: cleanup_redirector and redirector_instance_id is defined and redirector_instance_id != ""
|
|
register: redirector_termination
|
|
|
|
- name: Terminate C2 instance
|
|
amazon.aws.ec2_instance:
|
|
state: absent
|
|
instance_ids:
|
|
- "{{ c2_instance_id }}"
|
|
region: "{{ aws_region }}"
|
|
when: cleanup_c2 and c2_instance_id is defined and c2_instance_id != ""
|
|
register: c2_termination
|
|
|
|
- name: Wait for instances to be fully terminated
|
|
pause:
|
|
seconds: 30
|
|
when:
|
|
- (redirector_termination is defined and redirector_termination.changed) or
|
|
(c2_termination is defined and c2_termination.changed)
|
|
|
|
- name: Delete redirector security group
|
|
amazon.aws.ec2_group:
|
|
name: "{{ redirector_name }}-sg"
|
|
state: absent
|
|
region: "{{ aws_region }}"
|
|
ignore_errors: yes
|
|
when: cleanup_redirector and redirector_name is defined and redirector_name != ""
|
|
|
|
- name: Delete C2 security group
|
|
amazon.aws.ec2_group:
|
|
name: "{{ c2_name }}-sg"
|
|
state: absent
|
|
region: "{{ aws_region }}"
|
|
ignore_errors: yes
|
|
when: cleanup_c2 and c2_name is defined and c2_name != ""
|
|
|
|
- name: Delete redirector key pair
|
|
amazon.aws.ec2_key:
|
|
name: "{{ redirector_name }}"
|
|
state: absent
|
|
region: "{{ aws_region }}"
|
|
when: cleanup_redirector and redirector_name is defined and redirector_name != ""
|
|
|
|
- name: Delete C2 key pair
|
|
amazon.aws.ec2_key:
|
|
name: "{{ c2_name }}"
|
|
state: absent
|
|
region: "{{ aws_region }}"
|
|
when: cleanup_c2 and c2_name is defined and c2_name != ""
|
|
|
|
- name: Remove redirector SSH key file
|
|
file:
|
|
path: "~/.ssh/{{ redirector_name }}.pem"
|
|
state: absent
|
|
when: cleanup_redirector and redirector_name is defined and redirector_name != ""
|
|
|
|
- name: Remove C2 SSH key file
|
|
file:
|
|
path: "~/.ssh/{{ c2_name }}.pem"
|
|
state: absent
|
|
when: cleanup_c2 and c2_name is defined and c2_name != ""
|
|
|
|
- name: Cleanup complete message
|
|
debug:
|
|
msg:
|
|
- "AWS infrastructure cleanup complete!"
|
|
- "Resources that were cleaned up:"
|
|
- "{{ cleanup_redirector | ternary('- Redirector instance: ' + redirector_name, '') }}"
|
|
- "{{ cleanup_c2 | ternary('- C2 instance: ' + c2_name, '') }}" |