Skip to content

Commit 9737f5c

Browse files
committed
add route53, cleanup book, and autoscale
1 parent 9097cea commit 9737f5c

File tree

21 files changed

+272
-110
lines changed

21 files changed

+272
-110
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.retry
2+
.DS_Store

ansible.cfg

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
[defaults]
2-
serial=1
3-
forks=1
2+

aws_cleanup.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
3+
# Terminate all AWS related stuff
4+
5+
- name: Destroy AWS related stuff for Spring App
6+
hosts: local
7+
connection: local
8+
roles:
9+
- { role: aws_cleanup, tags: ['aws_cleanup'] } #clean up aws

deploy_app.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
gather_facts: yes
1212
roles:
1313
- { role: haproxy, tags: ['haproxy'] } # haproxy installation and configuration, load balancing between spring apps
14+
- { role: rsyslog, tags: ['rsyslog'] } # rsyslog configuration, enable it for haproxy
1415
- { role: nginx, tags: ['nginx'] } # nginx installation and configuration, proxying to haproxy from elb.
15-
- { role: deploy_spring_app, tags: ['deploy_spring_app'] } # this is a collection of tasks to clone/checkout/package/run an app
16+
- { role: deploy_spring_app, tags: ['deploy_spring_app'] } # this is a collection of tasks to clone/checkout/package/run an app

deploy_to_route53.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
3+
# This playbook deploys the route53 records and health checks for the launched elbs for two profiles
4+
# AWS_SECOND_PROFILE needs to be already deployed with full_deploy play
5+
6+
- name: Add route53 records for our ELBs
7+
hosts: local
8+
connection: local
9+
roles:
10+
- { role: aws_route53, tags: ['create_route53_records'] }

full_deploy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
- { role: haproxy, tags: ['haproxy'] } # haproxy installation and configuration, load balancing between spring apps
3232
- { role: rsyslog, tags: ['rsyslog'] } # rsyslog configuration, enable for haproxy
3333
- { role: nginx, tags: ['nginx'] } # nginx installation and configuration, proxying to haproxy from elb.
34-
- { role: deploy_spring_app, tags: ['deploy_spring_app'] } # this is a collection of tasks to clone/checkout/package/run an app
34+
- { role: deploy_spring_app, tags: ['deploy_spring_app'] } # this is a collection of tasks to clone/checkout/package/run an app

group_vars/all

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
---
22

3-
#how many instances of the Spring app we want to run in a single sever
3+
#how many app instances of the Spring app we want to run in a single ec2 instace
44
app_instances: 2
55

66
#the default base port for our
77
app_port: 8080
88

99
haproxy_springapps_port: 8888
1010

11+
#an aws_profile to be used to run our books
12+
1113
aws_profile: "{{ lookup('env', 'AWS_DEFAULT_PROFILE') | default('test', true) }}"
14+
aws_profile_2: "{{ lookup('env', 'AWS_SECOND_PROFILE') | default('false') }}"
15+
16+
aws_region: "{{ lookup('env', 'AWS_DEFAULT_REGION') | default('us-east-1', true) }}"
1217

1318
#ssh key
1419
aws_key_name: waheed
@@ -17,22 +22,29 @@ ansible_user: ubuntu
1722

1823
#git repo vars
1924
git_key: ec2-new
20-
app_git_url: "[email protected]:waheedi/hello-spring-app.git"
25+
app_git_url: "[email protected]:waheedi/spring-time-app.git"
2126
branch: master
2227

2328
#a marker to fail accidental run for create ec2s
2429
create_ec2_instances: yes
2530

26-
#How many instance per az
31+
#How many ec2 instances per az
2732
instances_count: 1
2833

2934
#How many azs we need for production only
3035
az_count: 2
3136

32-
#default instance type for our run
37+
#default ec2 instance type for our run
3338
aws_instance_type: t2.micro
3439

3540
#auto update the Spring App version using our versioning, maybe can be better used with mvn:commit
3641
update_version: yes
3742

38-
rsyslog_server: localhost
43+
rsyslog_server: localhost
44+
45+
#a configured zone on route53
46+
route53_zone: "saascoin.network"
47+
48+
# a dns record for our app to be hosted on it
49+
hello_dns: "hello.saascoin.network"
50+

group_vars/prod

-9
This file was deleted.

group_vars/test

-5
This file was deleted.

hosts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
[local]
22
localhost
33
[prod]
4-
18.215.33.203
5-
35.172.216.98
4+
18.206.152.159
5+
35.168.59.226
6+
[sta]
7+
52.53.159.204
8+
13.57.42.119
9+
[time-app]
10+
35.176.78.146
11+
3.8.95.133

