forked from yardbirdsax/terraform-aws-ecs-codedeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
400 lines (355 loc) · 9.68 KB
/
main.tf
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">3"
}
}
required_version = ">=0.12.31"
}
locals {
deployment_name = var.deployment_name == "" ? "nginx-test" : var.deployment_name
}
data aws_caller_identity current {}
data aws_ecr_repository ecr_repo {
name = local.deployment_name
count = var.container_image_name == "" ? 1 : 0
}
data aws_vpc vpc {
default = true
count = var.vpc_id == "" ? 1 : 0
}
data aws_availability_zones azs {
state = "available"
}
data aws_subnet subnet {
count = var.vpc_id == "" ? length(data.aws_availability_zones.azs.names) : 0
vpc_id = data.aws_vpc.vpc[0].id
availability_zone = data.aws_availability_zones.azs.names[count.index]
}
locals {
vpc_id = var.vpc_id == "" ? data.aws_vpc.vpc[0].id : var.vpc_id
deployment_group_listeners = var.lb_certificate_arn == "" ? [ aws_lb_listener.elb_listener.arn ] : [ aws_lb_listener.elb_listener_https[0].arn ]
container_image_name = var.container_image_name == "" ? data.aws_ecr_repository.ecr_repo[0].repository_url : var.container_image_name
security_group_ids = length(var.security_group_ids) == 0 ? [aws_security_group.security_group_web.id] : var.security_group_ids
subnet_ids = length(var.subnet_ids) == 0 ? flatten(data.aws_subnet.subnet.*.id) : var.subnet_ids
}
resource aws_security_group security_group_web {
name = var.deployment_name
ingress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 80
to_port = 80
protocol = "tcp"
}
ingress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 443
to_port = 443
protocol = "tcp"
}
egress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
to_port = 0
protocol = "-1"
}
vpc_id = local.vpc_id
}
resource aws_lb elb {
name = local.deployment_name
load_balancer_type = "application"
subnets = local.subnet_ids
enable_cross_zone_load_balancing = true
security_groups = [aws_security_group.security_group_web.id]
idle_timeout = var.alb_idle_timeout_seconds
tags = var.tags
}
resource aws_lb_target_group target_group_blue {
name = "${local.deployment_name}-blue"
target_type = "ip"
health_check {
enabled = true
healthy_threshold = 2
interval = var.health_check_interval
path = var.health_check_path
timeout = var.health_check_timeout
}
protocol = "HTTP"
port = 80
vpc_id = local.vpc_id
tags = var.tags
}
resource aws_lb_target_group target_group_green {
name = "${local.deployment_name}-green"
target_type = "ip"
health_check {
enabled = true
healthy_threshold = 2
interval = var.health_check_interval
path = var.health_check_path
timeout = var.health_check_timeout
}
protocol = "HTTP"
port = 80
vpc_id = local.vpc_id
tags = var.tags
}
resource aws_lb_listener elb_listener {
load_balancer_arn = aws_lb.elb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.target_group_blue.arn
}
lifecycle {
ignore_changes = [
default_action[0].target_group_arn
]
}
}
resource aws_lb_listener elb_listener_https {
count = var.lb_certificate_arn == "" ? 0 : 1
load_balancer_arn = aws_lb.elb.arn
port = 443
protocol = "HTTPS"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.target_group_blue.arn
}
certificate_arn = var.lb_certificate_arn
ssl_policy = var.ssl_policy == "" ? "ELBSecurityPolicy-TLS-1-2-2017-01" : var.ssl_policy
lifecycle {
ignore_changes = [
default_action[0].target_group_arn
]
}
}
resource aws_codedeploy_app app {
name = local.deployment_name
compute_platform = "ECS"
}
resource aws_codedeploy_deployment_config deploy_config {
compute_platform = "ECS"
deployment_config_name = local.deployment_name
traffic_routing_config {
type = "TimeBasedLinear"
time_based_linear {
interval = 1
percentage = 50
}
}
}
resource aws_iam_role codedeploy_role {
name = "${local.deployment_name}-CodeDeployRole"
assume_role_policy = <<JSON
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": ["codedeploy.amazonaws.com"]
},
"Effect": "Allow",
"Sid": ""
}
]
}
JSON
}
resource aws_iam_role_policy_attachment codedeploy_role_policy_attachment {
policy_arn = "arn:aws:iam::aws:policy/AWSCodeDeployRoleForECS"
role = aws_iam_role.codedeploy_role.name
}
resource aws_s3_bucket codedeploy_s3 {
bucket = "${local.deployment_name}-${data.aws_caller_identity.current.account_id}"
force_destroy = true
tags = var.tags
}
resource aws_codedeploy_deployment_group deploy_group {
app_name = aws_codedeploy_app.app.name
deployment_group_name = local.deployment_name
deployment_config_name = aws_codedeploy_deployment_config.deploy_config.deployment_config_name
ecs_service {
cluster_name = aws_ecs_cluster.ecs_cluster.name
service_name = aws_ecs_service.ecs_service.name
}
autoscaling_groups = []
service_role_arn = aws_iam_role.codedeploy_role.arn
load_balancer_info {
target_group_pair_info {
prod_traffic_route {
listener_arns = local.deployment_group_listeners
}
target_group {
name = aws_lb_target_group.target_group_blue.name
}
target_group {
name = aws_lb_target_group.target_group_green.name
}
}
}
blue_green_deployment_config {
deployment_ready_option {
action_on_timeout = "CONTINUE_DEPLOYMENT"
}
terminate_blue_instances_on_deployment_success {
action = "TERMINATE"
termination_wait_time_in_minutes = var.termination_wait_time
}
}
deployment_style {
deployment_option = "WITH_TRAFFIC_CONTROL"
deployment_type = "BLUE_GREEN"
}
}
resource aws_ecs_cluster ecs_cluster {
name = var.deployment_name
}
resource aws_iam_role ecs_task_execution_role {
name = "${var.deployment_name}-TaskExecutionRole"
assume_role_policy = <<JSON
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": ["ecs-tasks.amazonaws.com"]
},
"Effect": "Allow",
"Sid": ""
}
]
}
JSON
}
resource aws_iam_role_policy ecs_task_execution_policy {
name = "${var.deployment_name}-TaskExecutionPolicy"
role = aws_iam_role.ecs_task_execution_role.name
policy = <<JSON
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetAuthorizationToken",
"ecr:GetDownloadUrlForLayer"
],
"Resource": [
"*"
]
}
]
}
JSON
}
resource aws_iam_role_policy_attachment ecs_task_execution_policy_attachments {
count = length(var.task_exec_role_policies)
policy_arn = var.task_exec_role_policies[count.index]
role = aws_iam_role.ecs_task_execution_role.name
}
resource aws_iam_role ecs_task_role {
name = "${var.deployment_name}-TaskRole"
assume_role_policy = <<JSON
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": ["ecs-tasks.amazonaws.com"]
},
"Effect": "Allow",
"Sid": ""
}
]
}
JSON
}
resource aws_iam_role_policy_attachment ecs_task_policy_attachments {
count = length(var.task_role_policies)
policy_arn = var.task_role_policies[count.index]
role = aws_iam_role.ecs_task_role.name
}
resource aws_ecs_task_definition ecs_task {
cpu = var.container_cpu
memory = var.container_memory
container_definitions = <<JSON
[
{
"cpu": ${var.container_cpu},
"environment":${jsonencode(var.container_environment_variables)},
"essential": true,
"image": "${local.container_image_name}:${var.container_image_tag}",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/${var.deployment_name}",
"awslogs-region": "${var.aws_region_name}",
"awslogs-stream-prefix": "ecs",
"awslogs-create-group": "true"
}
},
"memory": ${var.container_memory},
"mountPoints": [],
"name": "${local.deployment_name}",
"portMappings": [{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}],
"secrets": ${jsonencode(var.container_secrets)},
"volumesFrom": []
}
]
JSON
family = local.deployment_name
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
task_role_arn = aws_iam_role.ecs_task_role.arn
}
resource local_file task_def_file {
content = templatefile("${path.module}/appspec.yml", {task_arn = aws_ecs_task_definition.ecs_task.arn, deployment_name = local.deployment_name})
filename = "${path.module}/terraform/build/appspec.yml"
}
resource aws_ecs_service ecs_service {
name = local.deployment_name
cluster = aws_ecs_cluster.ecs_cluster.arn
deployment_controller {
type = "CODE_DEPLOY"
}
task_definition = aws_ecs_task_definition.ecs_task.arn
launch_type = "FARGATE"
load_balancer {
container_name = local.deployment_name
container_port = 80
target_group_arn = aws_lb_target_group.target_group_blue.arn
}
network_configuration {
subnets = local.subnet_ids
assign_public_ip = true
security_groups = local.security_group_ids
}
desired_count = var.desired_count
depends_on = [
aws_lb_listener.elb_listener
]
lifecycle {
ignore_changes = [
load_balancer,
task_definition,
network_configuration,
desired_count
]
}
}