Skip to content

Commit 4ad3c54

Browse files
committed
fix: playbook webapp - delete resources
1 parent 91f82fb commit 4ad3c54

File tree

12 files changed

+99
-77
lines changed

12 files changed

+99
-77
lines changed

playbooks/webapp/tasks/create.yaml

+1-11
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,6 @@
218218
mode: 0400
219219
when: rsa_key is changed
220220

221-
- name: Check if the vm exists
222-
amazon.aws.ec2_instance_info:
223-
filters:
224-
instance-type: "{{ bastion_host_type }}"
225-
key-name: "{{ deploy_flask_app_sshkey_pair_name }}"
226-
vpc-id: "{{ vpc.vpc.id }}"
227-
instance-state-name: running
228-
register: vm_result
229-
230221
- name: Ensure IAM instance role exists
231222
amazon.aws.iam_role:
232223
name: "{{ ec2_iam_role_name }}"
@@ -237,7 +228,6 @@
237228
register: role_output
238229

239230
- name: Create a virtual machine
240-
when: vm_result.instances | length == 0
241231
amazon.aws.ec2_instance:
242232
name: "{{ deploy_flask_app_bastion_host_name }}"
243233
instance_type: "{{ bastion_host_type }}"
@@ -254,7 +244,7 @@
254244
- "{{ secgroup.group_id }}"
255245
user_data: |
256246
#!/bin/bash
257-
yum install -y python3 python-virtualenv sshpass netcat
247+
yum install -y python3 python-virtualenv sshpass netcat ansible
258248
wait: true
259249
state: started
260250
register: vm_result

playbooks/webapp/tasks/delete.yaml

+47-60
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,33 @@
2222
ansible.builtin.set_fact:
2323
vpc_id: "{{ vpc.vpcs.0.vpc_id }}"
2424

25-
- name: Get bastion instance info
25+
# Delete Load balancer
26+
- name: List Load balancer(s) from VPC
27+
community.aws.elb_classic_lb_info:
28+
register: load_balancers
29+
30+
- name: Delete load balancer(s)
31+
amazon.aws.elb_classic_lb:
32+
name: "{{ item }}"
33+
wait: true
34+
state: absent
35+
with_items: "{{ load_balancers.elbs | selectattr('vpc_id', 'equalto', vpc_id) | map(attribute='load_balancer_name') | list }}"
36+
37+
# Delete EC2 instances
38+
- name: Get EC2 instance info
2639
amazon.aws.ec2_instance_info:
2740
filters:
28-
instance-type: "{{ bastion_host_type }}"
29-
key-name: "{{ deploy_flask_app_sshkey_pair_name }}"
3041
vpc-id: "{{ vpc_id }}"
31-
instance-state-name: running
32-
register: bastion
33-
34-
- name: Delete EC2 instances with dependant Resources
35-
when: bastion.instances | length == 1
36-
block:
37-
- name: Set 'instance_host_name' variable
38-
ansible.builtin.set_fact:
39-
instance_host_name: "{{ bastion.instances.0.public_dns_name | split('.') | first }}"
40-
41-
- name: Delete workers key pair
42-
amazon.aws.ec2_key:
43-
name: "{{ instance_host_name }}-key"
44-
state: absent
45-
46-
- name: Delete load balancer
47-
amazon.aws.elb_classic_lb:
48-
name: "{{ instance_host_name }}-lb"
49-
wait: true
50-
state: absent
51-
52-
- name: List workers
53-
amazon.aws.ec2_instance_info:
54-
filters:
55-
tag:Name: "{{ instance_host_name }}-workers"
56-
instance-state-name: running
57-
register: running
58-
59-
- name: Delete workers
60-
when: running.instances | length != 0
61-
amazon.aws.ec2_instance:
62-
instance_ids: "{{ running.instances | map(attribute='instance_id') | list }}"
63-
wait: true
64-
state: terminated
65-
66-
- name: Delete bastion host
67-
amazon.aws.ec2_instance:
68-
instance_ids:
69-
- "{{ bastion.instances.0.instance_id }}"
70-
wait: true
71-
state: terminated
42+
register: ec2_instances
43+
44+
- name: Delete ec2 instances from VPC
45+
amazon.aws.ec2_instance:
46+
instance_ids: "{{ ec2_instances.instances | map(attribute='instance_id') | list }}"
47+
wait: true
48+
state: terminated
49+
when: ec2_instances.instances | length > 0
7250