roles/aws_autoscale/tasks/main.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
3+
- name: Create a launch config for our auto scaling instances
4+
ec2_lc:
5+
name: "lc-springapp-{{ aws_profile }}"
6+
instance_id: "{{ (hostvars[(groups[aws_profile] | last )].instance_ids | first ) }}"
7+
key_name: "{{ aws_key_name }}"
8+
security_groups: ["{{ hostvars[(groups[aws_profile] | last )].springappsg }}"]
9+
profile: "{{ aws_profile }}"
10+
instance_monitoring: yes
11+
12+
- name: Create Auto Scaling group for second profile
13+
ec2_asg:
14+
name: "asg-springapp-{{ aws_profile }}"
15+
load_balancers: ["spring-app-{{ aws_profile }}"]
16+
launch_config_name: "lc-springapp-{{ aws_profile }}"
17+
health_check_period: 60
18+
health_check_type: ELB
19+
min_size: "{{ az_count }}"
20+
max_size: "{{ az_count }}"
21+
desired_capacity: "{{ az_count }}"
22+
region: "{{ hostvars[(groups[aws_profile] | last )].aws_region }}"
23+
when: aws_profile_2 is defined

roles/aws_cleanup/tasks/main.yml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
3+
- name: Delete health check for route53 for the first region
4+
route53_health_check:
5+
state: absent
6+
fqdn: "{{ hostvars[(groups[aws_profile] | last )].springappelb_name }}"
7+
type: HTTP_STR_MATCH
8+
resource_path: /hello
9+
string_match: "hello"
10+
request_interval: 10
11+
failure_threshold: 2
12+
13+
- name: Delete Route53 record
14+
route53:
15+
state: absent
16+
zone: "{{ route53_zone }}"
17+
type: A
18+
record: "{{ hello_dns }}"
19+
wait: yes
20+
wait_timeout: 300
21+
value: "{{ hostvars[(groups[aws_profile] | last )].springappelb_name }}"
22+
23+
- name: Delete Load Balancer for Spring App
24+
ec2_elb_lb:
25+
name: "spring-app-{{aws_profile}}"
26+
state: absent
27+
profile: "{{ aws_profile }}"
28+
region: "{{ hostvars[(groups[aws_profile] | last )].aws_region }}"
29+
wait: yes
30+
wait_timeout: 300
31+
32+
- name: Delete instances that were previously launched
33+
ec2:
34+
state: absent
35+
instance_ids: "{{ hostvars[(groups[aws_profile] | last )].instance_ids }}"
36+
profile: "{{ aws_profile }}"
37+
region: "{{ hostvars[(groups[aws_profile] | last )].aws_region }}"
38+
wait: yes
39+
wait_timeout: 300
40+
41+
- name: Delete Elastic Load Balancer Security Group
42+
ec2_group:
43+
group_id: "{{ hostvars[(groups[aws_profile] | last )].springappelbsg }}"
44+
profile: "{{ aws_profile }}"
45+
region: "{{ hostvars[(groups[aws_profile] | last )].aws_region }}"
46+
state: absent
47+
48+
- name: Delete Spring App Security Group
49+
ec2_group:
50+
group_id: "{{ hostvars[(groups[aws_profile] | last )].springappsg }}"
51+
profile: "{{ aws_profile }}"
52+
region: "{{ hostvars[(groups[aws_profile] | last )].aws_region }}"
53+
state: absent

roles/aws_ec2_instances/tasks/main.yml

+23-52
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,10 @@
104104
count: "{{ instances_count }}"
105105
monitoring: yes
106106
register: springapp_az2_ec2s
107-
when: '"prod" in aws_profile'
108107

109108
- name: Set AZ2 instances fact
110109
set_fact:
111110
az2_instances: "{{ springapp_az2_ec2s.instances }}"
112-
when: '"prod" in aws_profile'
113-
114-
- name: PSEUDO Set AZ2 instances fact
115-
set_fact:
116-
az2_instances: []
117-
when: az2_instances is not defined
118111

119112
- name: Tag instances at AZ1
120113
ec2_tag:
@@ -137,7 +130,6 @@
137130
with_together:
138131
- "{{ az2_instances }}"
139132
- "{{ range(1, instances_count + 1) | list }}"
140-
when: '"prod" in aws_profile'
141133

142134
- debug: var=springapp_az2_ec2s
143135

@@ -160,14 +152,12 @@
160152
cidr_ip: 0.0.0.0/0
161153
state: "present"
162154
register: my_lb_group
163-
when: '"prod" in aws_profile or "sta" in aws_profile'
164155

165156
#- debug: var=my_lb_group
166157

167158
- name: Set Elastic Load Balancer Security Group ID in variable
168159
set_fact:
169160
lb_group_id: "{{ my_lb_group.group_id }}"
170-
when: '"prod" in aws_profile or "sta" in aws_profile'
171161

