-
Notifications
You must be signed in to change notification settings - Fork 158
/
ecs.tf
193 lines (169 loc) · 5.87 KB
/
ecs.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
/**
* Elastic Container Service (ecs)
* This component is required to create the Fargate ECS service. It will create a Fargate cluster
* based on the application name and environment. It will create a "Task Definition", which is required
* to run a Docker container, https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html.
* Next it creates a ECS Service, https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_services.html
* It attaches the Load Balancer created in `lb.tf` to the service, and sets up the networking required.
* It also creates a role with the correct permissions. And lastly, ensures that logs are captured in CloudWatch.
*
* When building for the first time, it will install a "default backend", which is a simple web service that just
* responds with a HTTP 200 OK. It's important to uncomment the lines noted below after you have successfully
* migrated the real application containers to the task definition.
*/
# How many containers to run
variable "replicas" {
default = "1"
}
# The name of the container to run
variable "container_name" {
default = "app"
}
# The minimum number of containers that should be running.
# Must be at least 1.
# used by both autoscale-perf.tf and autoscale.time.tf
# For production, consider using at least "2".
variable "ecs_autoscale_min_instances" {
default = "1"
}
# The maximum number of containers that should be running.
# used by both autoscale-perf.tf and autoscale.time.tf
variable "ecs_autoscale_max_instances" {
default = "8"
}
resource "aws_ecs_cluster" "app" {
name = "${var.app}-${var.environment}"
setting {
name = "containerInsights"
value = "enabled"
}
tags = var.tags
}
# The default docker image to deploy with the infrastructure.
# Note that you can use the fargate CLI for application concerns
# like deploying actual application images and environment variables
# on top of the infrastructure provisioned by this template
# https://github.com/turnerlabs/fargate
# note that the source for the turner default backend image is here:
# https://github.com/turnerlabs/turner-defaultbackend
variable "default_backend_image" {
default = "ghcr.io/warnermedia/fargate-default-backend:v0.9.0"
}
resource "aws_appautoscaling_target" "app_scale_target" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.app.name}/${aws_ecs_service.app.name}"
scalable_dimension = "ecs:service:DesiredCount"
max_capacity = var.ecs_autoscale_max_instances
min_capacity = var.ecs_autoscale_min_instances
}
resource "aws_ecs_task_definition" "app" {
family = "${var.app}-${var.environment}"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecsTaskExecutionRole.arn
# defined in role.tf
task_role_arn = aws_iam_role.app_role.arn
container_definitions = <<DEFINITION
[
{
"name": "${var.container_name}",
"image": "${var.default_backend_image}",
"essential": true,
"portMappings": [
{
"protocol": "tcp",
"containerPort": ${var.container_port},
"hostPort": ${var.container_port}
}
],
"environment": [
{
"name": "PORT",
"value": "${var.container_port}"
},
{
"name": "HEALTHCHECK",
"value": "${var.health_check}"
},
{
"name": "ENABLE_LOGGING",
"value": "false"
},
{
"name": "PRODUCT",
"value": "${var.app}"
},
{
"name": "ENVIRONMENT",
"value": "${var.environment}"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/fargate/service/${var.app}-${var.environment}",
"awslogs-region": "${var.region}",
"awslogs-stream-prefix": "ecs"
}
}
}
]
DEFINITION
tags = var.tags
}
resource "aws_ecs_service" "app" {
name = "${var.app}-${var.environment}"
cluster = aws_ecs_cluster.app.id
launch_type = "FARGATE"
task_definition = aws_ecs_task_definition.app.arn
desired_count = var.replicas
network_configuration {
security_groups = [aws_security_group.nsg_task.id]
subnets = split(",", var.private_subnets)
}
load_balancer {
target_group_arn = aws_alb_target_group.main.id
container_name = var.container_name
container_port = var.container_port
}
tags = var.tags
enable_ecs_managed_tags = true
propagate_tags = "SERVICE"
# workaround for https://github.com/hashicorp/terraform/issues/12634
depends_on = [aws_alb_listener.http]
# [after initial apply] don't override changes made to task_definition
# from outside of terraform (i.e.; fargate cli)
lifecycle {
ignore_changes = [task_definition]
}
}
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html
resource "aws_iam_role" "ecsTaskExecutionRole" {
name = "${var.app}-${var.environment}-ecs"
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}
data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "ecsTaskExecutionRole_policy" {
role = aws_iam_role.ecsTaskExecutionRole.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
variable "logs_retention_in_days" {
type = number
default = 90
description = "Specifies the number of days you want to retain log events"
}
resource "aws_cloudwatch_log_group" "logs" {
name = "/fargate/service/${var.app}-${var.environment}"
retention_in_days = var.logs_retention_in_days
tags = var.tags
}