51+
# Delete RDS instance
7352
- name: Delete RDS instance
7453
amazon.aws.rds_instance:
7554
state: absent
@@ -87,19 +66,7 @@
8766
name: "{{ rds_subnet_group_name }}"
8867
state: absent
8968

90-
- name: List Security group from VPC
91-
amazon.aws.ec2_security_group_info:
92-
filters:
93-
vpc-id: "{{ vpc_id }}"
94-
tag:prefix: "{{ resource_prefix }}"
95-
register: secgroups
96-
97-
- name: Delete security groups
98-
amazon.aws.ec2_security_group:
99-
state: absent
100-
group_id: "{{ item }}"
101-
with_items: "{{ secgroups.security_groups | map(attribute='group_id') | list }}"
102-
69+
# Delete VPC route table
10370
- name: List routes table from VPC
10471
amazon.aws.ec2_vpc_route_table_info:
10572
filters:
@@ -115,6 +82,7 @@
11582
state: absent
11683
with_items: "{{ route_table.route_tables | map(attribute='id') | list }}"
11784

85+
# Delete NAT Gateway
11886
- name: Get NAT gateway
11987
amazon.aws.ec2_vpc_nat_gateway_info:
12088
filters:
@@ -128,20 +96,39 @@
12896
wait: true
12997
with_items: "{{ nat_gw.result | map(attribute='nat_gateway_id') | list }}"
13098

99+
# Delete Internet gateway
131100
- name: Delete internet gateway
132101
amazon.aws.ec2_vpc_igw:
133102
vpc_id: "{{ vpc_id }}"
134103
state: absent
135104

105+
# Delete Subnets
106+
- name: List Subnets from VPC
107+
amazon.aws.ec2_vpc_subnet_info:
108+
filters:
109+
vpc-id: "{{ vpc_id }}"
110+
register: vpc_subnets
111+
136112
- name: Delete subnets
137113
amazon.aws.ec2_vpc_subnet:
138114
cidr: "{{ item }}"
139115
state: absent
140116
vpc_id: "{{ vpc_id }}"
141-
with_items: "{{ subnet_cidr }}"
117+
with_items: "{{ vpc_subnets.subnets | map(attribute='cidr_block') | list }}"
118+
119+
# Delete Security groups
120+
- name: List Security group from VPC
121+
amazon.aws.ec2_security_group_info:
122+
filters:
123+
vpc-id: "{{ vpc_id }}"
124+
register: secgroups
125+
126+
- name: Delete security groups
127+
amazon.aws.ec2_security_group:
128+
state: absent
129+
group_id: "{{ item }}"
130+
with_items: "{{ secgroups.security_groups | rejectattr('group_name', 'equalto', 'default') | map(attribute='group_id') | list }}"
142131

143-
# As ec2_vpc_route_table can't delete route table, the vpc still has dependencies and cannot be deleted.
144-
# You need to do it delete it manually using either the console or the cli.
145132
- name: Delete VPC
146133
amazon.aws.ec2_vpc_net:
147134
name: "{{ vpc_name }}"

playbooks/webapp/vars/main.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ resource_tags:
1212
prefix: "{{ resource_prefix }}"
1313
operation: create
1414