172162
- name: Create Load Balancer for Spring App
173163
ec2_elb_lb:
@@ -202,9 +192,7 @@
202192
register: ec2_elb
203193
with_flattened:
204194
- "{{ springapp_az1_ec2s.instance_ids }}"
205-
- "{{ springapp_az2_ec2s.instance_ids }}"
206-
when: '"prod" in aws_profile or "sta" in aws_profile'
207-
195+
- "{{ springapp_az2_ec2s.instance_ids }}"
208196

209197
#- debug: var=ec2_elb
210198

@@ -237,16 +225,6 @@
237225

238226
#- debug: var=az1_instances
239227

240-
- name: Add meta data for the newly created EC2 instances to the var group AZ1
241-
lineinfile:
242-
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
243-
regexp: "{{ aws_profile }}_{{ item.1 }}_{{ az1 | replace('-','_') }}"
244-
line: "{{ aws_profile }}_{{ item.1 }}_{{ az1 | replace('-','_') }}: '{{ item.0.id }}' "
245-
with_together:
246-
- "{{ az1_instances }}"
247-
- "{{ range(1, instances_count + 1) | list }}"
248-
changed_when: False
249-
250228
- name: Add AZ1 to our profile group
251229
lineinfile:
252230
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
@@ -265,55 +243,48 @@
265243
- name: Add instance_ids to current run profile az1 + az2
266244
lineinfile:
267245
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
268-
regexp: 'instance_ids_{{aws_profile}}'
269-
line: 'instance_ids_{{aws_profile}}: "{{ springapp_az1_ec2s.instance_ids + springapp_az2_ec2s.instance_ids }}"'
246+
regexp: 'instance_ids'
247+
line: 'instance_ids: "{{ springapp_az1_ec2s.instance_ids + springapp_az2_ec2s.instance_ids }}"'
270248
when: (az2_instances | length > 0)
271249
changed_when: False
272250

273-
- name: Add instance_ids to current run profile az1
274-
lineinfile:
275-
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
276-
regexp: 'instance_ids_{{aws_profile}}'
277-
line: 'instance_ids_{{aws_profile}}: "{{ springapp_az1_ec2s.instance_ids }}"'
278-
when: (az2_instances | length == 0)
279-
changed_when: False
280-
281-
- name: Add meta data for the newly created EC2 instances to the var group AZ2
282-
lineinfile:
283-
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
284-
regexp: "{{ aws_profile }}_{{ item.1 }}_{{ az2 | replace('-','_') }}"
285-
line: "{{ aws_profile }}_{{ item.1 }}_{{ az2 | replace('-','_') }}: '{{ item.0.id }}'"
286-
with_together:
287-
- "{{ az2_instances }}"
288-
- "{{ range(1, instances_count + 1) | list }}"
289-
when: '"prod" in aws_profile'
290-
changed_when: False
291-
292251
- name: Add meta data for the newly created EC2 Security Group to the var group
293252
lineinfile:
294253
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
295-
regexp: 'springappsg_{{aws_profile}}'
296-
line: "springappsg_{{aws_profile}}: '{{ item }}'"
254+
regexp: 'springappsg'
255+
line: "springappsg: '{{ item }}'"
297256
with_items:
298257
- "{{ springapp_group_id }}"
299258
changed_when: False
300259

301260
- name: Add meta data for the newly created ELB Security Group to the var group
302261
lineinfile:
303262
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
304-
regexp: "springappelbsg_"
305-
line: "springappelbsg_{{aws_profile}}: '{{ item }}'"
263+
regexp: "springappelbsg"
264+
line: "springappelbsg: '{{ item }}'"
306265
with_items:
307266
- "{{ lb_group_id }}"
308-
when: '"prod" in aws_profile or "sta" in aws_profile'
309267
changed_when: False
310268

311269
- name: Add meta data for the newly created ELB to the var group
312270
lineinfile:
313271
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
314-
regexp: "springappelb_"
315-
line: "springappelb_{{aws_profile}}: '{{ ec2_elb.results[0].elb.hosted_zone_name }}' "
316-
when: '"prod" in aws_profile or "sta" in aws_profile'
272+
regexp: "springappelb_name"
273+
line: "springappelb_name: '{{ ec2_elb.results[0].elb.hosted_zone_name }}' "
274+
changed_when: False
275+
276+
- name: Add meta data for the newly created ELB to the var group
277+
lineinfile:
278+
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
279+
regexp: "springappelb_zone"
280+
line: "springappelb_zone: '{{ ec2_elb.results[0].elb.hosted_zone_id }}' "
281+
changed_when: False
282+
283+
- name: Add the region to our vars
284+
lineinfile:
285+
dest: "{{ inventory_dir }}/group_vars/{{ aws_profile }}"
286+
regexp: "aws_region"
287+
line: "aws_region: {{ aws_region }}"
317288
changed_when: False
318289

319290
- name: Add last deploy epoch time to profile

0 commit comments

Comments
 (0)