-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_nlb.yaml
117 lines (117 loc) · 3.77 KB
/
create_nlb.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
- hosts: localhost
gather_facts: no
module_defaults:
group/aws:
region: "us-east-2"
tasks:
- name: Create Target Group
elb_target_group:
name: Module5-tg
protocol: tcp
port: 80
stickiness_enabled: no
stickiness_type: source_ip
vpc_id: "{{ vpc.vpc.id }}"
state: present
health_check_path: /
register: nlb_tg
# - name: Show debug for NLB-TG
# debug: var=nlb_tg
- name: Get first EIP
ec2_eip_info:
filters:
tag:Name: nginx-server-ip-1
register: eip_one
- name: Get second EIP
ec2_eip_info:
filters:
tag:Name: nginx-server-ip-2
register: eip_two
# - name: Show eip_one info
# debug: var=eip_one
# - name: Show eip_two info
# debug: var=eip_two
- name: Create Network Load Balancer
elb_network_lb:
name: Module5-nlb
subnet_mappings:
- SubnetId: "{{ public_subnet_1.subnet.id }}"
AllocationId: "{{ eip_one.addresses[0].allocation_id }}"
- SubnetId: "{{ public_subnet_2.subnet.id }}"
AllocationId: "{{ eip_two.addresses[0].allocation_id }}"
purge_listeners: yes
listeners:
- Protocol: TCP
Port: 80
DefaultActions:
- Type: forward
TargetGroupName: "{{ nlb_tg.target_group_name }}"
- Protocol: TLS
Port: 443
Certificates:
- CertificateArn: "{{ secrets.cert }}"
DefaultActions:
- Type: forward
TargetGroupName: "{{ nlb_tg.target_group_name }}"
state: present
register: nlb
# - name: Show Network Load Balancer info
# debug: var=nlb
- name: Create Launch Template
ec2_launch_template:
name: "Module5_launch_template"
image_id: "ami-026dea5602e368e96"
key_name: mint-tower
instance_type: t2.micro
iam_instance_profile: image-gallery-server-role
network_interfaces:
- device_index: 0
description: Module 5 launch template
associate_public_ip_address: yes
delete_on_termination: yes
groups:
- "{{ nginx_sg.group_id }}"
- "{{ development_sg.group_id }}"
- "{{ postgres_tag_sg.group_id }}"
user_data: "{{ lookup('file', './startup-scripts/ec2_startup.sh') | b64encode }}"
tags:
Name: Module5-server-asg
state: present
register: launch_template
- name: Gather EC2 info for Auto Scaling Group
ec2_instance_info:
filters:
tag:Name: Module5-server-asg
instance-state-name: "running"
register: asg_ec2
- name: Show asg_ec2 instances
debug: var=asg_ec2.instances
- name: Restart EC2 instances if Network Load Balancer changed
ec2:
state: 'absent'
instance_ids: "{{ item.instance_id}}"
with_items: "{{ asg_ec2.instances }}"
when: nlb['changed'] == true
# - name: Debug EC2s in ASG info
# debug: var=running_asg_ec2s
- name: Create Auto Scaling Group
ec2_asg:
name: Module5_asg
health_check_type: ELB
wait_for_instances: False
lt_check: yes
launch_template:
version: '$Latest'
launch_template_id: "{{ launch_template.latest_template.launch_template_id }}"
min_size: 2
max_size: 4
desired_capacity: 2
availability_zones:
- "{{ public_subnet_1.subnet.availability_zone }}"
- "{{ public_subnet_2.subnet.availability_zone }}"
vpc_zone_identifier:
- "{{ public_subnet_1.subnet.id }}"
- "{{ public_subnet_2.subnet.id }}"
target_group_arns: "{{ nlb_tg.target_group_arn }}"
state: present