generated from bcgov/bcgov-terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathecs.tf
113 lines (98 loc) · 3.07 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
# ecs.tf
resource "aws_ecs_cluster" "main" {
name = "sample-cluster"
# capacity_providers = ["FARGATE_SPOT"]
# default_capacity_provider_strategy {
# capacity_provider = "FARGATE_SPOT"
# weight = 100
# }
tags = local.common_tags
}
resource "aws_ecs_cluster_capacity_providers" "sample" {
cluster_name = aws_ecs_cluster.main.name
capacity_providers = ["FARGATE_SPOT"]
default_capacity_provider_strategy {
weight = 100
capacity_provider = "FARGATE_SPOT"
}
}
resource "aws_ecs_task_definition" "app" {
count = local.create_ecs_service
family = "sample-app-task"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
task_role_arn = aws_iam_role.sample_app_container_role.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.fargate_cpu
memory = var.fargate_memory
tags = local.common_tags
container_definitions = jsonencode([
{
essential = true
name = var.container_name
image = var.app_image
cpu = var.fargate_cpu
memory = var.fargate_memory
networkMode = "awsvpc"
portMappings = [
{
protocol = "tcp"
containerPort = var.app_port
hostPort = var.app_port
}
]
environment = [
{
name = "DB_NAME"
value = var.db_name
},
{
name = "AWS_REGION",
value = var.aws_region
},
{
name = "bucketName",
value = aws_s3_bucket.upload_bucket.id
}
]
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-create-group = "true"
awslogs-group = "/ecs/${var.app_name}"
awslogs-region = var.aws_region
awslogs-stream-prefix = "ecs"
}
}
mountPoints = []
volumesFrom = []
}
])
}
resource "aws_ecs_service" "main" {
count = local.create_ecs_service
name = "sample-service"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.app[count.index].arn
desired_count = var.app_count
enable_ecs_managed_tags = true
propagate_tags = "TASK_DEFINITION"
health_check_grace_period_seconds = 60
wait_for_steady_state = false
capacity_provider_strategy {
capacity_provider = "FARGATE_SPOT"
weight = 100
}
network_configuration {
security_groups = [aws_security_group.ecs_tasks.id]
subnets = module.network.aws_subnet_ids.app.ids
assign_public_ip = false
}
load_balancer {
target_group_arn = aws_alb_target_group.app.id
container_name = var.container_name
container_port = var.app_port
}
depends_on = [aws_iam_role_policy_attachment.ecs_task_execution_role]
tags = local.common_tags
}