15-
image_filter: Fedora-Cloud-Base-35-*gp2-0
15+
image_filter: Fedora-Cloud-Base-38-*
1616
public_secgroup_name: "{{ resource_prefix }}-sg"
1717
ec2_iam_role_name: "{{ resource_prefix }}-role"
1818
rds_subnet_group_name: "{{ resource_prefix }}-rds-sg"
@@ -23,15 +23,15 @@ rds_instance_class: db.m6g.large
2323
rds_instance_name: mysampledb123
2424
rds_engine: postgres
2525
rds_engine_version: "14.8"
26-
bastion_host_type: t2.xlarge
26+
bastion_host_type: t3.micro
2727
bastion_host_venv_path: ~/env
2828
rds_listening_port: 5432
2929

3030
# Variables for the deploy_flask_app role
3131
deploy_flask_app_sshkey_pair_name: "{{ resource_prefix }}-key"
3232
deploy_flask_app_bastion_host_name: "{{ resource_prefix }}-bastion"
3333
deploy_flask_app_bastion_host_username: fedora
34-
deploy_flask_app_workers_instance_type: t2.xlarge
34+
deploy_flask_app_workers_instance_type: t3.micro
3535
deploy_flask_app_workers_user_name: fedora
3636
deploy_flask_app_number_of_workers: 2
3737
deploy_flask_app_listening_port: 5000

roles/deploy_flask_app/tasks/main.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818

1919
- name: Start application container into workers
2020
ansible.builtin.include_tasks: start_containers.yaml
21+
22+
- name: Display application URL
23+
ansible.builtin.debug:
24+
msg: "Application accessible at http://{{ deploy_flask_app_lb_result.elb.dns_name }}:{{ deploy_flask_app_listening_port }}"
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
- name: Delete temporary RSA key directory
2+
- name: Delete RSA key directory
33
ansible.builtin.file:
44
state: absent
5-
path: "{{ setup_rsa_keys__tmpdir }}"
5+
path: "{{ setup_rsa_keys__path }}"
66
ignore_errors: true

tests/integration/targets/setup_rsa_keys/tasks/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ansible.builtin.file:
2020
path: "{{ setup_rsa_keys__path }}"
2121
state: directory
22+
notify: 'Delete RSA key directory'
2223

2324
- name: Generate RSA keys
2425
community.crypto.openssh_keypair:
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
cloud/aws
22
role/deploy_flask_app
33
time=35m
4+
unstable

tests/integration/targets/test_deploy_flask_app/tasks/delete.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
state: absent
7777
with_items: "{{ route_table.route_tables | map(attribute='id') | list }}"
7878

79-
# Delete VPC route table
79+
# Delete NAT Gateway
8080
- name: Get NAT gateway
8181
amazon.aws.ec2_vpc_nat_gateway_info:
8282
filters:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
time=35m
2+
cloud/aws
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
- hosts: localhost
3+
connection: local
4+
gather_facts: false
5+
tasks:
6+
- name: Write access key to file we can source
7+
ansible.builtin.copy:
8+
dest: access_key.sh
9+
content: |
10+
export AWS_ACCESS_KEY_ID="{{ aws_access_key }}"
11+
export AWS_SECRET_ACCESS_KEY="{{ aws_secret_key }}"
12+
export AWS_REGION="{{ aws_region }}"
13+
export AWS_SECURITY_TOKEN="{{ aws_security_token }}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
# generate inventory with access_key provided through a templated variable
4+
ansible-playbook create_aws_credentials.yml "$@"
5+
source access_key.sh
6+
7+
set -eux
8+
9+
function cleanup() {
10+
set +x
11+
source access_key.sh
12+
set -x
13+
ansible-playbook webapp.yaml -e "operation=delete" "$@"
14+
exit 1
15+
}
16+
17+
trap 'cleanup "${@}"' ERR
18+
19+
# Create web application
20+
ansible-playbook webapp.yaml "$@"
21+
22+
# Delete web application
23+
ansible-playbook webapp.yaml -e "operation=delete" "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- import_playbook: cloud.aws_ops.webapp.webapp

0 commit comments

Comments
 (0)