1
+ locals {
2
+ id = " ${ replace (var. name , " " , " -" )} "
3
+ }
4
+
5
+ # --------------------------------------------------------
6
+ # CREATE New Service
7
+ # --------------------------------------------------------
8
+
9
+ resource "aws_ecr_repository" "this" {
10
+ count = " ${ length (var. repositories )} "
11
+ name = " ${ lower (element (values (var. repositories ), count. index ))} "
12
+ }
13
+
14
+ resource "aws_ecs_task_definition" "this" {
15
+ family = " ${ local . id } "
16
+ container_definitions = " ${ data . template_file . this . rendered } "
17
+ volume = " ${ var . volumes } "
18
+ task_role_arn = " ${ aws_iam_role . this . arn } "
19
+ requires_compatibilities = [" ${ var . compatibilities } " ]
20
+ }
21
+
22
+ resource "aws_alb_target_group" "this" {
23
+ count = " ${ var . balancer [" vpc_id" ] != " " ? 1 : 0 } "
24
+ name = " ${ local . id } "
25
+ port = 80
26
+ protocol = " HTTP"
27
+ vpc_id = " ${ var . balancer [" vpc_id" ]} "
28
+ tags = " ${ merge (var. tags , map (" Name" , " ${ var . name } " ))} "
29
+ deregistration_delay = 3
30
+
31
+ stickiness {
32
+ type = " lb_cookie"
33
+ enabled = false
34
+ }
35
+ }
36
+
37
+ resource "aws_lb_listener_rule" "http" {
38
+ count = " ${ lookup (var. balancer , " condition_values" , " " ) != " " ? length (split (" ," , var. balancer [" condition_values" ])) : 0 } "
39
+ listener_arn = " ${ var . balancer [" listener_http" ]} "
40
+ priority = " ${ var . balancer [" priority" ] + count . index } "
41
+
42
+ action {
43
+ type = " forward"
44
+ target_group_arn = " ${ aws_alb_target_group . this . 0 . arn } "
45
+ }
46
+
47
+ condition {
48
+ field = " ${ var . balancer [" condition_field" ]} "
49
+ values = [" ${ element (split (" ," , var. balancer [" condition_values" ]), count. index )} " ]
50
+ }
51
+ }
52
+
53
+ resource "aws_lb_listener_rule" "https" {
54
+ count = " ${ lookup (var. balancer , " condition_values" , " " ) != " " ? length (split (" ," , var. balancer [" condition_values" ])) : 0 } "
55
+ listener_arn = " ${ var . balancer [" listener_https" ]} "
56
+ priority = " ${ var . balancer [" priority" ] + count . index } "
57
+
58
+ action {
59
+ type = " forward"
60
+ target_group_arn = " ${ aws_alb_target_group . this . 0 . arn } "
61
+ }
62
+
63
+ condition {
64
+ field = " ${ var . balancer [" condition_field" ]} "
65
+ values = [" ${ element (split (" ," , var. balancer [" condition_values" ]), count. index )} " ]
66
+ }
67
+ }
68
+
69
+ resource "aws_ecs_service" "this" {
70
+ count = " ${ var . balancer [" vpc_id" ] != " " && var . cluster != " " ? 1 : 0 } "
71
+ name = " ${ local . id } "
72
+ cluster = " ${ var . cluster } "
73
+ task_definition = " ${ aws_ecs_task_definition . this . arn } "
74
+ desired_count = " ${ var . desired } "
75
+ iam_role = " arn:aws:iam::159895783284:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS"
76
+ health_check_grace_period_seconds = 0
77
+
78
+ load_balancer {
79
+ target_group_arn = " ${ aws_alb_target_group . this . 0 . arn } "
80
+ container_name = " ${ var . balancer [" container_name" ]} "
81
+ container_port = " ${ var . balancer [" container_port" ]} "
82
+ }
83
+
84
+ ordered_placement_strategy {
85
+ type = " spread"
86
+ field = " host"
87
+ }
88
+ }
89
+
90
+ resource "aws_ecs_service" "no_balancer" {
91
+ count = " ${ var . balancer [" vpc_id" ] != " " || var . cluster == " " ? 0 : 1 } "
92
+ name = " ${ local . id } "
93
+ cluster = " ${ var . cluster } "
94
+ task_definition = " ${ aws_ecs_task_definition . this . arn } "
95
+ desired_count = " ${ var . desired } "
96
+ health_check_grace_period_seconds = 0
97
+
98
+ ordered_placement_strategy {
99
+ type = " spread"
100
+ field = " host"
101
+ }
102
+ }
103
+
104
+ resource "aws_iam_role" "this" {
105
+ name = " ${ local . id } "
106
+ description = " ${ var . name } ECSTask"
107
+ assume_role_policy = << EOF
108
+ {
109
+ "Version": "2012-10-17",
110
+ "Statement": [
111
+ {
112
+ "Effect": "Allow",
113
+ "Principal": {
114
+ "Service": "ecs-tasks.amazonaws.com"
115
+ },
116
+ "Action": "sts:AssumeRole"
117
+ }
118
+ ]
119
+ }
120
+ EOF
121
+ }
122
+
123
+ module "logs" {
124
+ source = " Aplyca/cloudwatchlogs/aws"
125
+ version = " 0.1.0"
126
+
127
+ name = " ${ local . id } "
128
+ role = " ${ aws_iam_role . this . name } "
129
+ description = " ${ var . name } ECSTask CloudWatch Logs"
130
+ tags = " ${ merge (var. tags , map (" Name" , " ${ var . name } " ))} "
131
+ }
0 commit comments