-
Notifications
You must be signed in to change notification settings - Fork 5
/
provision-and-deploy.yml
287 lines (257 loc) · 9.12 KB
/
provision-and-deploy.yml
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
---
- name: Build the Cloud Environment
hosts: localhost
connection: local
gather_facts: False
vars_files:
- ./vars/all.yml
- ./vars/secret.yml
tasks:
- name: Create the VPC
ec2_vpc_net:
region: "{{ cloud_config.region }}"
cidr_block: "{{ cloud_config.vpc_cidr }}"
name: "{{ cloud_config.vpc_name }}"
tags:
service: "{{ cluster_name }}"
state: present
register: cluster_vpc
- name: Create the Subnet
ec2_vpc_subnet:
region: "{{ cloud_config.region }}"
az: "{{ item.zone }}"
vpc_id: "{{ cluster_vpc.vpc.id }}"
cidr: "{{ item.subnet_cidr }}"
resource_tags:
Name: "{{ item.subnet_name }}"
state: present
with_items: "{{ cloud_config.subnets }}"
register: cluster_subnet
- name: Add an Internet Gateway to the VPC
ec2_vpc_igw:
region: "{{ cloud_config.region }}"
vpc_id: "{{ cluster_vpc.vpc.id }}"
state: present
when: cloud_config.internet_gateway
register: cluster_igw
- name: lookup route tables
ec2_vpc_route_table_facts:
region: "{{ cloud_config.region }}"
filters:
"tag:Name": scalable-app
register: check_route_table
ignore_errors: True
- debug: msg="{{ check_route_table }}"
- debug: msg="{{ cluster_subnet.results[0]['subnet'] }}"
- name: Set up the public subnet route table for the Internet Gateway
block:
- ec2_vpc_route_table:
tags:
Name: scalable-app
region: "{{ cloud_config.region }}"
vpc_id: "{{ cluster_vpc.vpc.id }}"
subnets:
- "{{ cloud_config.subnets[0].subnet_cidr }}"
- "{{ cloud_config.subnets[1].subnet_cidr }}"
- "{{ cloud_config.subnets[2].subnet_cidr }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ cluster_igw.gateway_id }}"
state: present
when: cluster_igw.gateway_id is defined and check_route_table|failed
- name: Create Security Groups
ec2_group:
region: "{{ cloud_config.region }}"
name: "{{ item.name }}"
vpc_id: "{{ cluster_vpc.vpc.id }}"
description: "{{ item.description }}"
purge_rules: false
purge_rules_egress: false
rules: "{{ item.rules }}"
with_items: "{{ cloud_config.security_groups }}"
register: cluster_security_groups
- name: Docker login (must `--no-include-email`)
shell: "$(aws ecr get-login --region {{ cloud_config.region }} --no-include-email)"
args:
executable: /bin/bash
- name: Create ECR repository
ecs_ecr:
name: "{{ repository_name }}"
aws_access_key: "{{ key_id }}"
aws_secret_key: "{{ access_key }}"
region: "{{ cloud_config.region }}"
register: ecr_repo
- name: Tag local image to push to ecr
docker_image:
name: "{{ image_name }}:{{ image_version }}"
repository: "{{ ecr_repo.repository.repositoryUri }}"
tag: "{{ ecr_repo.repository.repositoryUri }}:{{ image_version }}"
- name: Push image to ecr
docker_image:
name: "{{ ecr_repo.repository.repositoryUri }}:{{ image_version }}"
push: yes
- name: Create a bucket for static assets
aws_s3:
mode: create
bucket: "{{ s3_static_asset_bucket }}"
region: "{{ cloud_config.region }}"
permission: public-read
- name: Push static assets to S3
aws_s3:
bucket: "{{ s3_static_asset_bucket }}"
region: "{{ cloud_config.region }}"
object: tomato.jpg
src: ./app/src/static/tomato.jpg
mode: put
- name: Setup ECS cluster
ecs_cluster:
name: "{{ cluster_name }}"
state: present
region: "{{ cloud_config.region }}"
- name: Create a policy
iam_policy:
iam_type: role
iam_name: fargate-ecs
policy_name: fargate-ecs
policy_json: " {{ lookup( 'file', './vars/policy.json') }} "
state: present
- name: Create a role
iam_role:
name: fargate-ecs
assume_role_policy_document: "{{ lookup('file','./vars/role.json') }}"
description: Allows ECS tasks to call AWS services on your behalf.
register: ecs_role
- name: Create a log group for the logs
cloudwatchlogs_log_group:
retention: 30
region: "{{ cloud_config.region }}"
state: present
log_group_name: "{{ cluster_name }}"
- name: Lookup http security group by name
ec2_group_facts:
region: "{{ cloud_config.region }}"
filters:
group-name: scalable-http
register: http_security_group
- name: Lookup VPC by tags
ec2_vpc_net_facts:
filters:
"tag:service": "{{ cluster_name }}"
register: scalable_vpc_facts
- name: create ECS cluster
ecs_cluster:
name: "{{ cluster_name }}"
state: present
region: "{{ cloud_config.region }}"
register: result
- debug: msg="{{ result }}"
- name: setup a target group for ELB
elb_target_group:
name: "{{ cluster_name |replace('_', '-') }}"
protocol: http
target_type: ip
health_check_port: 8448
port: 8448
vpc_id: "{{ scalable_vpc_facts.vpcs[0]['vpc_id'] }}"
health_check_path: /_health
successful_response_codes: "200, 250-260"
state: present
- name: configure ELB for app
elb_application_lb:
state: present
name: "{{ cluster_name |replace('_', '-') }}"
scheme: internet-facing
security_groups:
- "{{ http_security_group.security_groups[0]['group_id'] }}"
region: "{{ cloud_config.region }}"
subnets:
- "{{ cluster_subnet.results[0]['subnet']['id'] }}"
- "{{ cluster_subnet.results[1]['subnet']['id'] }}"
- "{{ cluster_subnet.results[2]['subnet']['id'] }}"
listeners:
- Protocol: HTTP
Port: 80
DefaultActions:
- Type: forward
TargetGroupName: "{{ cluster_name |replace('_', '-') }}"
register: ecs_elb
- name: Lookup elb_target_group_facts
elb_target_group_facts:
names:
- "{{ cluster_name |replace('_', '-') }}"
register: elb_target_group_facts
- debug: msg="target group {{ elb_target_group_facts.target_groups[0]['target_group_arn'] }}"
- name: Lookup Elb Facts
shell: "(aws elbv2 describe-load-balancers --names {{ cluster_name |replace('_', '-') }} --output=json)"
register: ecs_elb_facts
- name: Make facts about elbs
set_fact:
ecs_elb_fact: "{{ ecs_elb_facts.stdout }}"
- debug: msg="{{ ecs_elb_facts.stdout }}"
- name: Lookup zone info
route53:
state: get
zone: "{{ dns_zone }}"
record: "{{ dns_zone }}"
type: A
register: zone_info
- name: register the ELB under the zone A record.
route53:
state: present
zone: "{{ dns_zone }}"
record: "{{ dns_zone }}"
type: A
value: "dualstack.{{ ecs_elb_fact.LoadBalancers[0]['DNSName'] }}"
overwrite: yes
alias: True
alias_hosted_zone_id: "{{ zone_info.set['alias_hosted_zone_id'] }}"
- name: Create task definition
ecs_taskdefinition:
family: "{{ cluster_name }}-taskdef"
task_role_arn: "{{ ecs_role.arn }}"
execution_role_arn: "{{ ecs_role.arn }}"
containers:
- name: "scalable_app"
essential: true
image: "{{ ecr_repo.repository.repositoryUri }}:{{ image_version }}"
portMappings:
- containerPort: 8448
hostPort: 8448
environment:
- name: s3_static_asset_bucket
value: "{{ s3_static_asset_bucket }}"
logConfiguration:
logDriver: awslogs
options:
awslogs-group: "{{ cluster_name }}"
awslogs-region: eu-west-1
awslogs-stream-prefix: "{{ cluster_name }}-containers"
launch_type: FARGATE
cpu: 512
memory: 1GB
state: present
network_mode: awsvpc
- name: Create an ECS service
ecs_service:
state: present
name: "{{ cluster_name }}"
cluster: "{{ cluster_name }}"
task_definition: "{{ cluster_name }}-taskdef"
launch_type: FARGATE
load_balancers:
- targetGroupArn: "{{ elb_target_group_facts.target_groups[0]['target_group_arn'] }}"
containerName: "scalable_app"
containerPort: 8448
network_configuration:
assign_public_ip: yes
subnets:
- "{{ cluster_subnet.results[0]['subnet']['id'] }}"
- "{{ cluster_subnet.results[1]['subnet']['id'] }}"
- "{{ cluster_subnet.results[2]['subnet']['id'] }}"
security_groups:
- "{{ http_security_group.security_groups[0]['group_id'] }}"
desired_count: "{{ size_of_cluster }}"
region: "{{ cloud_config.region }}"
deployment_configuration:
minimum_healthy_percent: 75
maximum_percent: 150