-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs.tf
95 lines (82 loc) · 2.75 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
# create ecs cluster
resource "aws_ecs_cluster" "ecs_cluster" {
name = "${var.project_name}-${var.environment}-ecs_cluster"
setting {
name = "containerInsights"
value = "disabled"
}
}
# create cloudwatch log group
resource "aws_cloudwatch_log_group" "log_group" {
name = "/ecs/${var.project_name}-${var.environment}-task_definition"
lifecycle {
create_before_destroy = true
}
}
# create task definition
resource "aws_ecs_task_definition" "ecs_task_definition" {
family = "${var.project_name}-${var.environment}-task_definition"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = 256
memory = 512
runtime_platform {
operating_system_family = "LINUX"
cpu_architecture = var.architecture
}
# create container definition
container_definitions = jsonencode([
{
name = "${var.project_name}-${var.environment}-container"
image = "${var.container_image}"
essential = true
portMappings = [
{
containerPort = 80
hostPort = 80
}
]
environmentFiles = [
{
value = "arn:aws:s3:::${var.project_name}-${var.env_file_bucket_name}/${var.env_file_name}"
type = "s3"
}
]
logConfiguration = {
logDriver = "awslogs",
options = {
"awslogs-group" = "${aws_cloudwatch_log_group.log_group.name}",
"awslogs-region" = "${var.region}",
"awslogs-stream-prefix" = "ecs"
}
}
}
])
}
# create ecs service
resource "aws_ecs_service" "ecs_service" {
name = "${var.project_name}-${var.environment}-ecs_service"
launch_type = "FARGATE"
cluster = aws_ecs_cluster.ecs_cluster.id
task_definition = aws_ecs_task_definition.ecs_task_definition.arn
platform_version = "LATEST"
desired_count = 2
deployment_minimum_healthy_percent = 100
deployment_maximum_percent = 200
# task tagging configuration
enable_ecs_managed_tags = false
propagate_tags = "SERVICE"
# vpc and security groups
network_configuration {
subnets = [aws_subnet.private_eligantapp_subnet_az1.id, aws_subnet.private_eligantapp_subnet_az2.id]
security_groups = [aws_security_group.app_server_security_group.id]
assign_public_ip = false
}
# load balancing
load_balancer {
target_group_arn = aws_lb_target_group.alb_target_group.arn
container_name = "${var.project_name}-${var.environment}-container"
container_port = 80
}
}